如何将数据从JTextField保存到mysql数据库?

时间:2011-07-22 12:46:30

标签: java swing jdbc awt

import java.awt.Panel;
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 {

    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 first 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"')");


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


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

当我尝试使用s1和s2更新数据库时,会发生错误。

2 个答案:

答案 0 :(得分:8)

不确定这可能是问题,但您的stat.executeQuery(...)声明应该是错误的:

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

要使用+运算符连接变量字符串。

答案 1 :(得分:2)

将此代码复制并粘贴到您的项目中,这是100%工作

数据插入方法:

public void insert()
{   
    try {
    // connection string
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        Connection con = DriverManager
                .getConnection("jdbc:mysql://localhost:3306/Project?user=root&password=root");
        Statement st = con.createStatement();

        st.executeUpdate("insert into Register VALUES('"
                + tf1.getText() + "','" + tf2.getText() + "','"
                + tf3.getText() + "','" + cb1.getSelectedItem()
                + "','" + tf4.getText() + "','" + tf5.getText()
                + "','" + tf6.getText() + "','" + tf7.getText()
                + "','" + tf8.getText() + "','" + tf9.getText()
                + "'," + "'" + tf10.getText() + "','"
                + tf11.getText() + "','" + tf12.getText() + "','"
                + tf13.getText() + "','" + tf14.getText() + "',"
                + "'" + tf15.getText() + "','" +tf16.getText()
                + "','" + tf17.getText() + "','" + tf18.getText()
                + "'," + "'" + tf19.getText() + "','"
                + p1.getText() + "','" + p2.getText() + "')");

        JOptionPane.showConfirmDialog(null, "Your Data Has been Inserted",
                "Result", JOptionPane.DEFAULT_OPTION,
                JOptionPane.PLAIN_MESSAGE);

        st.close();
        con.close();

    }

    catch (Exception e1)

    {
        System.out.println("Exception:" + e1);
    }

}

然后使用按钮调用方法:

        b1.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent arg0) {
            insert();
        }

    });