一.JDBC简介
1.简介:java DataBase Connectivity java数据库连接工具,用于执行sql语句的java
2.jdbc api支持用于数据库访问的两层和三层处理模型,但是通常由两层构成 jdbc和jdbc驱动
3.核心组件: DriveManger:管理数据库驱动程序列表,
Driver 处理与数据库服务器的通信
Connection 用于连接数据库的所有方法,连接对象表示通信上下文,数据库的所有通信仅通过连接对象
Staement 将sql语句提交到数据库
resultset 使用statement对象执行sql查询后,这些对象保存从数据库检索的数据,作为一个迭代器允许移动数据。
sqlException 处理数据库应用程序中发生的任何异常
二.CRUD语法 create read update delete
三.jdbc的使用
1.导入jdbc驱动包
2.注册驱动程序
//1.class.forname注册驱动
try {
Class.forName("com.mysql.jdbc.Driver");
}
catch(ClassNotFoundException ex) {
System.out.println("Error: unable to load driver class!");
System.exit(1);
}
//2.静态drivermanger.registerdriver()方法
try {
Driver myDriver = new com.mysql.jdbc.Driver();
DriverManager.registerDriver( myDriver );
}
catch(ClassNotFoundException ex) {
System.out.println("Error: unable to load driver class!");
System.exit(1);
}
3.创建连接 使用drivermanger.getconnect()表示与数据库的物理连接
4.执行查询 需要使用类型为statement的对象来构建和提交sql语句到数据库
5.提取数据 resultset.getxxx检索数据
6.释放资源
查询数据:代码
public class JdbcDemo1 {
public static void main(String[] args) {
Connection connection=null;
Statement statement=null;
ResultSet rs=null;
//1.导入jar包
//2.注册驱动
try {
Class.forName("com.mysql.jdbc.Driver");
//3.获取连接
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/school", "root", "0810");
//4.创建命令语句对象
statement = connection.createStatement();
//执行命令语句
rs = statement.executeQuery("select ename,empno from emp");
//5.处理结果
while (rs.next()){
int empno=rs.getInt(1);
String ename=rs.getString("ename");
System.out.println(empno+"..."+ename);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if (rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (rs!=null){
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (rs!=null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
添加数据
public class JdbcDemo2 {
public static void main(String[] args) {
Connection connection=null;
Statement stat=null;
//2 注册驱动
try {
Class.forName("com.mysql.jdbc.Driver");
//3打开连接
connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/school", "root", "root");
//4创建命令并执行
stat=connection.createStatement();
int result=stat.executeUpdate("insert into dept(deptno,dname,loc) values(80,'市场部','天津')");
if(result>0){
System.out.println("添加成功");
}else{
System.out.println("添加失败");
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if(stat!=null){
try {
stat.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(connection!=null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
SQL注入问题
输入的密码欺骗服务器,输入一个为true的等式就可以骗服务器登录进设计的网页从而骗取获得进入许可
preparedstatement 接口扩展了statement接口,进行预编译 效率高安全避免了sql注入的问题
把statement转变成preparedstatement
抽取数据库工具类
DbUtils类功能:1.注册驱动 2.获取连接 3.释放资源 4.执行命令
jdbc批处理
一.步骤:
1 注册驱动获取连接
2 使用createStatement()方法创建Statement对象。
3 使用setAutoCommit()将auto-commit设置为false 。(可选)
4 使用addBatch()方法在创建的语句对象上添加您喜欢的SQL语句到批处理中。
5 在创建的语句对象上使用executeBatch()方法执行所有SQL语句。
6 使用commit()方法提交所有更改。(可选)
7 释放资源
jdbc操作二进制
preparedstatement对象可以使用输入和输出流来提供参数数据,可以将整个文件放入可以保存大值的数据库列,比如2text和blob
将图片放到数据库中
public class Demo4 {
public static void main(String[] args) throws Exception{
//write();
read();
}
public static void write() throws Exception{
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/school";
Connection conn=DriverManager.getConnection(url, "root", "root");
PreparedStatement pstat=conn.prepareStatement("insert into bigdata2(id,img) values(?,?)");
FileInputStream fis=new FileInputStream("d:\\图片\\003.jpg");
pstat.setInt(1, 1);
pstat.setBinaryStream(2, fis);
int count=pstat.executeUpdate();
System.out.println(count);
pstat.close();
conn.close();
}
public static void read() throws Exception{
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/school";
Connection conn=DriverManager.getConnection(url, "root", "root");
PreparedStatement pstat=conn.prepareStatement("select * from bigdata2 where id=1");
ResultSet rs=pstat.executeQuery();
if(rs.next()) {
int id=rs.getInt("id");
System.out.println(id);
//处理图片
InputStream is=rs.getBinaryStream("img");
FileOutputStream fos=new FileOutputStream("d:\\haha.jpg");
byte[] buf=new byte[1024];
int len=0;
while((len=is.read(buf))!=-1) {
fos.write(buf,0,len);
}
fos.close();
is.close();
}
rs.close();
pstat.close();
conn.close();
System.out.println("读取完成");
}
}
数据库事务
事务:要么同时执行成功要么同时执行失败
事务的四大特点:原子性,一致性 隔离性 持久性
隔离级别
read uncommit 读取未提交内容
read committed 读取提交内容
repeatable read 可重读
serializable 可串行化
卖方的隔离级别需要高于买家,买家不确认直接回滚会使数据库的信息返回原来,不会修改完成