新手,借鉴了一下网上大神的示例:http://blog.csdn.net/hope2jiang/article/details/590733
大家可以看一下原作者是如何做的,我只是拿过来学习一下,然后做一下实际测试。大笑大笑
首先,大家应该做好准备工作:
1.JDBC驱动包下载(http://www.mysql.com/downloads/).
2.XAMPP下载{(Apache+MySQL+PHP+PERL)是一个功能强大的建 XAMPP 软件站集成软件包}.
下载成功后启动 Apache和MySQL………..
并且
3.在CMD下进入mql添加一个database(暂且命名为test),并创建一个表 pic.
创建表代码:
create table pic(idpic int auto_increment primary key,namepic varchar(45) NOT NULL,img longblob NOT NULL);
注:图片是以二进制的方式直接插入,当然地址插入最好,这里仅做测试示范。
添加成功后可以在XAMPP下的mysql的data下看到创建成功后的db文件。
OK:接下来准备工作做好了就开始在java中使用JDBC建立数据库连接。
代码:
package Demo3;
import java.io.File;
import java.io.FileInputStream;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
public class TestDemo {
private String dbDriver;
private String dbURL;
private String dbUser;
private String dbPassword;
private java.sql.Connection con;
private PreparedStatement ps;
/* 执行静态SQL语句。通常通过Statement实例实现。 */
public TestDemo() throws InstantiationException, IllegalAccessException{
dbDriver="com.mysql.jdbc.Driver";
dbURL="jdbc:mysql://localhost:3306/test";
dbUser="root";
dbPassword="";
InitDB();
}
public TestDemo(String strDriver, String strURL,
String strUser, String strPwd) throws InstantiationException, IllegalAccessException {
dbDriver = strDriver;
dbURL = strURL;
dbUser = strUser;
dbPassword = strPwd;
InitDB();
}
private void InitDB() throws InstantiationException, IllegalAccessException {
// TODO Auto-generated method stub
try {
Class.forName(dbDriver).newInstance();
/*
* 链接Mysql数据库,统一资源定位器,用户名,密码///
*/
con=DriverManager.getConnection(dbURL,dbUser,dbPassword);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("SQLException: " + e.getMessage());
System.out.println("SQLState: " + e.getSQLState());
System.out.println("VendorError: " + e.getErrorCode());
e.printStackTrace();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public boolean storeImg(String strFile) throws Exception {
boolean written=false;
if(con==null)
written=false;
else{
int id=0;
File file=new File(strFile);
FileInputStream fis=new FileInputStream(file);
ps=(PreparedStatement) con.prepareStatement("Select MAX(idpic) from pic");
ResultSet rs=ps.executeQuery();
if(rs!=null)
{
while(rs.next())
id=rs.getInt(1)+1;
}else
{
return written;
}
ps = (PreparedStatement) con.prepareStatement("insert "
+ "into PIC values (?,?,?)");
ps.setInt(1, id);
ps.setString(2, file.getName());
ps.setBinaryStream(3, fis, (int) file.length());
ps.executeUpdate();
written = true;
ps.close();
fis.close();
// close db con
con.close();
}
return written;
}
public static void main(String[] args) throws InstantiationException, IllegalAccessException
{
boolean flag = false;
TestDemo sp = new TestDemo();
try {
flag = sp.storeImg("D:\\01.jpg"); //这个地方应该是自己本地的图片地址。
} catch (Exception e) {
e.printStackTrace();
}
if(flag) {
System.out.println("Picture uploading is successful."); //如果上传成功会显示。
} else {
System.out.println("Picture uploading is failed.");
}
}
}
运行代码后,如何上传成功会在控制台显示Picture uploading is successful……..这样就可以把指定的文件存入数据库了。
也可以打开http://127.0.0.1/phpmyadmin/ 查看
然后我们可以自己写一个PHP程序把图片从数据库读取出来。(PHP只学了一点点,班门弄斧。。。)
<?php
$dbh=new PDO("mysql:host=localhost;port=3306;dbname=Test","root","");
$dbh->exec("SET CHARACTER SET gb2312");
$result=$dbh->query("SELECT * FROM pic Where idpic=1");
$row = $result->fetch();
if(!empty($row)){
header("Content-type: image/JPEG",true);
echo($row["img"]);
}
//显式的关闭PDO连接
$dbh = NULL;
?>
写好的PHP文件放到\XAMPP\htdocs 下,然后在浏览器地址栏输入127.0.0.1/自己的PHP文件名,下面是我运行后的结果。。。。。
OK,完毕,由于是第一次写博客,写的很乱,请大家见谅哈。