Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa JDBC SQLException:Column not found? (help me:(

Have Programming Question? Ask it here!
 
 
 


apis lavida
SQLException:Column not found? (help me:(
0 Answer(s)      a year and 3 months 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

Advertisement
View Answers

Related Tutorials/Questions & Answers:
Advertisements
 

 

 

DMCA.com