mysql mediumblob insert_(Java+MySQL)MySQL大字段(数据类型mediumblob)存储图片、doc文件失败,请问是什么原因?...

我在复习Java的JDBC部分时,参照书上做大字段存取的练习,遇到了问题,特来求助,希望大哥们帮我看下:

我建了一个数据表,表结构如下:

mysql> desc file_table;

+-----------+-------------+------+-----+---------+----------------+

| Field | Type | Null | Key | Default | Extra |

+-----------+-------------+------+-----+---------+----------------+

| file_id | int(11) | NO | PRI | NULL | auto_increment |

| file_name | varchar(64) | YES | | NULL | |

| file_data | mediumblob | YES | | NULL | |

+-----------+-------------+------+-----+---------+----------------+

//配置文件:

dbType=mysql

driver=com.mysql.jdbc.Driver

url=jdbc:mysql://127.0.0.1:3306/20130316

user=root

pass=123456

//代码片段如下

private static final String PROP_FILE_PATH = "conf/mysql.properties";

private static Connection conn = null;

private static PreparedStatement insertPstmt = null;

/**

* 读取配置文件→获取数据库连接→生成预编译对象

*/

private void init() {

prop = new Properties();

try {

prop = MyUtil.initParam(PROP_FILE_PATH);

} catch (FileNotFoundException e) {

System.out.println("找不到配置文件: " + e);

e.printStackTrace();

} catch (IOException e) {

System.out.println("加载配置文件出错: " + e);

e.printStackTrace();

}

String driver = prop.getProperty("driver");

String url = prop.getProperty("url");

String user = prop.getProperty("user");

String pass = prop.getProperty("pass");

// 自定义方法,获取数据库连接

conn = MyUtil.getConnection(driver, url, user, pass);

String insertSQL = "insert into file_table values(null, ?, ?)";

// 该对象执行插入语句后能够返回自动生成的键

try {

insertPstmt = conn.prepareStatement(insertSQL, Statement.RETURN_GENERATED_KEYS);

} catch (SQLException e) {

System.out.println("添加文件失败,原因:预编译插入语句失败: " + e);

e.printStackTrace();

}

}

/**

* 上传文件到数据表

* @param filePath

*/

private void upload(String filePath) {

InputStream inStream = null;

int lastIndexOfNode = filePath.lastIndexOf(".");

int lastIndexOfBacklash = filePath.lastIndexOf("\");

String fileName = filePath.substring(lastIndexOfBacklash + 1, lastIndexOfNode);

System.out.println("filePath=" + filePath);

System.out.println("fileName=" + fileName);

int affect = 0;

try {

insertPstmt.setString(1, fileName);

inStream = new FileInputStream(filePath);

File file= new File(filePath);

// 设置二进制流参数

insertPstmt.setBinaryStream(2, inStream, file.length());

affect = insertPstmt.executeUpdate();

System.out.println("affect=" + affect);

if (affect == 1) {

System.out.println("文件(" + filePath + ")上传成功");

}

} catch (FileNotFoundException e) {

System.out.println("找不到文件: " + e);

e.printStackTrace();

} catch (SQLException e) {

// System.out.println("上传文件失败,原因向数据表添加记录出错: " + e);

e.printStackTrace();

} finally {

// 回收IO资源,自定义的方法

MyUtil.closeInputStream(inStream);

}

}

public static void main(String[] args) {

try {

UploadFileTest uploadTest = new UploadFileTest();

uploadTest.init();

String txtPath = "C:\Documents and Settings\Administrator\桌面\temp.txt";

String docPath = "C:\Documents and Settings\Administrator\桌面\temp.doc";

String jpgPath = "C:\Documents and Settings\Administrator\桌面\temp.jpg";

uploadTest.upload(txtPath);

uploadTest.upload(docPath);

uploadTest.upload(jpgPath);

} catch (Exception e) {

e.printStackTrace();

} finally {

//自定义方法,回收数据库资源

MyUtil.closePreparedStatement(insertPstmt);

MyUtil.closeConnection(conn);

}

}

上面就是代码片段,出现的问题是:上传纯文本文件(.txt)正常;上传.doc(里边没有贴图的也可以上传成功,但是再取出doc文件后,打开就是乱码了,而且文件的大小也变了)、*.jpg文件上传失败(

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?#?§?×?BRP?M?v??á??i??R??;?p?+?\0??§c?e¤d+70

别人也曾指点我“文件上传只保存地址即可,也可减轻数据库负担,增加访问速度”。但这明显不是我想要的答案,我就是想知道①为什么会引发SQL异常(异常提示说我SQL语句有语法问题),②该怎么解决修复这个异常。希望经验丰富的哥哥姐姐们抽空指点一下小弟(why & how)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值