我正在创建一个程序,该程序将允许显示和操作数据库中的数据。我根据找到的教程编写了以下代码:
public class JavaApplication25 {
public static void main(String[] args) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:*****";
Connection conn = DriverManager.getConnection(url, "****", "****");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM dbo.Patients");
System.out.println(rs);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
这是代码的输出:
run:
sun.jdbc.odbc.JdbcOdbcResultSet@1cda59b
BUILD SUCCESSFUL (total time: 1 second)
我的问题是为什么程序不向我显示 SQL Server 数据库中的数据?谁能告诉我如何显示它?
我将其重新编码为:
import java.sql.*;
public class JavaApplication25 {
public static void main(String[] args) {
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:OJT_dsn";
String user = "sa";
String pass = "";
Connection connection = DriverManager.getConnection(url, user, pass);
Statement statement = connection.createStatement();
String sql = "SELECT pIDNo,pLName,pFName,pMI,pSex,pStatus FROM dbo.Patients";
ResultSet result = statement.executeQuery(sql);
while(result.next()){
int ID = result.getInt("ID");
String Lname = result.getString("Lname");
String Fname = result.getString("Fname");
String MI = result.getString("MI");
String Sex = result.getString("Sex");
String Status = result.getString("Status");
System.out.println(ID);
System.out.println(Lname);
System.out.println(Fname);
System.out.println(MI);
System.out.println(Sex);
System.out.println(Status);
}
result.close();
statement.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
}
我的编码有什么问题?现在我收到了这个错误:
java.sql.SQLException:在 sun.jdbc.odbc.JdbcOdbcResultSet.getInt(JdbcOdbcResultSet.java:600) 在 javaapplication25.JavaApplication25.main( JavaApplication25.java:28) 构建成功(总时间:1 秒)