此为传统方法连接数据库,没有用连接池。
连接数据库步骤:
注册驱动----------打开连接-------------------执行语句------------关闭
(2021.8.29) 跑通每一种方法,但是原理不太懂后面慢慢再体会。待学知识点:反射
数据库为test2,创建表actor,
结果
1
属于静态加载,灵活性差,依赖强
public class Jdbctest {
@Test
public void connection() throws SQLException {
Driver driver=new com.mysql.cj.jdbc.Driver();
String url="jdbc:mysql://localhost:3306/test2?serverTimezone=GMT";
Properties properties=new Properties();
properties.setProperty("user","root");
properties.setProperty("password","ning");
Connection connection=driver.connect(url,properties);
//System.out.println(connection);
//--------------------------------------------------------------
String sql="insert into actor values(null,'刘大海','男')";
Statement statement=connection.createStatement();
int rows=statement.executeUpdate(sql);
System.out.println(rows>0?"成功":"失败");
statement.close();
connection.close();
}
}
使用反射加载Driver类,动态加载,更加的灵活,减少依赖性
@Test
public void connection2() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
//和第一种方式差别就在这,
Class<?> aClass=Class.forName("com.mysql.cj.jdbc.Driver");
Driver driver=(Driver)aClass.newInstance();
//--------------------------------------
String url="jdbc:mysql://localhost:3306/test2?serverTimezone=GMT";
Properties properties=new Properties();
properties.setProperty("user","root");
properties.setProperty("password","ning");
Connection connection=driver.connect(url,properties);
//System.out.println(connection);
//----------------------------------------------------------------------
String sql2="insert into actor values(null,'刘大海','男')";
Statement statement=connection.createStatement();
int rows=statement.executeUpdate(sql2);
System.out.println(rows>0?"成功":"失败");
statement.close();
connection.close();
}
推荐使用以下方法
利用Driver+DriverManager
@Test
public void connection3() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
Class<?> aCLass2=Class.forName("com.mysql.cj.jdbc.Driver");
Driver driver=(Driver)aCLass2.newInstance();
String url="jdbc:mysql://localhost:3306/test2?serverTimezone=GMT";
String user="root";
String password="ning";
DriverManager.registerDriver(driver);//本句可以不要,因为使用Class.forName自动完成注册驱动
Connection connection=DriverManager.getConnection(url,user,password );
//------------------------------
String sql3="insert into actor values(null,'刘大海','男')";
Statement statement=connection.createStatement();
int rows=statement.executeUpdate(sql3);
System.out.println(rows>0?"成功":"失败");
statement.close();
connection.close();
}
单独写一个配置文件Mysql.properties,从配置文件中加载数据
@Test
public void connection4() throws IOException, SQLException {
Properties properties=new Properties();
properties.load(new FileInputStream("src\\Mysql.properties"));
String user=properties.getProperty("user");
String password=properties.getProperty("password");
String driver=properties.getProperty("driver");
String url=properties.getProperty("url");
Connection connection=DriverManager.getConnection(url,user,password );
String sql5="insert into actor values(null,'刘大海','男')";
Statement statement=connection.createStatement();
int rows=statement.executeUpdate(sql5);
System.out.println(rows>0?"成功":"失败");
statement.close();
connection.close();
}