关于彩信mms的程序
源代码在线查看: dbconnection.java~13~
package mmscenter; /** * Title: 旅游气象站 * Description: * Copyright: Copyright (c) 2003 * Company: 华风 * @author 刘聪 * @version 1.0 */ import java.sql.*; import java.util.*; import java.lang.*; public class DBConnection{ String sql=""; protected java.lang.String password= Config.db_password ; protected java.lang.String driver= Config.odbcstring ; protected java.lang.String Url= "";//"jdbc:odbc:travel"; protected java.lang.String userID= Config.db_user; //protected java.lang.String password= "weathercn"; //protected java.lang.String Url= "jdbc:oracle:thin:@127.0.0.1:1521:weather"; //protected java.lang.String userID= "weathercn"; Connection connection=null; Statement statement=null; public DBConnection(String db_name,String db_host){ this.password= Config.db_password ; this.driver= Config.odbcstring ; this.Url= "";//"jdbc:odbc:travel"; this.userID= Config.db_user; this.Url = "jdbc:mysql://"+db_host+":3306/"+db_name; } public void Close(){ try { closeStatement(); if(!connection.isClosed()) { connection.close(); } } catch(Throwable e){System.out.println(e);} connection = null; } private void closeStatement() { try { if(statement != null) statement.close(); } catch(Throwable e) { System.out.println("exception in closeStmt " + e); } statement=null; } public Connection getConnection() throws SQLException{ if(connection != null && !connection.isClosed()) { return connection; } try{ Class.forName(driver); //DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); connection = DriverManager.getConnection (Url, userID, password); } catch(Exception e) { System.out.println("exception in getConnection " + e.getMessage()); } return connection ; } public Statement getStatement() throws SQLException{ try { statement = getConnection().createStatement(); statement.setMaxFieldSize(16000); statement.setMaxRows(1000); } catch(SQLException e) { System.out.println("exception in getStmt " + e); throw e; }; return statement; } public Object execute(String sql) { System.out.println("execute"); java.sql.ResultSet rs = null; java.util.Vector vResult = null; try { if(!isSelect(sql)) { try { return new Integer(getStatement().executeUpdate(sql)); } catch(SQLException e1) { System.out.println(e1); return new Integer(-1 * e1.getErrorCode()); } } else { rs = getStatement().executeQuery(sql); int columnCount = rs.getMetaData().getColumnCount(); vResult = new Vector(); while(rs.next()) { java.util.Vector vTemp = new Vector(); for(int i = 0;i< columnCount;i++) { try { String data = rs.getString(i + 1); //去掉Encode的转换 if (data == null) vTemp.addElement(""); else vTemp.addElement(data); } catch (Exception e) { System.out.println(e.getMessage()); } } vResult.addElement(vTemp); } rs.close(); } closeStatement(); } catch(Throwable e1) { } return vResult; } public Integer executeUpdate(String sql) { return (Integer)execute(sql); } protected boolean isSelect(String psql) { String sql = psql.trim().toUpperCase(); if(sql.indexOf("SELECT ") return true; } public Vector executesql(String sql,int iPage,int iRows) { java.sql.ResultSet rs = null; java.util.Vector vResult=new Vector(); try { rs = getStatement().executeQuery(sql); int columnCount = rs.getMetaData().getColumnCount(); vResult = new Vector(); for(int j=0;rs.next();j++) { if((j >= iPage * iRows) && (j { java.util.Vector vTemp = new Vector(); for(int i = 0;i< columnCount;i++) { try { String data = rs.getString(i + 1); if (data == null) vTemp.addElement(""); else vTemp.addElement(data); } catch (Exception e) { System.out.println(e.getMessage()); } } vResult.addElement(vTemp); } else if (j >= ((iPage+1) * iRows - 1)) break; else continue; } rs.close(); statement.close(); } catch(Throwable e1) { } return vResult; } public static void main(String[] args) { DBConnection db = new DBConnection("test",Config.db_host); System.out.println(db.executeUpdate("insert into mms_content (is_forecast)values(1)")) ; } }