文章目录
一、JDBC编程步骤
(1)加载驱动Class.forName()
在应用程序中进行数据库连接,调用JDBC接口,首先要将特定厂商的JDBC驱动加载到系统内存中,然后供系统使用。驱动本质上还是一个class
,将驱动加载到内存和加载普通的class原理是一样的,如加载MySQL的驱动是下面的方式:
//加载MySQL数据库驱动到内存
Class.forName("com.mysql.jdbc.Driver");
(2)获取连接DriverManager.getConnection(url, user, password);
加载驱动后会创建一个驱动Driver的实例,放入DriverManager中
,供DriverManager使用。
可以使用DriverManager的getConnection方法
获取连接:
String url ="jdbc:mysql://localhost:3306/mybase";
String user = "root";
String password = "Hudie";
Connection conn = DriverManager.getConnection(url, user, password);
(3)创建Statement或preparedStatement
创建Statement对象:
Statement stm = conn.createStatement();
创建preparedStatement对象:
String sql = "select * from emp where job=?";
pstm = conn.prepareStatement(sql);//创建sql半成品
pstm.setString(1,"clerk");//参数绑定
①创建sql半成品:通过连接获得PreparedStatement对象,创建时将SQL语句中取值发生变化的部分用占位符?
代替。
②参数绑定,即为?
赋值(set[type](1,2)方法
)第一个参数代表?
位置,从1开始;第二个参数是具体取值。
(4)执行sql语句
如果是Statement对象的查询,使用rs = stm.executeQuery(sql);
如果是preparedStatement对象的查询,使用rs = pstm.executeQuery();
如果有很多sql语句需要执行,就要使用批处理。
(5)处理结果集(如果是查询语句)
rs指针开始时在表头(所有记录的前面),nextInt()方法可以后移一位,如果移动到最后一条记录的后面一行,next()方法返回false。get(int index)方法来根据下标获得值,close()方法释放资源。
封装结果集一般使用while循环,如下面的代码就是将表中记录全部输出:
while(rs.next()){
int empno = rs.getInt(1);
String ename = rs.getString(2);
String job = rs.getString(3);
int mgr = rs.getInt(4);
Date hiredate = rs.getDate(5);
double sal = rs.getDouble(6);
double comm = rs.getDouble(7);
int deptno = rs.getInt(8);
System.out.println(empno+" "+ename+" "+job+" "+mgr+" "+hiredate+" "+sal+" "+comm+" "+deptno);
}
(6)关闭连接
注意:资源的关闭顺序和创建顺序相反,可以类比栈结构。
try {
rs.close();//关闭结果集资源
pstm.close();//关闭pstm对象资源
conn.close();//关闭连接资源
} catch (SQLException e) {
e.printStackTrace();
}
二、使用JDBC创建Statement连接数据库
package jdbc;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* 类说明:
* JDBC创建statement对象连接数据库
* @author qianliangguo
*/
public class testStatement {
public static void main(String[] args){
Connection conn = null;//定义为全局,就是为了在关闭连接时,在finally中访问到
Statement stm = null;
ResultSet rs = null;
try {
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2.获取连接
String url = "jdbc:mysql://localhost:3306/mybase";
String user = "root";
String password = "Hudie";
conn = DriverManager.getConnection(url, user, password);
//3.创建statement对象
stm = conn.createStatement();
//4.执行sql语句
/*
* 操作数据库(增删改)的用stm.executeUpdate(),返回int类型的影响行数
* String sql ="insert into emp(ename,job,deptno) values('英琪','manager',10)";
* int line = stm.executeUpdate(sql);
*/
//查询返回的类型是ResultSet结果集
String sql = "select * from emp";
rs = stm.executeQuery(sql);
//5.处理结果集
while(rs.next()){
int empno = rs.getInt(1);
String ename = rs.getString(2);
String job = rs.getString(3);
int mgr = rs.getInt(4);
Date hiredate = rs.getDate(5);
double sal = rs.getDouble(6);
double comm = rs.getDouble(7);
int deptno = rs.getInt(8);
System.out.println(empno+" "+ename+" "+job+" "+mgr+" "+hiredate+" "+sal+" "+comm+" "+deptno);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
//关闭连接
rs.close();
stm.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
三、使用JDBC创建preparedStatement连接数据库
package jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
/**
* 类说明:
* JDBC创建PreparedStatement对象连接数据库
* @author qianliangguo
*/
public class testPreparedStatement {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement pstm = null;
ResultSet rs = null;
try {
// 1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
// 2.获取连接
String url = "jdbc:mysql://localhost:3306/mybase?useUnicode=true&characterEncoding=utf-8";
String user = "root";
String password = "Hudie";
conn = DriverManager.getConnection(url, user, password);
// 3.创建PreparedStatement,预编译sql语句
String sql = "select * from emp where job=?";
pstm = conn.prepareStatement(sql);//创建sql半成品
pstm.setString(1,"clerk");//参数绑定
//4.执行sql语句
rs = pstm.executeQuery();
//5.处理结果集
while(rs.next()){
int empno = rs.getInt("empno");
String ename = rs.getString("ename");
String job = rs.getString(3);
int mgr = rs.getInt("mgr");
Date hiredate = rs.getDate(5);
double sal = rs.getDouble("sal");
double comm = rs.getDouble("commit");
int deptno = rs.getInt(8);
System.out.println(empno+" "+ename+" "+job+" "+mgr+" "+hiredate+" "+sal+" "+comm+" "+deptno);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 6.关闭连接/释放资源
try {
rs.close();
pstm.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
四、Statement和preparedStatement比较
statement
的执行过程:
创建stm对象—>进行编译—>执行(执行一条sql语句10秒).PreparedStatement
的执行过程:
创建pstm对象(sql半成品)—>设置值—>执行发送数据(执行一条sql语句需要12秒,但是设置sql和值只需要2秒)
Statement | PreparedStatement | |
---|---|---|
关系 | 父接口 | 子接口 |
语法 | stm=conn.createStatement(); stm.executeUpdate(sql); | String sql ="…?.."; pstm=conn.prepareStatement(sql); pstm.set***(index,value); pstm.executeUpdate(); |
安全 | 存在安全注入隐患 | 解决了SQL注入问题 |
效率 | 执行异构sql效率较高 | 执行同构sql效率较高 |