我有以下代码:
package connectivity;
import java.sql.ResultSet;
import java.sql.Statement;
public class Connectivity {
public static void main(String[] args) {
cls o=new cls();
try{
Statement s=o.getct();
ResultSet rs;
rs=s.executeQuery("select * from REGISTRATION1");
while(rs.next()){
String st=rs.getString("FACULTY_ID");
String st2=rs.getString("STUDENT_ID");
}
}catch(Exception e){
System.out.println(e);
}
}
}
输出结果为:
java.sql.SQLException:
[Microsoft][ODBC SQL Server Driver]Invalid Descriptor Index"
为什么?
答案 0 :(得分:0)
请解释方法getct()中的连接语句? 确保你已添加 库中的mysql-connector-java-xyz-bin jar文件。 以下代码可能会有所帮助:
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
System.out.println("Driver Setup");
String connectionUrl = "jdbc:mysql://localhost:3306/db";
String connectionUser = "root";
String connectionPassword = "";//password if any
conn = DriverManager.getConnection(connectionUrl, connectionUser, connectionPassword);
System.out.println("Connection setup");
stmt = (Statement) conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM table_name");
while (rs.next()) {
String col1 = rs.getString("column1name");
String col2 = rs.getString("column2name");
System.out.println("First Value"+col1+"Second Value"+col2);
}
} catch (Exception e) {
e.printStackTrace();
}
}
答案 1 :(得分:0)
看起来FACULTY_ID或STUDENT_ID不存在。请检查ResultSet中列的名称或尝试按索引读取。