jdbc详细教程(JDBC技术知识PreparedStatement)

发布日期:2024-05-15 11:57:38     作者:寡人寡心     手机:https://m.xinb2b.cn/tech/osy505291.html     违规举报
1、PreparedStatement概述

可以通过调用 Connection 对象的 preparedStatement(String sql) 方法获取 PreparedStatement 对象

PreparedStatement 接口是 Statement 的子接口,它表示一条预编译过的 SQL 语句

l PreparedStatement 对象所代表的 SQL 语句中的参数用问号(?)来表示,调用 PreparedStatement 对象的 setXxx() 方法来设置这些参数. setXxx() 方法有两个参数,第一个参数是要设置的 SQL 语句中的参数的索引(从 1 开始),第二个是设置的 SQL 语句中的参数的值

l ResultSet executeQuery()执行查询,并返回该查询生成的 ResultSet 对象。

l int executeUpdate():执行更新,包括增、删、该

2、Statement的不足

(1)SQL拼接

(2)SQL注入

SQL 注入是利用某些系统没有对用户输入的数据进行充分的检查,而在用户输入数据中注入非法的 sql 语句段或命令,从而利用系统的 SQL 引擎完成恶意行为的做法。对于 java 而言,要防范 SQL 注入,只要用 PreparedStatement 取代 Statement 就可以了。

(3)处理BLOB类型的数据

BLOB (binary large object),二进制大对象,BLOB常常是数据库中用来存储二进制文件的字段类型。

插入BLOB类型的数据必须使用PreparedStatement,因为BLOB类型的数据无法使用字符串拼接写的。

MySQL的四种BLOB类型(除了在存储的最大信息量上不同外,他们是等同的)

jdbc详细教程(JDBC技术知识PreparedStatement)(1)

如果还是报错:xxx too large,那么在mysql的安装目录下,找my.ini文件加上如下的配置参数:max_allowed_packet=16M注意:修改了my.ini文件,一定要重新启动服务

实际使用中根据需要存入的数据大小定义不同的BLOB类型。需要注意的是:如果存储的文件过大,数据库的性能会下降。

CREATE TABLE `user` (`id` int(11) NOT NULL AUTO_INCREMENT,`username` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,`head_picture` mediumblob,PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

package com.JDBC;import java.io.FileInputStream;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.util.Properties;public class TestBlob {public static void main(String[] args) throws Exception{//加载jdbc.properties资源配置文件Properties pro = new Properties();pro.load(ClassLoader.getSystemResourceAsStream("jdbc.properties"));//1、加载与注册驱动Class.forName(pro.getProperty("driver"));//2、获取数据库连接Connection conn = DriverManager.getConnection(pro.getProperty("url"), pro);//3、访问数据库//(1)准备带参数(?)的SQL//PreparedStatement 接口是 Statement 的子接口,它表示一条预编译过的 SQL 语句//PreparedStatement 对象所代表的 SQL 语句中的参数用问号(?)来表示String sql ="insert into user(username,head_picture) value(?,?)";//(2)通过调用 Connection 对象的 preparedStatement(String sql) 方法获取 PreparedStatement 对象PreparedStatement pst = conn.prepareStatement(sql);//(3)调用 PreparedStatement 对象的 setXxx(int parameterIndex,XX value) 方法来设置这些参数pst.setString(1, "lily");pst.setBlob(2, new FileInputStream("head/girl.jpg"));//(4)调用PreparedStatement的executeUpdate()执行sql语句进行插入//注意此处不能在传sql,否则?就白设置了int len = pst.executeUpdate();//(5)处理结果if (len > 0) {System.out.println("添加成功");} else {System.out.println("添加失败");}//4、释放资源pst.close();conn.close();}}

jdbc详细教程(JDBC技术知识PreparedStatement)(2)

3、PreparedStatement vs Statement

代码的可读性和可维护性. Statement的sql拼接是个难题。

PreparedStatement 可以防止 SQL 注入

PreparedStatement 可以处理Blob类型的数据

PreparedStatement 能最大可能提高性能:(Oracle和PostgreSQL8是这样,但是对于MySQL不一定比Statement高)

DBServer会对预编译语句提供性能优化。因为预编译语句有可能被重复调用,所以语句在被DBServer的编译器编译后的执行代码被缓存下来,那么下次调用时只要是相同的预编译语句就不需要编译,只要将参数直接传入编译过的语句执行代码中就会得到执行。

在statement语句中,即使是相同操作但因为数据内容不一样,所以整个语句本身不能匹配,没有缓存语句的意义.事实是没有数据库会对普通语句编译后的执行代码缓存.这样每执行一次都要对传入的语句编译一次.

(语法检查,语义检查,翻译成二进制命令,缓存)

4、示例代码

(1)使用Statement

package com.atguigu.statement;import java.sql.Connection;import java.sql.ResultSet;import java.sql.Statement;import java.util.Scanner;import org.junit.Test;import com.atguigu.utils.JDBCUtils;public class TestStatementProblem {@Testpublic void add() throws Exception{Scanner input = new Scanner(System.in);System.out.println("请输入姓名:");String name = input.nextLine();System.out.println("请输入领导编号:");int mid = input.nextInt();System.out.println("请输入部门编号:");int did = input.nextInt();//1、获取连接Connection conn = JDBCUtils.getConnection();//2、创建Statement对象Statement st = conn.createStatement();//3、编写sqlString sql = "INSERT INTO emp (ename,`mid`,did) VALUES('" name "'," mid "," did ")";//4、执行sqlint update = st.executeUpdate(sql);System.out.println(update>0?"添加成功":"添加失败");//5、释放资源JDBCUtils.closeQuietly(st, conn);}@Testpublic void select()throws Exception{Scanner input = new Scanner(System.in);System.out.println("请输入姓名:");String name = input.nextLine();//1、获取连接Connection conn = JDBCUtils.getConnection();//2、写sql//孙红雷 ' or '1' = '1String sql = "SELECT eid,ename,tel,gender,salary FROM t_employee WHERe ename = '" name "'";System.out.println(sql);// SELECt eid,ename,tel,gender,salary FROM t_employee WHERe ename = '孙红雷 ' or '1' = '1'//3、用Statement执行Statement st = conn.createStatement();//4、执行查询sqlResultSet rs = st.executeQuery(sql);while(rs.next()){int id = rs.getInt(1);String ename = rs.getString(2);String tel = rs.getString(3);String gender =rs.getString(4);double salary = rs.getDouble(5);System.out.println(id "\t" ename "\t" tel "\t" gender "\t" salary);}//5、释放资源JDBCUtils.closeQuietly(rs, st, conn);}@Testpublic void testAddBlob(){String sql = "INSERT INTO `user` (username,`password`,photo)VALUES('chai','123',没法在String中处理Blob类型的数据);";}}

(2)使用PreparedStatement

package com.atguigu.preparedstatement;import java.io.FileInputStream;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.util.Scanner;import org.junit.Test;import com.atguigu.utils.JDBCUtils;public class TestPreparedStatement {@Testpublic void add() throws Exception {Scanner input = new Scanner(System.in);System.out.println("请输入姓名:");String name = input.nextLine();System.out.println("请输入性别:");String gender = input.nextLine();System.out.println("请输入领导编号:");int mid = input.nextInt();System.out.println("请输入部门编号:");int did = input.nextInt();String sql = "INSERT INTO emp VALUES(NULL,?,?,?,?)";// 参数,占位符,通配符,表示这个地方需要设置值// 2、获取连接Connection conn = JDBCUtils.getConnection();// 3、准备一个PreparedStatement:预编译sqlPreparedStatement pst = conn.prepareStatement(sql);// 对带?的sql进行预编译// 4、把?用具体的值进行代替pst.setString(1, name);pst.setString(2, gender);pst.setInt(3, mid);pst.setInt(4, did);// 5、执行sqlint len = pst.executeUpdate();// 6、释放资源JDBCUtils.closeQuietly(pst, conn);}@Testpublic void select() throws Exception {// 3、写sqlScanner input = new Scanner(System.in);System.out.println("请输入姓名:");String name = input.nextLine();// 孙红雷 ' or '1' = '1String sql = "SELECT eid,ename,tel,gender,salary FROM t_employee WHERe ename = ?";// 1、注册驱动,注册过了// 2、获取连接Connection conn = JDBCUtils.getConnection();// 3、把带?的sql语句进行预编译PreparedStatement pst = conn.prepareStatement(sql);// 4、把?用具体的变量的赋值pst.setString(1, name);// 5、执行sqlResultSet rs = pst.executeQuery();while (rs.next()) {int id = rs.getInt("eid");String ename = rs.getString("ename");String tel = rs.getString("tel");String gender = rs.getString("gender");double salary = rs.getDouble("salary");System.out.println(id "\t" ename "\t" tel "\t" gender "\t" salary);}// 6、释放资源JDBCUtils.closeQuietly(rs, pst, conn);}@Testpublic void addBlob() throws Exception {Scanner input = new Scanner(System.in);System.out.println("请输入用户名:");String username = input.nextLine();System.out.println("请输入密码:");String password = input.nextLine();System.out.println("请指定照片的路径:");String photoPath = input.nextLine();// INSERT INTO `user` VALUES(NULL,用户名,密码,照片)String sql = "INSERT INTO `user` VALUES(NULL,?,?,?)";// 1、注册驱动,注册过了// 2、获取连接Connection conn = JDBCUtils.getConnection();// 3、准备一个PreparedStatement:预编译sqlPreparedStatement pst = conn.prepareStatement(sql);// 对带?的sql进行预编译// 4、对?进行设置pst.setString(1, username);pst.setString(2, password);pst.setBlob(3, new FileInputStream(photoPath));// 5、执行sqlint len = pst.executeUpdate();System.out.println(len > 0 ? "添加成功" : "添加失败");// 6、释放资源JDBCUtils.closeQuietly(pst, conn);}}

相关阅读:

JDBC工具类

如何使用JDBC API

java编程技术JDBC

如何使用JDBC API操作数据库

数据库连接池之DBCP数据源

 
 
本文地址:https://www.xinb2b.cn/tech/osy505291.html,转载请注明出处。

推荐图文
推荐科技知识
网站首页  |  关于我们  |  联系方式  |  使用协议  |  版权隐私  |  网站地图  |  违规举报  |  蜀ICP备18010318号-4  |  百度地图  | 
Processed in 0.023 second(s), 6 queries, Memory 0.58 M