开发者

when i am trying to save the data to mysql, an exception is being thrown( java.sql.SQLException)

import java.awt.Panel;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.Statement;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JTextField;

import com.mysql.jdbc.Connection;

public class one implements ActionListener{

    JFrame frame = new JFrame("ghar hisab");
    JButton b = new JButton("save");
    Panel p = new Panel();
    JTextField f = new JTextField(20);
    JTextField f1 = new JTextField(20);
    JLabel l = new JLabel("Enter the first name");
    JLabel l1 = new JLabel("Enter the last name");
    String s1,s2;
    String ppl;
    int people;

            void display() throws Exception{
                                p.add(l);
                                p.add(f);
                                p.add(l1);
                                p.add(f1);
                                p.add(b);

                                frame.setSize(400,400);

                                frame.setVisible(true);
                                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                                frame.getContentPane().add(p);
                                s1=f.getText();
                                s2=f1.getText();


                //Class.forName("com.mysql.jdbc.Driver");
                //Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/people","roo开发者_高级运维t","desire");

                //Statement stat = con.createStatement();
            //  String s3= "insert into name values s1 + s2";
            //  stat.executeUpdate(s3);



        //  stat.executeQuery("insert into name (first,last) values('"+s1+"','"+s2+"')");
            b.addActionListener(this);

        //  ResultSet rs= stat.executeQuery("insert into name (first,last) values("arun","yadav"));


            //while(rs.next()){
        //      System.out.println(rs.getString(1)+" "+rs.getString(2));
        //  }




        }

        @Override
        public void actionPerformed(ActionEvent event) {
            // TODO Auto-generated method stub
            try{
                Class.forName("com.mysql.jdbc.Driver");

            Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/people","root","desire");

            Statement stat = con.createStatement();

            stat.executeQuery("insert into name (first,last) values('"+s1+"','"+s2+"')");
            }
            catch(Exception e){
                System.out.println("the exception caught is "+e);
            }
        }


}


Without the stack trace, it's difficult to tell. Either you aren't establishing a connection or the values in your SQL are causing the query to error out (perhaps one or both are null). I'd suggest putting a break point on con, and see if the object is null. And also see what s1 and s2 are. Below, I've added two lines. You need to close your statement and connection.

public void actionPerformed(ActionEvent event) {
    // TODO Auto-generated method stub
    try
    {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/people","root","desire");
        Statement stat = con.createStatement();
        stat.executeQuery("insert into name (first,last) values('"+s1+"','"+s2+"')");
        stat.close();
        con.close();
    }
    catch(Exception e){
        System.out.println("e.fillInStackTrace:" + e.fillInStackTrace());
        System.out.println("e.getCause:" + e.getCause());
        System.out.println("e.getLocalizedMessage:" + e.getLocalizedMessage());
        System.out.println("e.getMessage.:" + e.getMessage());
        System.out.println("e.getStackTrace:" + e.getStackTrace());
        System.out.println("e.initCause:" + e.initCause());
        System.out.println("e.printStackTrace:" + e.printStackTrace());
        System.out.println("e.toString:" + e.toString());       
    }
}


I noticed that you import PreparedStatement, but your code never uses it. Well, I don't know much about SQL but I do that when you use a PreparedStatement you are less likely to have problems. Here is a simple example for an insert:

String sql = "INSERT INTO Name (First, Last) VALUES (?, ?)";

PreparedStatement stmt = connection.prepareStatement(sql);

stmt.setString( 1, s1 );
stmt.setString( 2, s2 );
stmt.executeUpdate();

I took the liberty of uppercasing the table name and columns names since that is the way most people would create the table names.

Edit:

s1=f.getText();
s2=f1.getText(); 

When that code is executed the text field contains no values since the user hasn't had a chance to enter any data into the text fields yet. Those two statements need to be moved to the actionPerformed() method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜