JDBC
1、JDBC的核心API
|-Driver接口: 数据库驱动程序的接口,所有具体数据库厂商需要的驱动程序需要实现次接口。
Connection connect(String url, Properties info) 用于获取数据库连接
|-Connection接口:与具体的数据库的连接对象。
Statement createStatement() 创建一个静态sql语句对象
PreparedStatement prepareStatement(String sql) 创建预编译的sql语句对象
CallableStatement prepareCall(String sql) 创建存储过程的sql语句对象
|-Statement接口:用于执行静态 SQL 语句
int executeUpdate(String sql) 执行更新操作的sql语句 (create/alter/drop/insert/update/delete)
ResultSet executeQuery(String sql) 执行查询操作的sql语句 (select)
|- PreparedStatement接口:用于执行预编译的 SQL 语句(是Statement的子接口)
int executeUpdate() 执行更新操作的sql语句
ResultSet executeQuery() 执行查询操作的sql语句
|-CallableStatement接口:用于执行 SQL 存储过程的接口(是PreparedStatement的子 接口)
ResultSet executeQuery() 执行存储过程的sql语句
|- ResultSet接口:结果集对象。 存储所有数据库查询的结果,用该对象进行数据遍历。
boolean next() : 把光标移动到下一行。如果下一行有数据,返回true,如果没有下一行数据,返回false。
getXXX(列索引|列字段名称): 获取字段的数据
2、Statement对象
/**
* 通过jdbc执行DDL语句
* @author APPle
*
*/
public class Demo1 {
//数据库的连接的URL
private static String url = "jdbc:mysql://localhost:3306/day17";
//数据库用户名
private static String user = "root";
//数据库密码
private static String password = "root";
public static void main(String[] args){
Connection conn = null;
Statement stmt = null;
try {
//1.注册驱动程序
Class.forName("com.mysql.jdbc.Driver");
//2.从驱动程序管理类获取连接
conn = DriverManager.getConnection(url, user, password);
//3.通过Connection对象获取Statement对象
stmt = conn.createStatement();
//4.准备sql语句
String sql = "CREATE TABLE student(id INT PRIMARY KEY AUTO_INCREMENT,NAME VARCHAR(20),gender VARCHAR(2))";
//5.执行sql语句,返回结果
int count = stmt.executeUpdate(sql);
System.out.println("影响了"+count+"行");
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally{
//6.关闭资源(先关闭statement,再关闭connection)
if(stmt!=null)
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
if(conn!=null)
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
}
3、PreparedStatement对象
/**
* 使用PreparedStatement执行sql语句
* @author APPle
*
*/
public class Demo1 {
/**
* 插入操作
*/
@Test
public void test1(){
Connection conn = null;
PreparedStatement stmt = null;
try{
//获取连接
conn = JdbcUtil.getConnection();
String sql = "INSERT INTO student(NAME,gender) VALUES(?,?)"; //预编译sql:使用?号代替参数值。一个?号代表一个参数值
//创建PreparedStatement对象,执行预编译的sql语句
stmt = conn.prepareStatement(sql);
//设置参数
/**
* 参数一: 参数位置。从1开始
* 参数二: 参数实际值
* 注意: 所有参数必须要赋值
*/
stmt.setString(1, "rose");
stmt.setString(2, "女");
//发送参数,执行sql语句
int count = stmt.executeUpdate();
System.out.println(count);
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException(e);
}finally{
//关闭资源
JdbcUtil.close(conn, stmt, null);
}
}
/**
* 修改操作
*/
@Test
public void test2(){
Connection conn = null;
PreparedStatement stmt = null;
//声明变量
String name = "jacky";
int id = 8;
try{
//获取连接
conn = JdbcUtil.getConnection();
String sql = "UPDATE student SET NAME=? WHERE id=?"; //预编译sql:使用?号代替参数值。一个?号代表一个参数值
//创建PreparedStatement对象,执行预编译的sql语句
stmt = conn.prepareStatement(sql);
//设置参数
stmt.setString(1,name);
stmt.setInt(2, id);
//发送参数,执行sql语句
int count = stmt.executeUpdate();
System.out.println(count);
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException(e);
}finally{
//关闭资源
JdbcUtil.close(conn, stmt, null);
}
}
/**
* 删除操作
*/
@Test
public void test3(){
Connection conn = null;
PreparedStatement stmt = null;
//声明变量
int id = 8;
try{
//获取连接
conn = JdbcUtil.getConnection();
String sql = "DELETE FROM student WHERE id=?"; //预编译sql:使用?号代替参数值。一个?号代表一个参数值
//创建PreparedStatement对象,执行预编译的sql语句
stmt = conn.prepareStatement(sql);
//设置参数
//任何类型都可以使用setObject进行赋值
stmt.setObject(1, id);
//发送参数,执行sql语句
int count = stmt.executeUpdate();
System.out.println(count);
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException(e);
}finally{
//关闭资源
JdbcUtil.close(conn, stmt, null);
}
}
/**
* 查询操作
*/
@Test
public void test4(){
Connection conn = null;
PreparedStatement stmt = null;
//声明变量
String name = "张%";
try{
//获取连接
conn = JdbcUtil.getConnection();
String sql = "SELECT * FROM student WHERE NAME LIKE ?";
//创建PreparedStatement,预编译sql语句
stmt = conn.prepareStatement(sql);
//设置参数
stmt.setObject(1, name);
//发送参数,执行sql,返回结果集
ResultSet rs = stmt.executeQuery();
//遍历结果集
while(rs.next()){
int id = rs.getInt("id");
String nameStr = rs.getString("name");
String gender = rs.getString("gender");
System.out.println(id+"\t"+nameStr+"\t"+gender+"\t");
}
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException(e);
}finally{
//关闭资源
JdbcUtil.close(conn, stmt, null);
}
}
}
4、CallableStatement对象
/**
* 执行带有输入参数存储过程
*/
@Test
public void test1(){
Connection conn = null;
CallableStatement stmt = null;
ResultSet rs = null;
try{
//获取连接
conn = JdbcUtil.getConnection();
//创建CallableStatement对象
String sql = "CALL pro_findById(?)";//预编译sql、可以带?号
//执行预编译的sql
stmt = conn.prepareCall(sql);
//设置参数
stmt.setInt(1, 4);
//发送参数,执行sql,返回结果
rs = stmt.executeQuery();// 注意: 执行存储过程必须使用exeuteQuery!!!!
//遍历结果
while(rs.next()){
int id = rs.getInt("id");
String name = rs.getString("name");
String gender = rs.getString("gender");
System.out.println(id+"\t"+name+"\t"+gender+"\t");
}
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException(e);
}finally{
//关闭资源
JdbcUtil.close(conn, stmt, rs);
}
}