Share on Google+Share on Google+

apis lavida
SQLException:Column not found? (help me:(
0 Answer(s)      4 years ago
Posted in : JDBC
import javax.swing.*;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
public class Mini2 extends JFrame implements ActionListener
{
    String userid="data",password="1234";
    String url="jdbc:odbc:miniprojek";
    Statement stmt;
    Connection con;
    JLabel lblCode,lblName,lblPrice,lblSupplier;
    JTextField txtCode,txtName,txtPrice,txtSupplier;
    JButton btnInsert,btnDisplay,btnSearch,btnDelete,btnExit;
    JTextArea txtDisplay;
    Container c;
    public Mini2()
        {
            super("Store Data");
            setSize(300,400);
            setVisible(true);
            c=getContentPane();
            c.setLayout(new FlowLayout());

            lblCode=new JLabel("Stock Code:");
            lblName=new JLabel("Stock Name:");
            lblPrice=new JLabel("Price per Unit:");
            lblSupplier=new JLabel("Supplier:");

            txtCode=new JTextField(20);
            txtName=new JTextField(20);
            txtPrice=new JTextField(20);
            txtSupplier= new JTextField(20);

            c.add(lblCode);
            c.add(txtCode);
            c.add(lblName);
            c.add(txtName);
            c.add(lblPrice);
            c.add(txtPrice);
            c.add(lblSupplier);
            c.add(txtSupplier);

            btnInsert=new JButton("Insert");
            btnDisplay=new JButton("Display");
            btnSearch=new JButton("Search");
            btnDelete=new JButton("Delete");
            btnExit=new JButton("Exit");
            c.add(btnInsert);
            c.add(btnDisplay);
            c.add(btnSearch);
            c.add(btnDelete);
            c.add(btnExit);
            btnInsert.addActionListener(this);
            btnDisplay.addActionListener(this);
            btnSearch.addActionListener(this);
            btnDelete.addActionListener(this);
            btnExit.addActionListener(this);

            txtDisplay=new JTextArea();
            txtDisplay.setColumns(20);
            c.add(txtDisplay);
        }
    public void actionPerformed(ActionEvent a)
    {
        if(a.getActionCommand() =="Insert")
            insertData();
            if(a.getActionCommand()=="Display")
                accessData();
                if(a.getActionCommand()=="Search")
                    searchData();
                    if(a.getActionCommand()=="Delete")
                    deleteData();
                if(a.getActionCommand()=="Exit")
                {
                    dispose();
                    try{ stmt.close();
                         con.close();
                         System.exit(0);
                    }catch(Exception e){}
                    }
        }

    public void getConnection()
    {
        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        }
        catch(java.lang.ClassNotFoundException e)
        {
            System.err.print("ClassNotFoundException:");
            System.err.println(e.getMessage());
        }
        try
        {
            con = DriverManager.getConnection(url,userid,password);
        }
        catch(SQLException ex)
        {
            System.err.println("SQLException:"+ex.getMessage());
        }
    }

        public void insertData()//insert data
        {
            String insertString1;
            String Stock_Code=txtCode.getText();
            String Stock_Name = txtName.getText();
            String Price =txtPrice.getText();
            String Supplier =txtSupplier.getText();
            insertString1 = "insert into stock values('"+Stock_Code+"','"+Stock_Name+"','"+Price+"','"+Supplier+"')";
            int codeNum = Integer.parseInt(Stock_Code);

            try
            {
                stmt = con.createStatement();
                stmt.executeUpdate(insertString1);
            }catch(SQLException ex)
            {
                System.err.println("SQLException:"+ex.getMessage());
            }
            JOptionPane.showMessageDialog(null,"Data Inserted into stock Table");
            txtCode.setText("");
            txtName.setText("");
            txtPrice.setText("");
            txtSupplier.setText("");
        }

        public void accessData()
        {
            txtDisplay.setText("");
            txtDisplay.setText("\tDisplay Output");
            txtDisplay.append("\nStock Code\tStock Name\tPrice per Unit\tSupplier\n");

            String result = "";
            String selectString;
            selectString ="select*from stock";
            try
            {
                stmt=con.createStatement();
                ResultSet rs=stmt.executeQuery(selectString);
                while(rs.next())
                {
                    int code=rs.getInt("Stock_Code");
                    String name = rs.getString("Stock_Name");
                    String price = rs.getString("Price");
                    String supplier = rs.getString("Supplier");
                    result+=code+"\t"+name+"\t"+price+"\t"+supplier+"\n";
                }
                txtDisplay.append(result);
            }catch(SQLException ex)
            {
                System.err.println("SQLException:"+ex.getMessage());
            }
        }

        public void searchData()
            {
                txtDisplay.setText("");
                txtDisplay.setText("\tDisplay Output");
                txtDisplay.append("\nStock Code\tStock Name\tPrice per Unit\tSupplier\n");

                String result = "";
                String searchString;
                String code1 = txtCode.getText();

                searchString ="select * from stock where Stock_Code ="+code1;
                int codeNum = Integer.parseInt(code1);
                try
                {
                    stmt=con.createStatement();
                    ResultSet rs=stmt.executeQuery(searchString);
                    while(rs.next())
                    {
                        int code=rs.getInt("Stock_Code");
                        String name = rs.getString("Stock_Name");
                        String price = rs.getString("Price");
                        String supplier = rs.getString("Supplier");
                        result=code+"\t"+name+"\t"+price+"\t"+supplier+"\n";
                    }
                    txtDisplay.append(result);
                }catch(SQLException ex)
                {
                    System.err.println("SQLException:"+ex.getMessage());
                }
        }
        public void deleteData()
        {
            String stockcode = txtCode.getText();
            try{
                Statement stmt = con.createStatement();
                String deleteString = "delete from stock where Stock_Code="+stockcode;
                int codeNum = Integer.parseInt(stockcode);
                int delete = stmt.executeUpdate(deleteString);
            }
            catch (SQLException s)
            {
            System.out.println("SQL statement is not executed!");
            }
            JOptionPane.showMessageDialog(null,"Record is deleted");
        }

        public static void main(String args[])
        {
            Mini2 obj = new Mini2();
            obj.getConnection();
        }
}//end of class


![access database][1]


  [1]: http://i.stack.imgur.com/9fz98.jpg

Ads

View Answers









Related Tutorials/Questions & Answers:
Tutorials   
Java Spring Hibernate Struts Training how to retrieve text and images from mysql database and show on html page using jsp servlet How to install Neo4j on Ubuntu 15.10? please help me in these prog MYSQL - mysql copy table to another table example by creating new table solution for mapping hibernate and pojos from net beans through database Connect GWT application with Oracle inventory System of Medical Store inventory System of Medical Store inventory System of Medical Store inventory System of Medical Store This code have created jTable that connected with a database.every cell of last column have a button.But this button doesn't work properly.Where is the error java program Java Database Program jdbc Database connection class in a live project ..?? What is JDBC architecture? Unable to Create DSN for excel to ms access importing data and edit that data Database Scrolling Buttons how to use foreign key in java How to delete content of a log file from linux terminal? JSP-Oracle connectivity Student Management System Java code for Saving Marathi (Indian local language ) in Mysql and retrieving the same crud operations with foreign key constraint. I really need help with this assignment question Please help me out Please Backup Failed Mysqldump Got Errno 28 On Write Upgrade MySQL 5.1 to 5.5 in cpanel mysqldump all databases com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'jpa.sequence' doesn't exist persistence xml class element persistence.xml for MySQL MySQL JDBC Driver pom dependency code Blob column jdk6 and oracle 8i connexion oracle 8i et jdk6 java [ cannot retrive date from sql ] why?? Order Entry System How to threow timeout exception when DB is not responding How to threow timeout exception when DB is not responding This is the employee form to add data to mysql database but I got error of NullPointerException, Is this write code please give me feedback as soon as possible make a method which takes user name & pwd as parameter & validate them against the database. If the corresponding user exist return true otherwise false. prabhakar validating credentials and displaying error message in login .jsp if not valid JDBC jdbc jdbc code register servlet vibhu connectiondetails from vibhu

Ads

 
Advertisement null

Ads