public void conn1() throws SQLException {
/*
第三方的jar包,静态加载,灵活性差,依赖性强
*/
// 获取驱动
Driver driver = new Driver();
// 得到连接
String url="jdbc:mysql://localhost:3306/test1";
// 将用户和密码封装成Properties文件
Properties properties = new Properties();
properties.setProperty("user","root");
properties.setProperty("password","123456");
// 获得连接
Connection connect = driver.connect(url, properties);
System.out.println(connect);
// 得到SQL语句
String sql="update emp set empname='周润发' where empno = 'E101'";
Statement statement = connect.createStatement();
statement.executeUpdate(sql);
// 关闭资源
statement.close();
connect.close();
}
该方式采取第三方的jar包,静态加载,灵活性差,依赖性强
public void conn2() throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException {
/*
使用反射机制动态加载Driver类,减少依赖性
*/
//用配置文件保存"com.mysql.jdbc.Driver"
Class aClass = Class.forName("com.mysql.jdbc.Driver");
Driver driver = (Driver) aClass.newInstance();
String url="jdbc:mysql://localhost:3306/test1";
Properties properties = new Properties();
properties.setProperty("user","root");
properties.setProperty("password","123456");
// 获得连接
Connection connect = driver.connect(url, properties);
System.out.println(connect);
// 得到SQL语句
String sql="update emp set empname='周润发' where empno = 'E101'";
Statement statement = connect.createStatement();
statement.executeUpdate(sql);
// 关闭资源
statement.close();
connect.close();
}
此方式使用反射机制动态加载Driver类,减少依赖性
public void conn3() throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException {
Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
Driver driver = (Driver) aClass.newInstance();
String url="jdbc:mysql://localhost:3306/test1";
String user="root";
String password="123456";
// 注册driver驱动
DriverManager.registerDriver(driver);
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println(connection);
}
该方式采用Driver Manager的连接管理包对连接进行管理,有助于方便管理
public void conn4() throws ClassNotFoundException, SQLException {
/*
使用class.forname自动完成注册,简化代码
*/
// 使用反射机制
// 在加载Driver类时自动注册
/* static {
try {
DriverManager.registerDriver(new Driver());
} catch (SQLException var1) {
throw new RuntimeException("Can't register driver!");
}*/
// 底层原码静态代码块中自动注册了
Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/test1";
String user="root";
String password="123456";
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println(connection);
}
这种方式采用Class.forName()自动注册,采用反射机制加载
public void conn5() throws IOException, ClassNotFoundException, SQLException {
Properties properties = new Properties();
properties.load(new FileInputStream("src\\mysql.properties"));
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String url = properties.getProperty("url");
Class.forName("jdbc:mysql://localhost:3306/test1");
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println(connection);
}
该方式同上