数据库存取对象


代码
  1 /*
  2  * To change this template, choose Tools | Templates
  3  * and open the template in the editor.
  4  */
  5 
  6 package objectintodb;
  7 
  8 import java.io.ByteArrayInputStream;
  9 import java.io.IOException;
 10 import java.io.ObjectInputStream;
 11 import java.io.ObjectOutputStream;
 12 import java.io.PipedInputStream;
 13 import java.io.PipedOutputStream;
 14 import java.sql.Connection;
 15 import java.sql.DriverManager;
 16 import java.sql.PreparedStatement;
 17 import java.sql.ResultSet;
 18 import java.sql.SQLException;
 19 import java.util.logging.Level;
 20 import java.util.logging.Logger;
 21 
 22 /**
 23  *
 24  * @author root
 25  */
 26 public class Main {
 27 
 28     /**
 29      * @param args the command line arguments
 30      */
 31     public static void main(String[] args) throws SQLException, ClassNotFoundException, IOException {
 32         // TODO code application logic here
 33         Connection conn=getConn();
 34         java.sql.PreparedStatement pstmt=conn.prepareStatement("insert into user values (?)");
 35 
 36         saveObject(new User("ty"""20), pstmt, 1);
 37         pstmt.execute();
 38        ResultSet rs=conn.createStatement().executeQuery("select user from user");
 39         User user=null;
 40        while(rs.next())
 41        {
 42            user= (User)(readObject(rs, "user"));
 43        }
 44        rs.close();
 45 
 46        System.out.println(user.getUname());
 47 
 48     }
 49 
 50  
 51 
 52 
 53     //保存对象
 54     static  void saveObject(User user,PreparedStatement pstmt,int paramterIndex)
 55     {
 56         PipedInputStream is=new PipedInputStream();
 57         try {
 58             ObjectOutputStream oout = new ObjectOutputStream(new PipedOutputStream(is));
 59             oout.writeObject(user);
 60             oout.close();
 61             pstmt.setBinaryStream(1, is, is.available());
 62         } catch (IOException ex) {
 63             Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
 64         }
 65         catch (SQLException sx)
 66         {
 67             sx.printStackTrace();
 68         }
 69     }
 70 
 71 
 72 
 73 
 74     //读取对象
 75     static Object readObject(ResultSet rs,String column) throws ClassNotFoundException, IOException, SQLException
 76     {
 77         byte[] buf=rs.getBytes(column);
 78         if(buf!=null)
 79         {
 80             ObjectInputStream objIn=new ObjectInputStream(new ByteArrayInputStream(buf));
 81             return objIn.readObject();
 82         }
 83         return null;
 84     }
 85 
 86 
 87     static Connection getConn()
 88     {
 89         Connection conn=null;
 90         try {
 91             String driver = "com.mysql.jdbc.Driver";
 92             String url = "jdbc:mysql://localhost:3306/user";
 93             String username = "ty";
 94             String pwd = "ty";
 95             Class.forName(driver);
 96             conn = DriverManager.getConnection(url, username, pwd);
 97         } catch (SQLException ex) {
 98             Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
 99         } catch (ClassNotFoundException ex) {
100             Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
101         }
102         
103 
104         return conn;
105 
106     }
107 
108 }

存取的对象需要实现序列化借口

posted @ 2009-11-27 10:01  busing  阅读(189)  评论(0编辑  收藏  举报