java连接MS Sql2005的代码实例如下:
package pub;
import java.sql.*;
public class DBConn {
public Connection conn() {
String driverClassName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url = "jdbc:sqlserver://localhost:1433;databaseName=SQLName"; //SQLName为数据库名字
String password = "密码";
String user1 = "用户名";
Connection conn = null;
try {
Class.forName(driverClassName);
} catch (ClassNotFoundException ex) {
System.out.println("加载错误!");
}
try {
conn = DriverManager.getConnection(url, user1, password);
System.out.println("成功");
} catch (SQLException ex1) {
System.out.println("失败");
}
return conn;
}
}