使用JDBC完成对数据库数据的操作
1.首先创建一张数据库表
2.创建一个类,开始编写JDBC程序,这里只是插入了一条数据进行展示
package com.test.jdbc;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
public class DemoJDBC {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
InputStream is = null;
OutputStream os = null;
Reader rd = null;
try {
//加载驱动类
Class.forName("com.mysql.jdbc.Driver");
//建立连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/sshdemo","root","8888");
//添加信息
addinfo(conn,ps);
//查询信息
selectInfo(conn, ps, rs, is, os, rd);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if(is!=null){
is.close();
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if(os!=null){
os.close();
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if(ps!=null){
ps.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(conn!=null){
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 添加数据
* @param conn
* @param ps
*/
private static void addinfo(Connection conn,PreparedStatement ps) {
try {
ps = conn.prepareStatement("insert into user (id,name,createdate,createtime,textnode,photo) values (?,?,?,?,?,?)");
ps.setInt(1, 1);
ps.setString(2, "测试");
java.sql.Date date = new java.sql.Date(System.currentTimeMillis());
Timestamp stamp = new Timestamp(System.currentTimeMillis());
ps.setDate(3, date);
ps.setTimestamp(4, stamp);
try {
//将文本文件内容直接输入到数据库中
ps.setClob(5, new FileReader(new File("f:/text/a.txt")));
//将图片文件内容直接输入到数据库中
ps.setBlob(6, new FileInputStream("f:/image/img/1.jpg"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//也可以将程序中的大量字符串输入到数据库的CLOB字段中
// ps.setClob(5, new BufferedReader(new InputStreamReader(new ByteArrayInputStream("aaaabbb111111bbb".getBytes()))));
ps.executeUpdate();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
/**
* 查询数据信息
* @param conn
* @param ps
* @param rs
* @param is
* @param os
* @param rd
*/
private static void selectInfo(Connection conn,PreparedStatement ps,ResultSet rs,InputStream is,OutputStream os,Reader rd) {
try {
ps = conn.prepareStatement("select * from user where id=?");
ps.setObject(1, 1);
rs = ps.executeQuery();
while(rs.next()){
Clob c = rs.getClob("textnode");
rd = c.getCharacterStream();
Blob b = rs.getBlob("photo");
is = b.getBinaryStream();
try {
int temp1 = 0;
while((temp1=rd.read())!=-1){
System.out.print((char)temp1); //将数据库中的textnode数据读取出来
}
os = new FileOutputStream("f:/image/img/wyd.jpg"); //将数据库中的photo图片保存到一个路径
int temp2 = 0;
while((temp2=is.read())!=-1){
os.write(temp2);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
运行main方法后,在数据库中查看到添加的数据
控制台,打印出了textnode字段的信息:
在图片输出对应的文件夹下看到了数据库中的图片