我在Java(Jdbc:Odbc)中建立数据库连接时遇到SQL错误。如何解决此错误?
以下是相关代码:
import java.sql.*;
import java.lang.*;
import java.io.*;
public class db {
public static void main(String args[]) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:data","system","password");
System.out.println("\n connection established");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from stud");
System.out.println("details of empdata\n");
while(rs.next()) {
System.out.println(rs.getInt(1)+"\t"+rs.getString(2)+"\t"+rs.getString(3));
}
} catch(Exception e) {
System.out.println("sql error");
}
}
}
答案 0 :(得分:-1)
/ *执行查询需要字符串作为参数,如果你没有找到类找不到异常而不是首先处理它,你在查询时没有使用半冒号 这应该通过双Q = =#34;从螺柱中选择*;" * /
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:data","system","password");
System.out.println("\n connection established");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from stud;");//----HERE--*/
System.out.println("details of empdata\n");
while(rs.next()) {
System.out.println(rs.getInt(1)+"\t"+rs.getString(2)+"\t"+rs.getString(3));
}
} catch(Exception e) {
System.out.println("sql error");
}
}
}