⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rpairpersistantmap.java

📁 与postgresql数据库结合的数据库水印软件包
💻 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 java.util.*;import Watermill.kernel.*;public class RPairPersistantMap extends PairPersistantMap {    private Connection connection;    private String tableName;    public RPairPersistantMap(Connection c,String tableName){	connection=c;	this.tableName=tableName;	if (!tableExists())	    createTable();    }    public Vector getKeys(){	try {	    Vector res=new Vector();	    Statement stm=connection.createStatement();	    String query="select key1,key2 from "+tableName;	    Msg.debug(query);	    	    ResultSet rs=stm.executeQuery(query);	    while(rs.next()){		res.add(new Doublet(rs.getString("key1"),rs.getString("key2")));	    }	    return res;	}	catch (Exception e){	    Msg.fatal(e);	    return null;	}    }    public void add(String key1,String key2,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,key1);	    ps.setString(2,key2);	    ps.setBytes(3, 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+" (key1 text, key2 text,data bytea,primary key (key1,key2))");	    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 key1,String key2){	try {	    Statement stm=connection.createStatement();	    String query="select data from "+tableName+" where key1='"+key1+"' and key2='"+key2+"'";	    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 + -