}
Connection conn = null; Statement stat = null; try{ conn = JDBCUtils.getConn(); stat = conn.createStatement(); stat.executeUpdate(\}catch (Exception e) { e.printStackTrace(); }finally{ JDBCUtils.close(null, stat, conn); }
4.3CRUD操作-delete
public void delete(){ Connection conn = null; Statement stat = null; ResultSet rs = null; try{ conn = JDBCUtils.getConn(); stat = conn.createStatement(); stat.executeUpdate(\ }catch (Exception e) { e.printStackTrace(); }finally{ JDBCUtils.close(rs, stat, conn); } }
4.4CRUD操作-read
public void find(){ Connection conn = null; Statement stat = null; ResultSet rs = null; try{ conn = JDBCUtils.getConn(); stat = conn.createStatement(); rs = stat.executeQuery(\ while(rs.next()){ String name = rs.getString(\ String password = rs.getString(\ System.out.println(name+\ } }catch (Exception e) { e.printStackTrace(); }finally{ JDBCUtils.close(rs, stat, conn); } }
5.PreparedStatement
PreperedStatement是Statement的孩子,它的实例对象可以通过调用Connection.preparedStatement()方法获得,相对于Statement对象而言:
? ? ? PreperedStatement可以避免SQL注入的问题。
Statement会使数据库频繁编译SQL,可能造成数据库缓冲区溢出。PreparedStatement 可对SQL进行预编译,从而提高数据库的执行效率。
并且PreperedStatement对于sql中的参数,允许使用占位符的形式进行替换,简化sql语句的编写。 public static void main(String[] args) { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try{ conn = JDBCUtils.getConn(); ps = conn.prepareStatement(\ ps.setString(1, \ ps.setString(2, \ rs = ps.executeQuery(); while(rs.next()){ System.out.println(rs.getString(\ } }catch (Exception e) { e.printStackTrace(); }finally{ JDBCUtils.close(rs, ps, conn); } }
6.使用JDBC处理大数据
? ? ?
在实际开发中,程序需要把大文本Text 或二进制数据Blob保存到数据库。 Text是mysql叫法,Oracle中叫Clob 。
基本概念:大数据也称之为LOB(Large Objects),LOB又分为: clob和blob
? clob用于存储大文本。Text
? blob用于存储二进制数据,例如图像、声音、二进制文等。
对MySQL而言只有blob,而没有clob,mysql存储大文本采用的是Text,Text和blob分别又分为:
? TINYTEXT(255)、TEXT(64k)、MEDIUMTEXT(16M)和LONGTEXT(4G) ? TINYBLOB、BLOB、MEDIUMBLOB和LONGBLOB
?
6.1操作大文本
@Test
public void findText(){ Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try{ conn = JDBCUtils.getConn(); ps = conn.prepareStatement(\ ps.setInt(1, 1); rs = ps.executeQuery(); while(rs.next()){ String filename = rs.getString(\ Reader reader = rs.getCharacterStream(\ Writer writer = new FileWriter(filename); char [] cs = new char[1024]; int i=0;
while((i=reader.read(cs))!=-1){ writer.write(cs,0,i); } reader.close(); writer.close(); } }catch (Exception e) { e.printStackTrace(); }finally{ JDBCUtils.close(rs, ps, conn); } } @Test public void addText(){ Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try{ conn = JDBCUtils.getConn(); ps = conn.prepareStatement(\ ps.setString(1, \钢铁是怎样练成的.txt\ File file = new File(\ ps.setCharacterStream(2, new FileReader(file), (int)file.length()); ps.executeUpdate(); }catch (Exception e) { e.printStackTrace(); }finally{ JDBCUtils.close(rs, ps, conn); } }
其中会遇到一下三个错误: (1)Exception in thread \java.lang.AbstractMethodError: com.mysql.jdbc.PreparedStatement.setCharacterStream(ILjava/io/Reader;J)V
//ps.setCharacterStream(2, new FileReader(file), file.length());第三个参数是long型的是从1.6才开始支持的,驱动里还没有开始支持。
//解决方案:ps.setCharacterStream(2, new FileReader(file), (int)file.length()); (2)Exception in thread \
//文件大小过大,导致PreparedStatement中数据多大占用内存,内存溢出。 //-Xms256M-Xmx256M
(3)com.mysql.jdbc.PacketTooBigException: Packet for query is too large (10886466 > 1048576). You can change this value on the server by setting the max_allowed_packet' variable.
//数据库连接传输用的包不够大,传输大文本时报此错误。 //在my.ini中配置max_allowed_packet指定包的大小。
6.2操作二进制数据
@Test
public void findBlob(){ Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try{ conn = JDBCUtils.getConn();
ps = conn.prepareStatement(\ rs = ps.executeQuery(); while(rs.next()){ String name = rs.getString(\ InputStream in = rs.getBinaryStream(\ OutputStream out = new FileOutputStream(name); byte [] bs = new byte[1024]; int i = 0; while((i=in.read(bs))!=-1){ out.write(bs,0,i); } in.close(); out.close(); } }catch (Exception e) { e.printStackTrace(); }finally{ JDBCUtils.close(rs, ps, conn); } }
@Test
public void addBlob(){ Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try{ conn = JDBCUtils.getConn(); ps = conn.prepareStatement(\ ps.setString(1, \洛天依.mp3\ File file = new File(\ ps.setBinaryStream(2, new FileInputStream(file),(int)file.length()); ps.executeUpdate(); }catch (Exception e) { e.printStackTrace(); }finally{ JDBCUtils.close(rs, ps, conn); } }
7.使用JDBC进行批处理
? 业务场景:当需要向数据库发送一批SQL语句执行时,应避免向数据库一条条的发送执行,而应采用JDBC的批处理机制,以提升执行效率。
? 实现批处理有两种方式,第一种方式: ? Statement.addBatch(sql) ? 执行批处理SQL语句
? executeBatch()方法:执行批处理命令 ? clearBatch()方法:清除批处理命令 ? Statement方式执行批处理:
? 优点:可以执行多条不同结构的sql语句。
? 缺点:没有使用预编译机制,效率低下,如果要执行多条结构相同仅仅参数不同的sql时,仍然需要写多次sql
语句的主干。
public static void main(String[] args) {
Connection conn = null; Statement stat = null; try{ conn = JDBCUtils.getConn(); stat = conn.createStatement(); stat.addBatch(\ stat.addBatch(\ stat.addBatch(\ \ \ \ stat.addBatch(\ stat.addBatch(\ stat.addBatch(\ stat.addBatch(\ stat.executeBatch(); }catch (Exception e) { e.printStackTrace(); }finally{ JDBCUtils.close(null, stat, conn); } }
? 实现批处理有两种方式,第二种方式:
? PreparedStatement.addBatch()
? prparedStatement 方式实现的批处理:
? 优点:有预编译机制,效率比较高.执行多条结构相同,参数不同的sql时,不需要重复写sql的主干。 ? 缺点:只能执行主干相同参数不同的sql,没有办法在一个批中加入结构不同的sql。
public static void main(String[] args) { Connection conn = null; PreparedStatement ps = null; try{ conn = JDBCUtils.getConn(); ps = conn.prepareStatement(\ for(int i=1;i<=100000;i++){ ps.setString(1, \ ps.addBatch(); if(i00==0){ ps.executeBatch(); ps.clearBatch(); } } ps.executeBatch(); }catch (Exception e) { e.printStackTrace(); }finally{ JDBCUtils.close(null, ps, conn); } }