gpt4 book ai didi

java - 我想从用户那里获取数据并将其与我在 java 中的数据库进行比较

转载 作者:搜寻专家 更新时间:2023-10-30 19:50:53 24 4
gpt4 key购买 nike

如标题所示,我非常努力地将用户输入与我的数据库进行比较,它适用于真实输入,但是当我的数据库中不存在输入时,我不知道哪里出了问题我下面的代码,请帮忙

 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");
}

这对你有帮助。

关于java - 我想从用户那里获取数据并将其与我在 java 中的数据库进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27454844/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com