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

主宰稳场 提交于 2019-12-14 03:44:45

问题


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","root","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);
            }
        }


}

回答1:


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());       
    }
}



回答2:


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.



来源:https://stackoverflow.com/questions/6838970/when-i-am-trying-to-save-the-data-to-mysql-an-exception-is-being-thrown-java-s

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!