用eclipse写java连接SQLServer数据库,该怎么解决

如题所述

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);
温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答