我想从用户那里获取数据,并将其与我的java数据库进行比较
如标题所示,我已尽力将用户输入与我的数据库进行比较,它与真实输入一起工作,但当我的数据库中不存在输入时,我不知道我下面的代码有什么问题,请帮助我想从用户那里获取数据,并将其与我的java数据库进行比较,java,database,input,Java,Database,Input,如标题所示,我已尽力将用户输入与我的数据库进行比较,它与真实输入一起工作,但当我的数据库中不存在输入时,我不知道我下面的代码有什么问题,请帮助 private class da implements ActionListener{ public void actionPerformed(ActionEvent e){ Connection con = null; Statement state = null; St
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){
}
}
}
}
首先,不要在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");
}
这对你有帮助