[英]My java Code in Eclipse seems to run but MySQL doesn't insert it into the table why? Can we use two try catch?
I am not getting where I am wrong. 我没有走错地方。 Java code is running smoothly but MySQL database doesn't displays the values inserted through query.
Java代码运行平稳,但是MySQL数据库不显示通过查询插入的值。 Can we use multiple try catch as I have used?Here is the main part of my code:
我们可以像以前一样使用多个try catch吗?这是代码的主要部分:
**Part of my main code::
JButton btnSubmit = new JButton("Submit");
btnSubmit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
int regno;
String bname,aname;
double cost;
try{
regno = Integer.parseInt(textField_regno.getText());
bname = textField_bookname.getText();
aname = textField_authorname.getText();
cost = Double.parseDouble(textField_cost.getText());
Class.forName("com.mysql.jdbc.Driver");
try{
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:1521/book","username","passwrd");
PreparedStatement stmt = conn.prepareStatement("INSERT INTO book('reg_no','b_name','a_name','cost')Values(?,?,?,?)");
stmt.setString(1, "regno");
stmt.setString(2, "bname");
stmt.setString(3, "aname");
stmt.setString(4, "cost");
JOptionPane.showMessageDialog(null,"Successfully added to the database");
}
catch(Exception e){
e.printStackTrace();
}
}
catch(Exception e1){
JOptionPane.showMessageDialog(null,"Check your Values" +e1);
}
}
});**
You are missing call to
executeUpdate()
您缺少对
executeUpdate()
调用
Add below line in your code - 在代码中添加以下行-
stmt.executeUpdate()
After 后
stmt.setString(4, "cost");
stmt.setString(1, "regno"); stmt.setString(2, "bname"); stmt.setString(3, "aname"); stmt.setString(4, "cost");
Here everything is being passed as a String (regardless of variable's value). 在这里, 所有内容都以字符串形式传递 (与变量的值无关)。 So try this:
所以试试这个:
stmt.setString(1, regno);
stmt.setString(2, bname);
stmt.setString(3, aname);
stmt.setString(4, cost);
Also, you haven't used stmnt.executeUpdate()
. 另外,您还没有使用过
stmnt.executeUpdate()
。 Add that after this. 在此之后添加。
And you don't need multiple try/catch
. 而且您不需要多次
try/catch
。 One does all the work. 一个人完成所有工作。 Just make sure you code it properly.
只要确保您正确编码即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.