用eclipse写java连接SQLServer数据库,该怎么解决
1个回答
展开全部
public static Connection getConnection() throws SQLException {
if (con != null && !con.isClosed())// 不为空且没关闭
return con;
//由首选项的设置获得连接
String className = "com.microsoft.sqlserver.jdbc.SQLServerDriver ";
String url = "jdbc:sqlserver://localhost:1433; ";
String username = "root ";
String password = "1234567 ";
//或 String url= "jdbc:sqlserver://localhost:1433;database=yourdatabasename;user=root;password=1234567 ";
// 生成一个数据库连接
try {
Class.forName(className);
con = DriverManager.getConnection(url, username, password);
//或 con=DriverManager.getconnection(url);
if (con != null && !con.isClosed())// 不为空且没关闭
return con;
//由首选项的设置获得连接
String className = "com.microsoft.sqlserver.jdbc.SQLServerDriver ";
String url = "jdbc:sqlserver://localhost:1433; ";
String username = "root ";
String password = "1234567 ";
//或 String url= "jdbc:sqlserver://localhost:1433;database=yourdatabasename;user=root;password=1234567 ";
// 生成一个数据库连接
try {
Class.forName(className);
con = DriverManager.getConnection(url, username, password);
//或 con=DriverManager.getconnection(url);
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询