JAVA连SQL SERVER
package system;
import java.sql.*;
public class DateBase {
private String db;
private String uname;
private String pwd;
private Connection conn;
private Statement stat;
DateBase(String db,String uname,String pwd) {
this.db = db;
this.uname = uname;
this.pwd = pwd;
}
public ResultSet executeQuery(String sql) {
try {
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver ");
}
catch (
ClassNotFoundException ex) {
ex.printStackTrace();
return null;
}
try {
conn = DriverManager.getConnection( "jdbc:odbc: " + this.db, "sa ", "sa ");
if(conn==null) {
return null;
} else {
stat = conn.createStatement();
return stat.executeQuery(sql);
}
}
catch (
SQLException ex1) {
ex1.printStackTrace();
return null;
}
}
}
为什么一运行就是
java.sql.SQLException: [Microsoft][ODBC 驱动程序管理器] 无效的描述器索引
初学JDBC,请高手指教!
------解决方案--------------------这是连接 ORACLE 数据库的 程序 我看你是连接SQL SERVER2000 把URL 改一下OK了
public ResultSet lookUp(String sql){
try{
connect = DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:XDF ", "scott ", "tiger ");
Statement stmt = connect.createStatement();
rs = stmt.executeQuery(sql);
}
catch(Exception e){
e.printStackTrace();
}
return rs;
}
------解决方案--------------------public class A{
public static void main(String[]args){
Class.forName( "com.microsoft.jdbc.sqlserver.SQLServerDriver ");
Connection conn = DriverManager.getConnection( "jdbc:micorsoft:sqlserver://localhost:1433;databasename=test ", "sa ", "sa ");
Statement stmt = conn.createStatement();
String sql = "SELECT * FROM student ";
rs = stmt.executeQuery(sql);
while(rs.next()){
System.out.println(rs.getString(1));
}
}
}
------解决方案--------------------看一下这个把,http://www.blogchinese.com/0603/182791/archives/2006/200652692114.shtml。