你好我正在使用java和MySQL。我的實施如下:Java檢查數據庫中是否存在值
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.sql.*;
import java.util.Scanner;
public class BankingSystem extends JFrame {
public static void main(String[] args) throws Exception{
int ur=0;
int PIN;
String ID;
Scanner s=new Scanner(System.in);
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/BankingSystem";
String user = "root";
String pass="";
Connection con = DriverManager.getConnection(url,user,pass);
Statement st = con.createStatement();
System.out.println("Enter Your 4 digit PIN");
PIN=s.nextInt();
ResultSet rs=st.executeQuery("select * from customerinformation where pin ="+PIN);
//checking for existance of user entered pin
if((rs.getString(1)).equals("")){System.out.println("Invalid PIN");}
while(rs.next())
{
System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3)+" "+rs.getString(4));
}
}
}
但這段代碼不起作用。給某種異常錯誤,但當我刪除包含if語句的行,那麼它工作正常。如何檢查引腳是否有效。
它給以下異常:
Exception in thread "main" java.sql.SQLException: Before start of result set
at com.mysql.jdbc.ResultSet.checkRowPos(ResultSet.java:3624)
at com.mysql.jdbc.ResultSet.getString(ResultSet.java:1762)
at BankingSystem.main(BankingSystem.java:22)
您可以發佈異常的詳細信息? – 2014-08-30 13:56:54