58,452
社区成员




package com.test;
import java.sql.*;
public class Test {
public void getConnectionSqlServer() {
String driverName = "com.jdbc.sqlserver.SQLServerDriver";
String dbURL = "jdbc:sqlserver://localhost:1433;DatabaseName=db_database28";
String userName = "sa"; // 用户名
String userPwd = "123"; // 密码
Connection dbConn = null;
try {
Class.forName(driverName).newInstance();
} catch (Exception ex) {
System.out.println("驱动加载失败");
ex.printStackTrace();
}
try {
dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
System.out.println("成功连接数据库!");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (dbConn != null)
dbConn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Test getConn = new Test();
getConn.getConnectionSqlServer();
}
}
try {
Class.forName(driverName).newInstance();
} catch (Exception ex) {
System.out.println("驱动加载失败");
ex.printStackTrace();
}
try {
dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
System.out.println("成功连接数据库!");
你的代码里只有驱动加载失败 和 成功连接数据库啊,没有驱动加载成功啊
try {
Class.forName(driverName).newInstance();
} catch (Exception ex) {
System.out.println("驱动加载失败");
ex.printStackTrace();
}
try {
dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
System.out.println("成功连接数据库!");
你的代码里只有驱动加载失败 和 成功连接数据库啊,没有驱动加载成功啊[/quote]
我一开始写的时候有的,后面好像改来改去不小心删去了,已经连接成功,谢谢耐心回答