定义:使用java代码发送sql语句,就是jdbc
步骤:
jdbc协议:数据库子协议://ip:端口/数据库名字
url = "jdbc:mysql://localhost:3306/day1";
jdbc体系:
核心API:
java.sql.*
javax.sql.*
|-Drive接口:java驱动程序接口,所有的具体的数据库厂商需要实现此接口。
|-connect(url,properties):连接数据库的方法
url:连接数据库的URL
URL语法:jdbc协议:数据库子协议://主机:端口/数据库
user:数据库的用户名
password:数据库的用户名密码
|-DriverManager类:驱动管理器类,用于管理所有注册的驱动程序
|-registerDriver(driver):注册驱动类对象
|-Connection getConnection(url,user,password):获取连接对象
|-Statement接口:用于执行静态的sql语句:
|-int executeUpdate(String sql):执行静态的更新sql语句(DDL,DML)
|-ResultSet executeQuery(String sql) :执行静态的查询语句;
|-PreparedStatement接口:用于执行预编译sql语句:
|-int executeUpdate(); 执行预编译的sql语句
|-int ResultSet executeQuery(); 执行预编译的查询语句;
|-CallableStatement接口:用于执行存储过程的sql语句(call xx)
|-ResultSet executeQuery();调用存储过程的方法
实例:
执行DDL语句:
1、驱动注册程序
Class.forName("com.oracle.jdbc.Driver");
2、获取连接对象
Connection conn = DriverManager.getConnection(url,user,password);
3、创建Statement对象
Statement stmt = conn.createStatement();
4、准备sql
String sql = "create table student(id ……";
5、发送sql语句
int count = stmt.executeUpdate(sql);
6、输出
System.out.println("影响了" +count+ "行");
7、关闭连接,finally 里面,后打开的先关闭
if(stmt != null) stmt.close();
if(conn != null) conn.close();
预编译与静态sql的区别
1、语法不一致,PreparedStatement可以使用预编译的sql,而statement只能使用静态的sql
2、执行效率不一致 ,PreparedStatement 可以使用sql缓存区,效率不同
3、安全性:PrepareStatement可以有效的防止sql注入,二statement不可以
CallableStatement:
注意:所有的调用存储过程的sql 都要使用executeQuery()方法;
. 在java项目中表示java bin的目录,在web项目中,表示tomcat的bin目录下
使用类路径方式读取
/ :表示classpath的根目录
在java项目下,classpath的根目录下
在web项目下,classpath的根目录从WEB-INF/classes目录开始
实例:
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
importjava.io.InputStream;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importjava.sql.Statement;
importjava.util.Properties;
/**
* jdbc工具类
* @author APPle
*
*/
publicclass JdbcUtil {
privatestatic String url = null;
privatestatic String user = null;
privatestatic String password = null;
privatestatic String driverClass = null;
/**
* 静态代码块中(只加载一次)
*/
static{
try{
//读取db.properties文件
Propertiesprops = new Properties();
/**
* . 代表java命令运行的目录
* 在java项目下,. java命令的运行目录从项目的根目录开始
* 在web项目下, . java命令的而运行目录从tomcat/bin目录开始
* 所以不能使用点.
*/
//FileInputStreamin = new FileInputStream("./src/db.properties");
/**
* 使用类路径的读取方式
* / : 斜杠表示classpath的根目录
* 在java项目下,classpath的根目录从bin目录开始
* 在web项目下,classpath的根目录从WEB-INF/classes目录开始
*/
InputStreamin = JdbcUtil.class.getResourceAsStream("/db.properties");
//加载文件
props.load(in);
//读取信息
url= props.getProperty("url");
user= props.getProperty("user");
password= props.getProperty("password");
driverClass= props.getProperty("driverClass");
//注册驱动程序
Class.forName(driverClass);
}catch (Exception e) {
e.printStackTrace();
System.out.println("驱程程序注册出错");
}
}
/**
* 抽取获取连接对象的方法
*/
publicstatic Connection getConnection(){
try{
Connectionconn = DriverManager.getConnection(url, user, password);
returnconn;
}catch (SQLException e) {
e.printStackTrace();
thrownew RuntimeException(e);
}
}
/**
* 释放资源的方法
*/
publicstatic void close(Connection conn,Statement stmt){
if(stmt!=null){
try{
stmt.close();
}catch (SQLException e) {
e.printStackTrace();
thrownew RuntimeException(e);
}
}
if(conn!=null){
try{
conn.close();
}catch (SQLException e) {
e.printStackTrace();
thrownew RuntimeException(e);
}
}
}
publicstatic void close(Connection conn,Statement stmt,ResultSet rs){
if(rs!=null)
try{
rs.close();
}catch (SQLException e1) {
e1.printStackTrace();
thrownew RuntimeException(e1);
}
if(stmt!=null){
try{
stmt.close();
}catch (SQLException e) {
e.printStackTrace();
thrownew RuntimeException(e);
}
}
if(conn!=null){
try{
conn.close();
}catch (SQLException e) {
e.printStackTrace();
thrownew RuntimeException(e);
}
}
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
importjava.sql.CallableStatement;
importjava.sql.Connection;
importjava.sql.ResultSet;
importorg.junit.Test;
/**
*使用CablleStatement调用存储过程
* @author APPle
*
*/
publicclass Demo1 {
/**
* 调用带有输入参数的存储过程
* CALL pro_findById(4);
*/
@Test
publicvoid test1(){
Connectionconn = null;
CallableStatementstmt = null;
ResultSetrs = null;
try{
//获取连接
conn= JdbcUtil.getConnection();
//准备sql
String sql = "CALLpro_findById(?)"; //可以执行预编译的sql
//预编译
stmt= conn.prepareCall(sql);
//设置输入参数
stmt.setInt(1,6);
//发送参数
rs = stmt.executeQuery(); //注意: 所有调用存储过程的sql语句都是使用executeQuery方法执行!!!
//遍历结果
while(rs.next()){
intid = rs.getInt("id");
Stringname = rs.getString("name");
Stringgender = rs.getString("gender");
System.out.println(id+","+name+","+gender);
}
}catch (Exception e) {
e.printStackTrace();
thrownew RuntimeException(e);
}finally {
JdbcUtil.close(conn,stmt ,rs);
}
}
/**
* 执行带有输出参数的存储过程
* CALL pro_findById2(5,@NAME);
*/
@Test
publicvoid test2(){
Connectionconn = null;
CallableStatementstmt = null;
ResultSetrs = null;
try{
//获取连接
conn= JdbcUtil.getConnection();
//准备sql
String sql = "CALL pro_findById2(?,?)";//第一个?是输入参数,第二个?是输出参数
//预编译
stmt= conn.prepareCall(sql);
//设置输入参数
stmt.setInt(1,6);
//设置输出参数(注册输出参数)
/**
*参数一: 参数位置
*参数二: 存储过程中的输出参数的jdbc类型 VARCHAR(20)
*/
stmt.registerOutParameter(2,java.sql.Types.VARCHAR);
//发送参数,执行
stmt.executeQuery(); //结果不是返回到结果集中,而是返回到输出参数中
//得到输出参数的值
/**
*索引值: 预编译sql中的输出参数的位置
*/
String result = stmt.getString(2); //getXX方法专门用于获取存储过程中的输出参数
System.out.println(result);
}catch (Exception e) {
e.printStackTrace();
thrownew RuntimeException(e);
}finally {
JdbcUtil.close(conn,stmt ,rs);
}
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
importjava.sql.Connection;
importjava.sql.PreparedStatement;
importjava.sql.ResultSet;
importorg.junit.Test;
/**
*PreparedStatement執行sql語句
* @author APPle
*
*/
publicclass Demo1 {
/**
* 增加
*/
@Test
publicvoid testInsert() {
Connectionconn = null;
PreparedStatementstmt = null;
try{
//1.获取连接
conn= JdbcUtil.getConnection();
//2.准备预编译的sql
String sql = "INSERT INTO student(NAME,gender)VALUES(?,?)"; //?表示一个参数的占位符
//3.执行预编译sql语句(检查语法)
stmt= conn.prepareStatement(sql);
//4.设置参数值
/**
*参数一: 参数位置 从1开始
*/
stmt.setString(1, "李四");
stmt.setString(2, "男");
//5.发送参数,执行sql
intcount = stmt.executeUpdate();
System.out.println("影响了"+count+"行");
}catch (Exception e) {
e.printStackTrace();
thrownew RuntimeException(e);
}finally {
JdbcUtil.close(conn,stmt);
}
}
/**
* 修改
*/
@Test
publicvoid testUpdate() {
Connectionconn = null;
PreparedStatementstmt = null;
try{
//1.获取连接
conn= JdbcUtil.getConnection();
//2.准备预编译的sql
String sql = "UPDATE student SET NAME=? WHEREid=?"; //?表示一个参数的占位符
//3.执行预编译sql语句(检查语法)
stmt= conn.prepareStatement(sql);
//4.设置参数值
/**
*参数一: 参数位置 从1开始
*/
stmt.setString(1, "王五");
stmt.setInt(2,9);
//5.发送参数,执行sql
intcount = stmt.executeUpdate();
System.out.println("影响了"+count+"行");
}catch (Exception e) {
e.printStackTrace();
thrownew RuntimeException(e);
}finally {
JdbcUtil.close(conn,stmt);
}
}
/**
* 删除
*/
@Test
publicvoid testDelete() {
Connectionconn = null;
PreparedStatementstmt = null;
try{
//1.获取连接
conn= JdbcUtil.getConnection();
//2.准备预编译的sql
String sql = "DELETE FROM student WHEREid=?"; //?表示一个参数的占位符
//3.执行预编译sql语句(检查语法)
stmt= conn.prepareStatement(sql);
//4.设置参数值
/**
*参数一: 参数位置 从1开始
*/
stmt.setInt(1,9);
//5.发送参数,执行sql
intcount = stmt.executeUpdate();
System.out.println("影响了"+count+"行");
}catch (Exception e) {
e.printStackTrace();
thrownew RuntimeException(e);
}finally {
JdbcUtil.close(conn,stmt);
}
}
/**
* 查询
*/
@Test
publicvoid testQuery() {
Connectionconn = null;
PreparedStatementstmt = null;
ResultSetrs = null;
try{
//1.获取连接
conn= JdbcUtil.getConnection();
//2.准备预编译的sql
Stringsql = "SELECT * FROM student";
//3.预编译
stmt= conn.prepareStatement(sql);
//4.执行sql
rs= stmt.executeQuery();
//5.遍历rs
while(rs.next()){
intid = rs.getInt("id");
Stringname = rs.getString("name");
Stringgender = rs.getString("gender");
System.out.println(id+","+name+","+gender);
}
}catch (Exception e) {
e.printStackTrace();
thrownew RuntimeException(e);
}finally {
//关闭资源
JdbcUtil.close(conn,stmt,rs);
}
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.SQLException;
importjava.sql.Statement;
importorg.junit.Test;
/**
*使用Statement对象执行静态sql语句
* @author APPle
*
*/
publicclass Demo1 {
privateString url = "jdbc:mysql://localhost:3306/day17";
privateString user = "root";
privateString password = "root";
/**
*执行DDL语句(创建表)
*/
@Test
publicvoid test1(){
Statementstmt = null;
Connectionconn = null;
try{
//1.驱动注册程序
Class.forName("com.mysql.jdbc.Driver");
//2.获取连接对象
conn= DriverManager.getConnection(url, user, password);
//3.创建Statement
stmt= conn.createStatement();
//4.准备sql
Stringsql = "CREATE TABLE student(id INT PRIMARY KEY AUTO_INCREMENT,NAMEVARCHAR(20),gender VARCHAR(2))";
//5.发送sql语句,执行sql语句,得到返回结果
intcount = stmt.executeUpdate(sql);
//6.输出
System.out.println("影响了"+count+"行!");
}catch (Exception e) {
e.printStackTrace();
thrownew RuntimeException(e);
}finally{
//7.关闭连接(顺序:后打开的先关闭)
if(stmt!=null)
try{
stmt.close();
}catch (SQLException e) {
e.printStackTrace();
thrownew RuntimeException(e);
}
if(conn!=null)
try{
conn.close();
}catch (SQLException e) {
e.printStackTrace();
thrownew RuntimeException(e);
}
}
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.SQLException;
importjava.sql.Statement;
importorg.junit.Test;
/**
*使用Statement执行DML语句
* @author APPle
*
*/
publicclass Demo2 {
privateString url = "jdbc:mysql://localhost:3306/day17";
privateString user = "root";
privateString password = "root";
/**
* 增加
*/
@Test
publicvoid testInsert(){
Connectionconn = null;
Statementstmt = null;
try{
//通过工具类获取连接对象
conn= JdbcUtil.getConnection();
//3.创建Statement对象
stmt= conn.createStatement();
//4.sql语句
String sql = "INSERT INTOstudent(NAME,gender) VALUES('李四','女')";
//5.执行sql
intcount = stmt.executeUpdate(sql);
System.out.println("影响了"+count+"行");
}catch (Exception e) {
e.printStackTrace();
thrownew RuntimeException(e);
}finally{
//关闭资源
/*if(stmt!=null)
try{
stmt.close();
}catch (SQLException e) {
e.printStackTrace();
thrownew RuntimeException(e);
}
if(conn!=null)
try{
conn.close();
}catch (SQLException e) {
e.printStackTrace();
thrownew RuntimeException(e);
}*/
JdbcUtil.close(conn,stmt);
}
}
/**
* 修改
*/
@Test
publicvoid testUpdate(){
Connectionconn = null;
Statementstmt = null;
//模拟用户输入
String name = "陈六";
intid = 3;
try{
/*//1.注册驱动
Class.forName("com.mysql.jdbc.Driver");
//2.获取连接对象
conn= DriverManager.getConnection(url, user, password);*/
//通过工具类获取连接对象
conn= JdbcUtil.getConnection();
//3.创建Statement对象
stmt= conn.createStatement();
//4.sql语句
Stringsql = "UPDATE student SET NAME='"+name+"' WHEREid="+id+"";
System.out.println(sql);
//5.执行sql
intcount = stmt.executeUpdate(sql);
System.out.println("影响了"+count+"行");
}catch (Exception e) {
e.printStackTrace();
thrownew RuntimeException(e);
}finally{
//关闭资源
/*if(stmt!=null)
try{
stmt.close();
}catch (SQLException e) {
e.printStackTrace();
thrownew RuntimeException(e);
}
if(conn!=null)
try{
conn.close();
}catch (SQLException e) {
e.printStackTrace();
thrownew RuntimeException(e);
}*/
JdbcUtil.close(conn,stmt);
}
}
/**
* 删除
*/
@Test
publicvoid testDelete(){
Connectionconn = null;
Statementstmt = null;
//模拟用户输入
intid = 3;
try{
/*//1.注册驱动
Class.forName("com.mysql.jdbc.Driver");
//2.获取连接对象
conn= DriverManager.getConnection(url, user, password);*/
//通过工具类获取连接对象
conn= JdbcUtil.getConnection();
//3.创建Statement对象
stmt= conn.createStatement();
//4.sql语句
Stringsql = "DELETE FROM student WHERE id="+id+"";
System.out.println(sql);
//5.执行sql
intcount = stmt.executeUpdate(sql);
System.out.println("影响了"+count+"行");
}catch (Exception e) {
e.printStackTrace();
thrownew RuntimeException(e);
}finally{
//关闭资源
/*if(stmt!=null)
try{
stmt.close();
}catch (SQLException e) {
e.printStackTrace();
thrownew RuntimeException(e);
}
if(conn!=null)
try{
conn.close();
}catch (SQLException e) {
e.printStackTrace();
thrownew RuntimeException(e);
}*/
JdbcUtil.close(conn,stmt);
}
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.SQLException;
importjava.sql.Statement;
importorg.junit.Test;
/**
*使用Statement执行DML语句
* @author APPle
*
*/
publicclass Demo2 {
privateString url = "jdbc:mysql://localhost:3306/day17";
privateString user = "root";
privateString password = "root";
/**
* 增加
*/
@Test
publicvoid testInsert(){
Connectionconn = null;
Statementstmt = null;
try{
//通过工具类获取连接对象
conn= JdbcUtil.getConnection();
//3.创建Statement对象
stmt= conn.createStatement();
//4.sql语句
String sql = "INSERT INTOstudent(NAME,gender) VALUES('李四','女')";
//5.执行sql
intcount = stmt.executeUpdate(sql);
System.out.println("影响了"+count+"行");
}catch (Exception e) {
e.printStackTrace();
thrownew RuntimeException(e);
}finally{
//关闭资源
/*if(stmt!=null)
try{
stmt.close();
}catch (SQLException e) {
e.printStackTrace();
thrownew RuntimeException(e);
}
if(conn!=null)
try{
conn.close();
}catch (SQLException e) {
e.printStackTrace();
thrownew RuntimeException(e);
}*/
JdbcUtil.close(conn,stmt);
}
}
/**
* 修改
*/
@Test
publicvoid testUpdate(){
Connectionconn = null;
Statementstmt = null;
//模拟用户输入
String name = "陈六";
intid = 3;
try{
/*//1.注册驱动
Class.forName("com.mysql.jdbc.Driver");
//2.获取连接对象
conn= DriverManager.getConnection(url, user, password);*/
//通过工具类获取连接对象
conn= JdbcUtil.getConnection();
//3.创建Statement对象
stmt= conn.createStatement();
//4.sql语句
Stringsql = "UPDATE student SET NAME='"+name+"' WHEREid="+id+"";
System.out.println(sql);
//5.执行sql
intcount = stmt.executeUpdate(sql);
System.out.println("影响了"+count+"行");
}catch (Exception e) {
e.printStackTrace();
thrownew RuntimeException(e);
}finally{
//关闭资源
/*if(stmt!=null)
try{
stmt.close();
}catch (SQLException e) {
e.printStackTrace();
thrownew RuntimeException(e);
}
if(conn!=null)
try{
conn.close();
}catch (SQLException e) {
e.printStackTrace();
thrownew RuntimeException(e);
}*/
JdbcUtil.close(conn,stmt);
}
}
/**
* 删除
*/
@Test
publicvoid testDelete(){
Connectionconn = null;
Statementstmt = null;
//模拟用户输入
intid = 3;
try{
/*//1.注册驱动
Class.forName("com.mysql.jdbc.Driver");
//2.获取连接对象
conn= DriverManager.getConnection(url, user, password);*/
//通过工具类获取连接对象
conn= JdbcUtil.getConnection();
//3.创建Statement对象
stmt= conn.createStatement();
//4.sql语句
Stringsql = "DELETE FROM student WHERE id="+id+"";
System.out.println(sql);
//5.执行sql
intcount = stmt.executeUpdate(sql);
System.out.println("影响了"+count+"行");
}catch (Exception e) {
e.printStackTrace();
thrownew RuntimeException(e);
}finally{
//关闭资源
/*if(stmt!=null)
try{
stmt.close();
}catch (SQLException e) {
e.printStackTrace();
thrownew RuntimeException(e);
}
if(conn!=null)
try{
conn.close();
}catch (SQLException e) {
e.printStackTrace();
thrownew RuntimeException(e);
}*/
JdbcUtil.close(conn,stmt);
}
}
}