如标题所示,我已经尽力将用户输入与我的数据库进行比较,它使用了真正的输入,但是当我的数据库中不存在输入时我不知道什么是错误的我的下面的代码,请帮助
private class da implements ActionListener{
public void actionPerformed(ActionEvent e){
Connection con = null;
Statement state = null;
Statement state2 = null;
ResultSet rs = null;
ResultSet rs2 = null;
String url = "jdbc:mysql://localhost:3306/المكتبة";
String unicode = "?useUnicode=yes&characterEncoding=UTF-8";
try{
con = DriverManager.getConnection(url+unicode,"root","");
state = con.createStatement();
state2 = con.createStatement();
rs = state.executeQuery("SELECT * FROM library WHERE author ='"+name.getText()+"'")
;
rs2=state2.executeQuery("SELECT * FROM library WHERE bookname ='"+title.getText()+"'");
while(rs.next()){
String authorname = rs.getString("author");
String bookname = rs.getString("bookname");
String categort = rs.getString("category");
int isbn = Integer.parseInt(rs.getString("ISBN"));
String data = "اسم المؤلف: "+authorname+"\n"+"اسم الكتاب: "+bookname+"\n"+"التصنيف: "+categort+"\n"+"ISBN: "+isbn;
if(name.getText().trim().equals(authorname))
txt.setText(data);
else
txt.setText("dosen't exist");
}
while(rs2.next()){
String authorname = rs.getString("author");
String bookname = rs.getString("bookname");
String categort = rs.getString("category");
int isbn = Integer.parseInt(rs2.getString("ISBN"));
String data2 = "اسم المؤلف: "+authorname+"\n"+"اسم الكتاب: "+bookname+"\n"+"التصنيف: "+categort+"\n"+"ISBN: "+isbn;
txt.setText(data2);
}
}
catch(Exception t){
}
} }
}
答案 0 :(得分:1)
首先不要在sql查询上使用字符串组合,请使用PreparedStatement。
并尝试这样:
PreparedStatement upd = connect.prepareStatement("select * from library where author=?");
upd.setString(1,VariableToholdAuthor);
ResultSet rs = upd.executeQuery();
while(rs.next())
{
CompareVariable = rs.getString("name");
}
这会对你有帮助。