import java.sql.*;
public class DBConnection
{
// 指定驱动程序
private static String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
// 创建指定数据库的URL
private static String url= "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test";
//提供用户名和密码
private static String user="sa";
private static String password="sasa";
public static Connection getConnection() {
Connection con;
try {
// 加载驱动程序
Class.forName(driver);
// 创建连接
con = DriverManager.getConnection(url, user, password);
return con;
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return null;
}
}表示感谢
public class DBConnection
{
// 指定驱动程序
private static String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
// 创建指定数据库的URL
private static String url= "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test";
//提供用户名和密码
private static String user="sa";
private static String password="sasa";
public static Connection getConnection() {
Connection con;
try {
// 加载驱动程序
Class.forName(driver);
// 创建连接
con = DriverManager.getConnection(url, user, password);
return con;
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return null;
}
}表示感谢
Connection con;
try {
// 加载驱动程序
Class.forName(driver);
// 创建连接
con = DriverManager.getConnection(url, user, password);
return con;
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return null;
}这里不是很明白这些语句的作用
// 加载驱动程序(要运行连接数据库的驱动程序)
Class.forName(driver);
// 创建连接(根据你所提供的用户名和密码和数据库连接)
con = DriverManager.getConnection(url, user, password);
return con;(如果连接成功,返回连接)
} catch (ClassNotFoundException e) {(找不到类的异常)
System.out.println(e.getMessage());(打印出异常的信息)
} catch (SQLException e) {(Sql的异常)
System.out.println(e.getMessage());(打印出异常的信息)
}
return null;(如果连接失败,返回null)