📄 database.java
字号:
package com.sxit.wap.threads;import java.sql.*;import java.util.*;public class Database { public final static String URL = "jdbc:oracle:thin:@10.0.0.67:1521:ora8"; public final static String USER = "SXITWAP"; public final static String PASSWORD = "SXITWAP"; public final static int ORACLE = 0; public final static int SQLSERVER = 1; public final static int MYSQL = 2; public static int dbType = ORACLE; public Database() { } public static Connection getConnection() throws Exception { Connection conn = null; try { Class.forName ( "oracle.jdbc.driver.OracleDriver" ); conn = DriverManager.getConnection ( URL, USER, PASSWORD ); } catch (Exception exe) { exe.printStackTrace(); System.out.println("can not getconnection"); throw new Exception("can not getconnection"); } return conn; } public static int updateBySql(String sql) throws Exception { Connection dbConnection = null; Statement stmt = null; try { dbConnection = getConnection(); stmt = dbConnection.createStatement(); return stmt.executeUpdate(sql); } catch(SQLException e) { throw new Exception("SQLException while execute updateBySql mothod :\n" + e); } finally { closeStatement(stmt); closeConnection(dbConnection); } } public static int updateBySql(String sql, Connection conn) throws Exception { Statement stmt = null; try { stmt = conn.createStatement(); return stmt.executeUpdate(sql); } catch(SQLException e) { throw new Exception("SQLException while execute updateBySql mothod :\n" + e); } finally { closeStatement(stmt); } } public static int getRowCountBySql(String sql) throws Exception { Connection dbConnection = null; Statement stmt = null; ResultSet rs = null; int rowCount = 0; try { dbConnection = getConnection(); stmt = dbConnection.createStatement(); rs = stmt.executeQuery(sql); if ( rs.next() ) { rowCount = rs.getInt(1); }else{ throw new Exception("ERROR in get row count!!"); } return rowCount; } catch(Exception e) { throw new Exception("SQLException while execute getRowCountBySql mothod :\n" + e); } finally { closeResultSet(rs); closeStatement(stmt); closeConnection(dbConnection); } } public static Collection queryBySql(String sql) throws Exception { Connection dbConnection = null; Statement stmt = null; Collection coll = new ArrayList(); try { dbConnection = getConnection(); stmt = dbConnection.createStatement(); ResultSet rs = stmt.executeQuery(sql); while(rs.next()){ UserSubModel user=new UserSubModel(); user.setUserMdn(rs.getString(1)); user.setChannelId(rs.getInt(2)); user.setFeeType(rs.getInt(3)); user.setFeeCode(rs.getInt(4)); coll.add(user); } return coll; } catch(SQLException e) { throw new Exception("SQLException while execute updateBySql mothod :\n" + e); } finally { closeStatement(stmt); closeConnection(dbConnection); } } public static void closeConnection(Connection dbConnection) throws Exception { try { if (dbConnection != null && !dbConnection.isClosed()) { dbConnection.close(); dbConnection = null; } } catch (SQLException se) { } } public static void closeResultSet(ResultSet result) throws Exception { try { if (result != null) { result.close(); } } catch (SQLException se) { } } public static void closeStatement(Statement stmt) throws Exception { try { if (stmt != null) { stmt.close(); } } catch (SQLException se) { } } public static void main(String[] args) { Database database = new Database(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -