mysql 附件上传_JDBC中级篇(MYSQL)——模拟从数据库中上传下载附件

smile.gif注意:其中的JdbcUtil是我自定义的连接工具类:代码例子链接:

package b_blob_clob;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.Date;

import util.JdbcUtil;

/**

* 模拟一个从数据库中:

* 上传下载附件

*

* @author mzy

*

*/

public class Demo03 {

public static void main(String[] args) {

write();

read();

}

private static void read() {

/**

* 读出

*/

Connection conn = null;

PreparedStatement stmt = null;

ResultSet rs = null;

try{

conn = JdbcUtil.getConnection();

String sql = "select * from attachments where id=?";

//预编译

stmt = conn.prepareStatement(sql);

//参数赋值

stmt.setInt(1, 2);

//执行查询

rs = stmt.executeQuery();

//遍历结果

if(rs.next()){

String name = rs.getString("name");

InputStream in = rs.getBinaryStream("file"); //rs.getBlob("file").getBinaryStream();

//取出附件

//写出到硬盘

BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("e:/picture.png"));

byte[] buf = new byte[1024];

int len = 0;

//边读边写

while( (len=in.read(buf))!=-1 ){

out.write(buf, 0, len);

}

//关闭流

out.close();

in.close();

Date date = rs.getDate("addtime");

String author = rs.getString("author");

System.out.println(name+"\t"+date+"\t"+author);

}

}catch(Exception e){

e.printStackTrace();

}finally{

JdbcUtil.close(stmt, conn);

}

}

private static void write() {

/**

* 写入

*/

Connection conn = null;

PreparedStatement stmt = null;

conn = JdbcUtil.getConnection();

String sql = "insert into attachments(name,file,addtime,author) values(?,?,?,?)";

try {

// 预编译

stmt = conn.prepareStatement(sql);

// 读取图片

File file = new File("./src/人和鸟.png");

FileInputStream in = new FileInputStream(file);

String fileName = file.getName();

// 去掉后缀名

fileName = fileName.substring(0, fileName.indexOf("."));

stmt.setString(1, fileName);

stmt.setBlob(2, in);

// 第三个时间的插入,插入的话要遵照mysql的Date类型,所以要进行转换

stmt.setDate(3, new java.sql.Date(new Date().getTime()));

stmt.setString(4, "mzy");

stmt.executeUpdate();

in.close();

System.out.println("添加成功!");

} catch (SQLException | IOException e) {

e.printStackTrace();

} finally {

JdbcUtil.close(stmt, conn);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,您的问题是关于使用 JDBC 直接操作 MySQL 数据库进行 PreparedStatement 的插入操作。下面是示例代码: ```java import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; public class InsertExample { public static void main(String[] args) { // 数据库连接信息 String url = "jdbc:mysql://localhost:3306/test"; String user = "root"; String password = "123456"; // SQL 语句 String sql = "INSERT INTO user (name, age) VALUES (?, ?)"; try ( // 获取数据库连接 Connection conn = DriverManager.getConnection(url, user, password); // 创建 PreparedStatement 对象 PreparedStatement pstmt = conn.prepareStatement(sql); ) { // 设置参数 pstmt.setString(1, "张三"); pstmt.setInt(2, 20); // 执行 SQL 语句 int rows = pstmt.executeUpdate(); // 输出结果 System.out.println("插入了 " + rows + " 行数据"); } catch (SQLException e) { e.printStackTrace(); } } } ``` 上述示例代码,我们首先定义了数据库连接信息和插入操作的 SQL 语句。然后,我们通过 DriverManager.getConnection() 方法获取数据库连接,再使用 conn.prepareStatement() 方法创建 PreparedStatement 对象。接下来,我们使用 pstmt.setString() 和 pstmt.setInt() 方法设置 SQL 语句的占位符参数。最后,我们使用 pstmt.executeUpdate() 方法执行 SQL 语句,并输出插入的行数。 注意,在使用 PreparedStatement 时,我们可以通过占位符 ? 来指定参数,这样可以避免 SQL 注入攻击。另外,PreparedStatement 对象在执行 SQL 语句时可以重复利用,可以提高数据库操作的效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值