存入和读取大文本文件:
publicclass ClobTest {
publicstaticvoid main(String[] args)throws Exception{
//create();
read();
}
//读取大文本文件
publicstaticvoid read()throws Exception{
Connection conn=null;
Statement st=null;
ResultSet rs=null;
try{
conn=JDBCUtils.getConnection();
String sql="select big_text from clob_text";
st=conn.createStatement();
rs=st.executeQuery(sql);
while(rs.next()){
File file=new File("JDBCUtils_bak.java");
Writer writer=new BufferedWriter(new FileWriter(file));
Reader reader=rs.getCharacterStream(1);
char[] buf=newchar[1024];
int len=0;
while((len=reader.read(buf))!=-1){
writer.write(buf,0,len);
}
writer.close();
reader.close();
}
}finally{
JDBCUtils.free(conn,st, rs);
}
}
//存入大文本文件
publicstaticvoid create()throws Exception{
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
try{
conn=JDBCUtils.getConnection();
String sql="insert into clob_text(big_text)values(?)";
File file=new File("src/cn/itcast/jdbc/JDBCUtils.java");
Reader reader=new BufferedReader(new FileReader(file));
ps=conn.prepareStatement(sql);
ps.setCharacterStream(1, reader);
ps.executeUpdate();
}finally{
JDBCUtils.free(conn,ps, rs);
}
}
}
存入和读取二进制类型的数据:
注:blob的大小不能超过64K,若更大用longblob
//读取二进制数据
package cn.itcast.jdbc;
public class BlobTest {
public static void main(String[] args) throws Exception{
read();
}
public static void read()throws Exception{
Connection conn=null;
Statement st=null;
ResultSet rs=null;
try{
conn=JDBCUtils.getConnection();
String sql="select blob_bit from blob_test";
st=conn.createStatement();
rs=st.executeQuery(sql);
while(rs.next()){
File file=new File("11.jpg");
OutputStream out=new BufferedOutputStream(new FileOutputStream(file));
InputStream in=rs.getBinaryStream(1);
byte[] buf=newbyte[1024];
intlen=0;
while((len=in.read(buf))!=-1){
out.write(buf,0,len);
} }
out.close();
in.close();
}
}finally{
JDBCUtils.free(conn, st, rs);
}
}
//存入二进制数据
public static void create()throws Exception{
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
try{
conn=JDBCUtils.getConnection();
String sql="insert into blob_test(blob_bit)values(?)";
File file=new File("src/10.jpg");
InputStream in=new BufferedInputStream(new FileInputStream(file));
ps=conn.prepareStatement(sql);
ps.setBinaryStream(1, in,(int)file.length());
int i=ps.executeUpdate();
System.out.println(i);
}finally{
JDBCUtils.free(conn, ps, rs);
}
}
}
JDBC抽出的工具类:
package cn.itcast.jdbc;
public final class JDBCUtils {
private static final String url = "jdbc:mysql://localhost:3306/jdbc";
private static final String user = "root";
private static final String password = "admin";
private JDBCUtils() {
}
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
throw new ExceptionInInitializerError(e);
}
}
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, user, password);
}
public static void free(Connection conn, Statement st, ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (st != null) {
st.close();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e1) {
e1.printStackTrace();
}
}
}
}
}
接口类:
package cn.itcast.jdbc.dao;
import cn.itcast.jdbc.domain.User;
publicinterface UserDao {
publicvoid addUser(User user);
public User getUser(int userId);
publicvoid update(User user);
publicvoid delete(intuserId);
}
实现类:
package cn.itcast.jdbc.dao.impl;
publicclass UserDaoJdbcImplimplements UserDao{
@Override
publicvoid addUser(User user) {
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
try{
conn=JDBCUtils.getConnection();
String sql="insert into user(id,name,birthday,money)values(?,?,?,?)";
ps=conn.prepareStatement(sql);
ps.setInt(1, user.getId());
ps.setString(2, user.getName());
ps.setDate(3,new java.sql.Date(user.getBirthday().getTime()));
ps.setFloat(4, user.getMoney());
ps.executeUpdate();
}catch(SQLException e){
thrownew DaoException(e.getMessage(),e);
}finally{
JDBCUtils.free(conn, ps, rs);
}
}
@Override
public User getUser(int userId) {
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
User user=null;
try{
conn=JDBCUtils.getConnection();
String sql="select id,name,birthday,money from user where id=?";
ps=conn.prepareStatement(sql);
ps.setInt(1, userId);
rs=ps.executeQuery();
while(rs.next()){
user=new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setBirthday(rs.getDate("birthday"));
user.setMoney(rs.getFloat("money"));
}
}catch(SQLException e){
thrownew DaoException(e.getMessage(),e);
}finally{
JDBCUtils.free(conn, ps, rs);
}
return user;
}
@Override
publicvoid update(User user) {
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
try{
conn=JDBCUtils.getConnection();
String sql="update user set id=?,name=?,birthday=?,money=?";
ps=conn.prepareStatement(sql);
ps.setInt(1, user.getId());
ps.setString(2, user.getName());
ps.setDate(3,new java.sql.Date(user.getBirthday().getTime()));
ps.setFloat(4, user.getMoney());
}catch(SQLException e){
thrownew DaoException(e.getMessage(),e);
}finally{
JDBCUtils.free(conn, ps, rs);
}
}
@Override
publicvoid delete(int userId) {
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
try{
conn=JDBCUtils.getConnection();
String sql="delete from user where id=?";
ps=conn.prepareStatement(sql);
ps.setInt(1, userId);
ps.executeUpdate();
}catch(SQLException e){
thrownew DaoException(e.getMessage(),e);
}finally{
JDBCUtils.free(conn, ps, rs);
}
}
}
工厂模式:
UserDao userDao=DaoFactory.getInstance().getUserDao();
作用:降低对接口的依赖,使所有关于接口的改动只需要在配置文件改动!
工厂设计:
package cn.itcast.jdbc.dao;
publicclass DaoFactory {
privatestatic UserDaouserDao=null;
privatestatic DaoFactoryinstance=new DaoFactory();
//上两句不能颠倒
private DaoFactory(){
try{
Properties prop=new Properties();
InputStream inStream=new FileInputStream(new File("src/daoconfig.properties"));
prop.load(inStream);
String userDaoClass=prop.getProperty("userDaoClass");
userDao=(UserDao) Class.forName(userDaoClass).newInstance();
}catch(Throwable e){
thrownewExceptionInInitializerError(e);
}
}
public static DaoFactory getInstance(){
returninstance;
}
publicstatic UserDao getUserDao(){
returnuserDao;
}
}