我正在尝试在Netbeans中构建一个简单的用户名 - 密码登录应用程序。我已将用户名和密码存储在PhpMyAdmin数据库中。当我在文本字段中使用PreparedStatement
到setString
时,会显示错误消息:Cannot find symbol for the method setString(int, String)
。
工作代码:
//Importing stuffs
import java.awt.HeadlessException;
import java.sql.PreparedStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.DriverManager;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
private void enterActionPerformed(java.awt.event.ActionEvent evt) { //login button
String sql = "select * from UserInfo where Username=? and Password=?";
try{
con = DriverManager.getConnection("jdbc:mysql://localhost/phpmyadmin/LoginInfo", "root","");
pst = con.prepareStatement(sql);
pst = setString(1, username.getText());//error here
pst = setString(2, password.getText());//error here
rs= pst.executeQuery();
if(rs.next()){
JOptionPane.showMessageDialog(null, "good");
}else{
JOptionPane.showMessageDialog(null, "bad");
}
}catch(Exception ex){
JOptionPane.showMessageDialog(null, ex);
}
}
答案 0 :(得分:2)
不要使用:
pst = setString(1, username.getText());//error here
pst = setString(2, password.getText());//error here
改为使用:
pst.setString(1, username.getText());
pst.setString(2, password.getText());
阅读本教程关于Using Prepared Statements
的内容