初识jdbc+批量处理+存取图片

本文详细介绍了在Java中使用Statement和PreparedStatement进行批量数据库操作的方法,并对比了两者的区别。此外,还演示了如何使用Java向数据库保存图片以及从数据库读取图片的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.qf.demo1;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;

public class Demo1 {
	private static Connection conn = null;
	private static PreparedStatement sm = null;
	private static ResultSet rs = null;
	static {
		try {
			//注册驱动
			Class.forName("com.mysql.jdbc.Driver");
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static Connection getConnection() {
		//获取连接
		try {
			conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/teacher","root","root");
			return conn;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	public static  void close(Connection conn,Statement sm,ResultSet rs) {
		try {
			if(conn!=null) {
				conn.close();
			}
			if(sm!=null) {
				sm.close();
			}
			if(rs!=null) {
				rs.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static  int executeUpdate(String sql,Object...arr) {
		Connection conn = null;
		PreparedStatement sm = null;
		ResultSet rs = null;
		conn  = getConnection();
		try {
			sm = (PreparedStatement) conn.prepareStatement(sql);
			for(int i=0;i<arr.length;i++) {
				sm.setObject(i+1,arr[i]);
			}
			//如果使用Statement,则会有sql注入的风险
			int i= sm.executeUpdate();
			System.out.println(i);
			/*while(rs.next()) {
				int id = rs.getInt(1);
				String name = rs.getString(2);
				System.out.println(id);
				System.out.println(name);
			}*/
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			close(conn, sm, rs);
		}
		
		
		
		return 0;
	}
}

批量处理(Statement)
package com.qf.demo1;

import java.io.IOException;
import java.io.InputStream;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.Properties;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

public class Demo2 {
	private static  Connection connection;
	private static Statement statement;
	private static ResultSet rs;
	private static String driver = null;
	private static String url = null;
	private static String username = null;
	private static String password = null;
	static {
		InputStream is = Demo2.class.getClassLoader().getResourceAsStream("db.properties");
		Properties p2 = new Properties();
		try {
			p2.load(is);
			driver = p2.getProperty("driver");
			url = p2.getProperty("url");
			password = p2.getProperty("password");
			username = p2.getProperty("username");
			Class.forName(driver);
			connection = (Connection) DriverManager.getConnection(url,username,password);
			statement = (Statement) connection.createStatement();
			//设置事务不会自动提交
			connection.setAutoCommit(false);
			statement.addBatch("insert into s1 values(6,'张三')");
			statement.addBatch("insert into s1 values(7,'李四')");
			statement.addBatch("insert into s1 values(8,'王五')");
			statement.addBatch("delete from s1 where id = 1");
			int[] count = statement.executeBatch();
			System.out.println(count);
			//提交事务
			connection.commit();
			statement.close();
			connection.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void main(String[] args) {
		
	}
}

批量处理(PreparedStatement)
package com.qf.demo1;

import java.io.IOException;
import java.io.InputStream;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.Properties;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;

public class Demo3 {
	private static  Connection connection;
	private static PreparedStatement preparedStatement;
	private static ResultSet rs;
	private static String driver = null;
	private static String url = null;
	private static String username = null;
	private static String password = null;
	static {
		InputStream is = Demo2.class.getClassLoader().getResourceAsStream("db.properties");
		Properties p2 = new Properties();
		try {
			p2.load(is);
			driver = p2.getProperty("driver");
			url = p2.getProperty("url");
			password = p2.getProperty("password");
			username = p2.getProperty("username");
			Class.forName(driver);
			connection = (Connection) DriverManager.getConnection(url,username,password);
			preparedStatement = (PreparedStatement) connection.prepareStatement("insert into s1 values(?,?)");
			connection.setAutoCommit(false);
			for(int i=10;i<20;i++) {
				preparedStatement.setObject(1,i);
				preparedStatement.setObject(2,"黄狗子");
				preparedStatement.addBatch();
			}
			
			int[] count = preparedStatement.executeBatch();
			System.out.println(count.length);
			connection.commit();
			preparedStatement.close();
			connection.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void main(String[] args) {
		
	}
}

由上面两种批量处理发现,statement可同时处理多种操作,preparedstatement只能处理单类型的操作

向数据库中保存图片
Class.forName(driver);
			connection = (Connection) DriverManager.getConnection(url,username,password);
			preparedStatement = (PreparedStatement) connection.prepareStatement("insert into s2 values(?,?)");
			FileInputStream fis = new FileInputStream("f:\\001.jpg");
			preparedStatement.setInt(1, 2);
			**preparedStatement.setBinaryStream(2, fis);**
			int count = preparedStatement.executeUpdate();
			System.out.println(count);
			connection.close();
			preparedStatement.close();

读取图片

preparedStatement = (PreparedStatement) connection.prepareStatement("select * from s2 where sid = 2");
			FileOutputStream fos = new FileOutputStream("f:\\001copy.jpg");
			rs = preparedStatement.executeQuery();
			while(rs.next()) {
				int id = rs.getInt(1);
				InputStream iss = rs.getBinaryStream(2);
				byte[] buf = new byte[1024];
				int len = 0;
				while((len = iss.read(buf))!=-1) {
					fos.write(buf, 0, len);
				}
				fos.close();
				is.close();
			}
			System.out.println("读取图片成功");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值