好的我有这个问题。我需要更新我的数据库上的记录,但由于我的SQL语法,我遇到了麻烦。通过按“更新名称”按钮,弹出2条消息,用户选择表的前一个名称和新名称。但该声明从未执行过,名称也不会改变。我的主要目标是将第1个语句中的id“解析”为变量
String name_1 = "SELECT id FROM consoles WHERE name LIKE ? ";
并在此处添加
String name = "UPDATE consoles SET name = ? WHERE id = ?";
是否可以将我从第一个声明中获得的ID解析为变量?
try
{
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/test1?user=me&password=12345");
String name_1 = "SELECT id FROM consoles WHERE name LIKE ? ";
String name = "UPDATE consoles SET name = ? WHERE name LIKE ?";
PreparedStatement psname = conn.prepareStatement(name);
PreparedStatement psname_1 = conn.prepareStatement(name_1);
String strin = JOptionPane.showInputDialog(null,"Previous Name : ");
String strout = JOptionPane.showInputDialog(null,"New Name : ");
psname.setString(1,strin);
psname.setString(2,"%" +strout+ "%");
psname_1.setString(1,"%"+strin+"%");
psname.executeUpdate();
Statement stmtname = conn.createStatement();
// show the updated table
ResultSet rsname = stmtname.executeQuery("SELECT * FROM consoles INNER JOIN hardware ON consoles.id=hardware.id");
.
.
.
ADDING THEM TO A JTABLE
.
.
.
答案 0 :(得分:1)
切换参数。
psname.setString(2,"%" +strout+ "%");
psname_1.setString(1,"%"+strin+"%");
应该是
psname.setString(2, "%" + strin + "%");
psname_1.setString(1, strout);
答案 1 :(得分:0)
try
{
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/test1?user=me&password=12345");
String name = "UPDATE consoles SET name = ? WHERE name LIKE ?";
PreparedStatement psname = conn.prepareStatement(name);
String strin = JOptionPane.showInputDialog(null,"Previous Name : ");
String strout = JOptionPane.showInputDialog(null,"New Name : ");
psname.setString(1,strout);
psname.setString(2,"%" +strin+ "%");
psname.executeUpdate();
Statement stmtname = conn.createStatement();
// show the updated table
ResultSet rsname = stmtname.executeQuery("SELECT * FROM consoles INNER JOIN hardware ON consoles.id=hardware.id");
.
.
.
ADDING THEM TO A JTABLE
.
.
.