10.2JDBC程序例子
import java.sql.*;
public class TestJdbc {
public static void main(String[] args) {
Connection con=null;
Statement statement=null;
ResultSet set=null;
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/xialiao";
String username="root";
String password="11111111";
//连接数据库对象
con= DriverManager.getConnection(url,username,password);
//执行sql对象
statement =con.createStatement();
String sql="select * from user1";
//返回结果集
set=statement.executeQuery(sql);
while(set.next()){
System.out.println("username="+set.getString("username"));
}
} catch (Exception e) {
e.printStackTrace();
}
finally {
try {
set.close();
statement.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
步骤流程
1、加载驱动
2、连接数据库DriverManager
3、获得执行sql对象statement
4、获得返回的结果集
5、释放连接
加载驱动
Class.forName(“com.mysql.jdbc.Driver”);
URL
String url=“jdbc:mysql://localhost:3306/xialiao”;
Statement执行sql的对象,PrepareStatement执行sql对象
statement.executeQuery()查询操作返回resultset
statement.execute()执行任何sql操作,比起其他方法较慢
statement.executeUpdate()更新、插入、删除都使用这个方法
ResultSet 查询的结果集:封装了所有的查询结果
获得指定的数据类型
resultset.getObject(字段名或列序号);在不知道列类型的情况下使用
知道列类型可以使用指定列类型
resultset.getString(字段名或列序号)
resultset.getInt(字段名或列序号)
遍历,指针
resultset.beforeFirst()移动到最前面
resultset.afterLast()移动到最后面
resultset.next()移动到下一个数据
resultset.previous()移动到前一行
resultset.absolute(row)移动到指定行
释放资源
set.close();
statement.close();
con.close();
10.3statement对象
jdbc中的statement对象用于向数据库发送sql语法,想完成对数据库的增删改查,只需要通过这个对象向数据库发送增删改查语句即可
statement对象的executeUpdate方法,用于向数据库发送增删改的sql语句,executeUpdate执行完成后,会返回一个整数(相关sql语句导致数据库几行数据发生了变化)
statement对象的executeQuery方法用于向数据库发送查询语句,executeQuery方法返回代表查询结果的ResultSet对象
curd代表创建(Create)、更新(Update)、读取(Retrieve)和删除(Delete)操作。
curd操作–create
使用executeUpdate(String sql)方法完成数据添加操作
Statement st =con.createStatement();
String sql="insert into user(....) values(....)";
int num=st.executeUpdate(sql);
if(num>0)
{
System.out.println("插入成功");
}
curd操作–delete
使用executeUpdate(String sql)方法完成数据删除操作
Statement st =con.createStatement();
String sql="delete from user where id=1";
int num=st.executeUpdate(sql);
if(num>0)
{
System.out.println("删除成功");
}
curd操作–修改操作
使用executeUpdate(String sql)方法完成数据修改
Statement st =con.createStatement();
String sql="update user set username='' where username=''";
int num=st.executeUpdate(sql);
if(num>0)
{
System.out.println("删除成功");
}
curd操作-读取操作read
使用executeQuery(String sql)方法完成数据查询操作
statement st=con.createStatement();
String sql="select * from user where id=1";
ResultSet rs=st.executeQuery(sql);
where(rs.next()){}
SQL注入问题
sql存在漏洞,会被攻击导致数据泄露,sql会被拼接or
10.4PreparedStatement对象
PreparedStatement可以防止sql注入,效率更高
PreparedStatement防止注入的本质是,把传递进来的参数当作字符
import java.sql.*;
public class TestInsert {
public static void main(String[] args) {
Connection con=null;
PreparedStatement statement=null;
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/xialiao";
String username="root";
String password="11111111";
//连接数据库对象
con= DriverManager.getConnection(url,username,password);
//执行sql对象
//区别 使用?占位符代替参数
String sql="insert into user1(username,password,nickname) values(?,?,?) ";
//预编译sql
statement=con.prepareStatement(sql);
//为每个参数赋值
statement.setString(1,"test");
statement.setString(2,"000");
statement.setString(3,"hhh");
int i=statement.executeUpdate();
if(i>0){
System.out.println("插入成功");
}
} catch (Exception e) {
e.printStackTrace();
}
finally {
try {
statement.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
10.5事务
//关闭数据库自动提交,自动会开启事务
con.setAutoCommit(false);
String sql="update account set money=money-100 where name=a";
statement=con.prepareStatement(sql);
statement.executeUpdate();
String sql1="update account set money=money+100 where name=b";
statement=con.prepareStatement(sql1);
statement.executeUpdate();
//业务完成,提交事务
con.commit();
代码实现
1、开启事务con.setAutoCommit(false);
2、一组业务执行完毕,提交事务
3、可以在catch语句中显式地定义回滚语句,但默认失败就回滚
10.6数据库连接池
数据库连接—执行完毕—释放
连接—释放 十分浪费系统资源
池化技术:预先准备一些连接资源,需要时就调用,不需要时就放回连接池中
最小连接数
最大连接数
连接超时
连接池实现接口DataSource
开源数据源实现
DBCP
C3P0
Druid:阿里巴巴
DBCP
需要用到的jar包:commons-dbcp-1.4、commons-pool-1.6