Mysql jdbc
jdbc概念
1, 什么是jdbc
2,jdbc操作API
3, jdbc 实现步骤
jdbc 实现增删改查操作
1,jdbc 概念
java 里面针对操作数据库 提供一套规范 好比是接口 比如操作mysql数据库,mysql提供这些接口实现类
jdbc 是java 操作数据库一套规范 由具体数据库提供这些规范实现,以jar包形式提供
2.jdbc操作API
Driver Manager: 注册驱动 ,创建连接
Connection :数据库连接对象
Statement , PreparedStatement CallableStatment :执行sql语句对象
Result :查询知后的结果
3jdbc操作步骤(固定)
第一步加载数据库的驱动
1,把数据库实现jar包导入到项目中
2.通过反射代码加载驱动对象
第二部 创建数据库连接
地三步编写sql语句 执行sql语句
第四步 如果执行查询操作,得到查询知后结果集, 遍历结果集内容
第五步 关闭资源
4.Jdbc 实现对数据库crud操作
1, 第一步,加载数据库驱动
1,把数据实现jar包导入到项目中
根目录下创建文件夹名字lib
赋值mysqljar包导入过来
2, 通过反射代码加载驱动对象
@Test
public void testDelete() throws Exception{
Connection connection =null;
Statement statement1 =null;
Class.forName(“com.mysql.jdbc.Driver”);
connection = DriverManager.getConnection(“jdbc:mysql://localhost/db2”, “root”, “123456”);
String sql= “delete from dept where did=4”;
statement1 = connection.createStatement();
int row = statement1.executeUpdate(sql);
System.out.println(row);
statement1.close();
connection.close();
}
//修改操作
@Test
public void testUpdate() throws Exception{
Connection connection =null;
Statement statement1 =null;
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost/db2", "root", "123456");
String sql= "update dept set dname ='对外交流部' where did=4";
statement1 = connection.createStatement();
int row = statement1.executeUpdate(sql);
System.out.println(row);
statement1.close();
connection.close();
}
//增加操作
@Test
public void testInsert() throws Exception{
Connection connection =null;
Statement statement1 =null;
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost/db2", "root", "123456");
String sql = "insert into dept values(4,'外交部')";
statement1 = connection.createStatement();
int row = statement1.executeUpdate(sql);
System.out.println(row);
statement1.close();
connection.close();
}
//查询操作
@Test
public void testSelect() throws SQLException {
// 1, 加载数据库驱动
Connection conn =null;
Statement statement =null;
ResultSet rs =null;
try {
Class.forName(“com.mysql.jdbc.Driver”);
// 2. 创建 数据库连接
//第一个参数 : 数据库地址 url
//用户名和密码
conn = DriverManager.getConnection(“jdbc:mysql://localhost:3306/db2”,“root”,“123456”);
// 3. 编写SQL语句创建Statement 执行SQL语句
String sql = “select did,dname from dept”;
statement = conn.createStatement();
rs = statement.executeQuery(sql);
//4. 查询返回结果集 ,遍历显示内容
while(rs.next()){
int did = rs.getInt(“did”);
String dname = rs.getString(“dname”);
System.out.println(“did:”+did);
System.out.println("dname: "+dname);
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}finally {
//5. 关闭资源
rs.close();
statement.close();
conn.close();
}
}
statement 对象
1.statement 对象主要作用执行sql语句的
2. Statement 对象的缺点
sql 语句拼接
@Test
public void test01() throws Exception{
Class.forName(“com.mysql.jdbc.Driver”);
Class.forName(“com.mysql.jdbc.Driver”);
Connection conn= null;
Statement statement= null;
ResultSet rs =null;
conn= DriverManager.getConnection(“jdbc:mysql://localhost/db2”,“root”,“123456”);
String dname ="安保部";
String sql ="select * from dept where dname= '"+dname+"'" ;
statement = conn.createStatement();
rs = statement.executeQuery(sql);
while (rs.next()){
String did = rs.getString("did");
String dnameValue = rs.getString("dname");
System.out.println(did+"::"+dnameValue);
}
rs.close();
statement.close();
conn.close();
}
数据库连接池
1.什么是数据库连接池
预先创建好一些连接放到数据库连接池中
操作 -》 从连接池取出来连接 连接池
操作完成 把链接放回到连接池
2.使用连接池,可以自己创建连接池,实现DataSource 接口就可以了 但是实际操作中,一般不去自己实现,使开源连接池进行使用
3. 常用的开源连接池:DBCP C3P0 Druid
4. 使用德鲁伊连接 引入jar包
- 导入jar包
2.编写德鲁伊连接池的代码
1, 创建properties 类型配置文件,设置数据库连接池信息
url=jdbc:mysql://localhost:3306/db2
username=root
password=123456
driverClassName=com.mysql.jdbc.Driver
initialSize=10
maxActive=20
maxWait=1000