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

发布于 2024-11-25 12:14:46 字数 1682 浏览 0 评论 0原文

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 更新数据库时,发生错误。

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

When I try to update the database using s1 and s2 an error occurs.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

要走干脆点 2024-12-02 12:14:46

不确定这可能是问题所在,但您的 stat.executeQuery(...) 语句是错误的,它应该是:

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

要将字符串与变量连接,请使用 + 运算符。

Not sure this can be the issue but your stat.executeQuery(...) statement is wrong it should be:

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

To concat string with variables use + operator.

梦屿孤独相伴 2024-12-02 12:14:46

将此代码复制并粘贴到您的项目中,这是 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();
        }

    });

Copy and paste this code into your project it is 100% work

data insert method:

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

}

Then call the method using button:

        b1.addActionListener(new ActionListener() {

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

    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文