Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将JTextField中的数据保存到mysql数据库中?_Java_Swing_Jdbc_Awt - Fatal编程技术网

Java 如何将JTextField中的数据保存到mysql数据库中?

Java 如何将JTextField中的数据保存到mysql数据库中?,java,swing,jdbc,awt,Java,Swing,Jdbc,Awt,当我尝试使用s1和s2更新数据库时,出现了一个错误。不确定这是否是问题所在,但您的stat.executeQuery。。。声明是错误的,应该是: 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

当我尝试使用s1和s2更新数据库时,出现了一个错误。

不确定这是否是问题所在,但您的stat.executeQuery。。。声明是错误的,应该是:

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

要使用+运算符压缩带有变量的字符串。

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

数据插入方法:

stat.executeQuery("insert into name (first,last) values('"+s1+"','"+s2+"')");
然后使用按钮调用该方法:

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

}

这与你的问题无关,只是更改面板p=新面板;到JPanel p=新JPanel;,请不要把AWT和SwingConsider混为一谈,要考虑接受/支持有用的答案。阅读以下内容:另请参见和,例如。
        b1.addActionListener(new ActionListener() {

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

    });