JDBC编程步骤


一、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秒)
StatementPreparedStatement
关系父接口子接口
语法stm=conn.createStatement();
stm.executeUpdate(sql);
String sql ="…?..";
pstm=conn.prepareStatement(sql);
pstm.set***(index,value);
pstm.executeUpdate();
安全存在安全注入隐患解决了SQL注入问题
效率执行异构sql效率较高执行同构sql效率较高
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hudie.

不要打赏!不要打赏!不要打赏!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值