当我尝试将数据保存到mysql时,抛出了异常(java.sql.SQLException) [英] when i am trying to save the data to mysql, an exception is being thrown( java.sql.SQLException)

查看:334
本文介绍了当我尝试将数据保存到mysql时,抛出了异常(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","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);
            }
        }


}

推荐答案

没有堆栈跟踪,很难分辨.您未建立连接,或者SQL中的值导致查询错误(也许一个或两个都为空).我建议在con上放置一个断点,然后查看该对象是否为null.还可以看到s1和s2是什么.在下面,我添加了两行.您需要关闭语句和连接.

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

这篇关于当我尝试将数据保存到mysql时,抛出了异常(java.sql.SQLException)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆