如何将 JTextField 中的数据保存到 mysql 数据库中?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不确定这可能是问题所在,但您的
stat.executeQuery(...)
语句是错误的,它应该是:要将字符串与变量连接,请使用
+
运算符。Not sure this can be the issue but your
stat.executeQuery(...)
statement is wrong it should be:To concat string with variables use
+
operator.将此代码复制并粘贴到您的项目中,这是 100% 工作
数据插入方法:
然后使用按钮调用该方法:
Copy and paste this code into your project it is 100% work
data insert method:
Then call the method using button: