public class DButil {
private static Connection ct=null;
public static Connection getConn(){
try {
//加载驱动
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
//得到连接
ct=(Connection) DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=BOOKSHOP","sa","910212");
//System.out.println("ok");
} catch (Exception e) {
e.printStackTrace();
}
return ct;
}
}
连接数据库载入驱动的时候ct= DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=BOOKSHOP","sa","910212");
为什么会报错,加一个Connection就可以了 这有什么区别?
private static Connection ct=null;
public static Connection getConn(){
try {
//加载驱动
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
//得到连接
ct=(Connection) DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=BOOKSHOP","sa","910212");
//System.out.println("ok");
} catch (Exception e) {
e.printStackTrace();
}
return ct;
}
}
连接数据库载入驱动的时候ct= DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=BOOKSHOP","sa","910212");
为什么会报错,加一个Connection就可以了 这有什么区别?
因为你引入import java.sql.Connection;
import java.sql.DriverManager;
这两个的时候不是同时为sql目录下的,所以必需加上(Connection)强制类型转换,lz你可以试试!
那你有2005的数据库驱动,给我发一个,[email protected]
http://msdn.microsoft.com/en-us/sqlserver/aa937724.aspx
private static Connection conn=null;
public static Connection getConn(){
try {
//加载驱动
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
//得到连接
conn=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;databaseName=BOOKSHOP","sa","910212");
if(!conn.isClosed())
System.out.println("ok");
else
System.out.println("error");} catch (Exception e) {e.printStackTrace();
}
return ct;
}
}楼主测试一下,如果你是2005的话,Class.forName("com.microsoft.sqlserver。jdbc.SQLServerDriver");
conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=BOOKSHOP","sa","910212");
严重: 此驱动程序不支持 Java Runtime Environment (JRE) 1.6 版。请使用支持 JDBC 4.0 的 sqljdbc4.jar 类库。
java.lang.UnsupportedOperationException: 此驱动程序不支持 Java Runtime Environment (JRE) 1.6 版。请使用支持 JDBC 4.0 的 sqljdbc4.jar 类库。我就把sqljdbc4导进去就可以用了。之前都没报这些错误,今天啥就报了。问题解决了,哈哈