📄 rpersistantmap.java
字号:
/* * * Copyright 2003,2004 The Watermill Team * * This file is part of Watermill. * * Watermill is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Watermill is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Watermill; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */package Watermill.relational;import java.io.*;import java.sql.*;import Watermill.kernel.*;public class RPersistantMap extends PersistantMap { private Connection connection; private String tableName; public RPersistantMap(Connection c,String tableName){ connection=c; this.tableName=tableName; if (!tableExists()) createTable(); } public void add(String key,Object o){ try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oout = new ObjectOutputStream(baos); oout.writeObject(o); oout.close(); String query="insert into "+tableName+" values (?,?)"; Msg.debug(query); PreparedStatement ps= connection.prepareStatement(query); ps.setString(1,key); ps.setBytes(2, baos.toByteArray()); ps.executeUpdate(); } catch (Exception e){ e.printStackTrace(); Msg.fatal("problem in persistant storage:"+e.getMessage()); } } public boolean tableExists(){ Statement stm=null; try { stm=connection.createStatement(); stm.execute("select count(*) from "+tableName); } catch (SQLException se){ return false; } return true; } public void createTable(){ Statement stm=null; try { stm=connection.createStatement(); stm.execute("DROP TABLE "+tableName); } catch (SQLException se){ Msg.debug("table does not exists"); } try { stm.execute("CREATE TABLE "+tableName+" (key text PRIMARY KEY,data bytea)"); Msg.debug("cpt2"); } catch (SQLException e){ Msg.debug("cpt3"+e.getMessage()); Msg.fatal(e); } } public void dropAll(){ Statement stm=null; try { stm=connection.createStatement(); stm.execute("drop table "+tableName); createTable(); } catch (SQLException se){ Msg.debug(se); } } public Object get(String key){ try { Statement stm=connection.createStatement(); String query="select data from "+tableName+" where key='"+key+"'"; Msg.debug(query); ResultSet rs=stm.executeQuery(query); rs.next(); byte[] buf = rs.getBytes(1); if (buf != null) { ObjectInputStream objectIn = new ObjectInputStream(new ByteArrayInputStream(buf)); return objectIn.readObject(); } else { Msg.fatal("Persistant storage: incorrect format."); return null; } } catch (Exception e){ Msg.fatal(e); return null; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -