在选择行然后单击按钮时从多个表中获取数据 [英] geting data from multiple table when selecting a row then clicking a button
问题描述
我尝试运行下面的代码但是每次运行它都不起作用。请问任何人可以强调我做错了什么?
I am try to run the code below but every time I run it, it don't work. Could any one please highlight what i am doing wrong?
代码应该做的是:
我有在数据库中调用表r和另一个名为sa的表,表r包含列表示为外键。在我的java前端,我在jpanel中有一个Jtable,在另一个jpanel中有一个更新按钮。当用户在jtable中选择一行然后单击更新时。如果jtable r中的选定行具有sa id作为外键,该工具将显示来自文本框中的r的数据以及来自sa的数据。
I have to table in the database called r and another called sa, table r contain column said as a foreign key. in my java front end i have a Jtable in a jpanel and an update button in another jpanel. when the user select a row in the jtable and then click update. the tool will display data from r in text boxes as well as data from sa if the selected row in the jtable r have sa id as a foreign key.
代码
if(updateClicked == true){
btnSubmit.setVisible(false);
btnUpdate.setVisible(true);
btnNew.setEnabled(false);
Statement st;
PreparedStatement ps;
ResultSet rs;
try {
String rid = table.getValue(0);
JOptionPane.showMessageDialog(null, rid);
String rq ="SELECT * FROM `r` WHERE 'r_id`=' "+rid+"'";
ps = Login.con.prepareStatement(rq);
rs = ps.executeQuery();
String saID = rs.getString(2);
String q = "SELECT sa.Argument FROM sa, r WHERE r.sid ="+sID ;
st = Login.con.createStatement();
rs = st.executeQuery(q);
String argu = rs.getString(1);
System.out.println(argu);
if (argu.isEmpty()==false){
btnAddSA.doClick();
txtaArg.setText(argu);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
控制台输出
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:'where子句'中的未知列'R0004'
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'R0004' in 'where clause'
推荐答案
首先给你的表格提供专有名称。 r和sa没有任何意义。
First of all give your tables proper names. "r" and "sa" don't mean anything.
我不知道你的SQL有什么问题,但我建议你使用PreparedStatement。它使得编写SQL更容易,因此您不必担心分隔符类型错误。
I don't know what the problem with your SQL is but I will suggest that you use a PreparedStatement. It makes it easier to code the SQL so you don't worry about delimiter type mistakes.
一个基本的例子是:
String sql = "INSERT INTO Page (Name, Title) VALUES (?, ?)";
PreparedStatement stmt = connection.prepareStatement(sql);
stmt.setString( 1, "Name1" );
stmt.setString( 2, "Title1" );
stmt.executeUpdate();
您可以在论坛或网站上搜索更多信息。
You can search the forum or web for more information.
这篇关于在选择行然后单击按钮时从多个表中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!