import java.sql.Connection;//导入连接数据库类
import java.sql.DriverManager;//导入数据库驱动管理类import java.sql.SQLException; //导入连接数据库异常类
public class ConnDb {
static Connection conn = null;
static String dbUrl ="127.0.0.1",
dbName ="friendanDB",
dbUser ="sa",
dbPass ="test";
public static void main(String[] args) {
//加载数据库驱动
//注意在加前在工程目录入引入连接数据库的驱动文件sqljdbc4.jar
//sqljdbc4.jar里面包含有类SQLServerDriver
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}
catch (ClassNotFoundException e) {
System.out.println("未找到驱动文件,错误信息:" + e.toString());
return;
}
//连接数据库
try {
conn = DriverManager.getConnection("jdbc:sqlserver://" + dbUrl
+ ":1433;databaseName=" + dbName, dbUser, dbPass);
System.out.print(conn.getCatalog());
}
catch (SQLException e) {
System.out.println("连接数据库失败,错误信息:" + e.toString());
return;
}
System.out.println("连接数据库成功!");
}
}