PHP 代码:
package com.ljsilver.util; import java.sql.*;
public class DatabaseOperate{ private String Database_host = "localhost:3306"; private String Database_dsn = "mydb"; private String Database_user = "user"; private String Database_pswd = "password"; private Connection con = null; private PreparedStatement ps = null; private ResultSet result = null; private boolean dirty = false; private String sqlStr; private String url = "jdbc:mysql://"+Database_host+"/"+Database_dsn+"?user=" +Database_user+"&password="+Database_pswd +"&useUnicode=true&characterEncoding=GBK";
public DatabaseOperate(String sqlStr){ this.sqlStr =sqlStr; buildConnection(); }
public DatabaseOperate(){ }
public void buildConnection(){ try{ Class.forName("org.gjt.mm.mysql.Driver").newInstance(); con = DriverManager.getConnection(url); ps=con.prepareStatement(this.sqlStr, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); } catch(Exception e){ System.out.println(e.toString()); } }
public void setSqlStr(String s){ this.sqlStr = s; }
public String getSqlStr(){ return this.sqlStr; }
public void setString(int n,String sqlString) throws SQLException{ ps.setString(n,sqlString); }
public void setInt(int n,int sqlInt) throws SQLException{ ps.setInt(n,sqlInt); }
public void setBoolean(int i, boolean flag) throws SQLException{ ps.setBoolean(i, flag); }
public void setDate(int i, Date date) throws SQLException{ ps.setDate(i, date); }
public void setLong(int i, long l) throws SQLException{ ps.setLong(i, l); }
public void setFloat(int i, float f) throws SQLException{ ps.setFloat(i, f); }
public void setBytes(int i, byte abyte0[]) throws SQLException{ ps.setBytes(i, abyte0); }
public ResultSet getResultSet(){ try{ this.result = ps.executeQuery(); }catch(SQLException sqlex){ System.out.println(sqlex.toString()); } return this.result; }
public void executeSql(){ try{ ps.executeUpdate(); dirty = true; }catch(SQLException sqlex){ System.out.println(sqlex.toString()); } }
public boolean getDirty(){ return this.dirty; }
public void close(){ try{ if(result!=null){ result.close(); result=null; } if(ps!=null){ ps.close(); ps=null; } if(con!=null){ con.close(); con=null; } }catch(SQLException sqlex){ System.out.println(sqlex.toString()); } } }
本文出自 51CTO.COM技术博客 |