|
package common; import java.sql.*; import java.io.*; import javax.naming.*; import javax.sql.*; import java.util.*;
public class DbAction{ public Connection conn = null; public PreparedStatement stmt=null;
public Properties getProperties(String str){ Properties properties = new Properties(); try{ InputStream is =getClass().getResourceAsStream("/"+str); properties.load(is); if(is != null) is.close(); } catch(IOException ioexception){ System.out.println("Open config file failure."); } catch(NullPointerException e){ System.out.println("is is null"); } return properties; } public synchronized void DbConnect(){ String strCon=null; Properties properties = getProperties("datasource.properties"); String username = properties.getProperty("username"); String password = properties.getProperty("password"); String hostname = properties.getProperty("hostname"); String hostip = properties.getProperty("hostip"); String hostport = properties.getProperty("hostport");
try { Class.forName("oracle.jdbc.driver.OracleDriver"); } catch(ClassNotFoundException classnotfoundexception) { System.out.println("Could not load the driver."); classnotfoundexception.printStackTrace(); } strCon = "jdbc:oracle:thin:@"+hostip+":"+hostport+":"+hostname; try { conn = DriverManager.getConnection(strCon,username,password); } catch(SQLException sqlexception) { System.out.println("Creat connection error."); sqlexception.printStackTrace(); } // try{ // Context initContext = new InitialContext(); //连接池用 // Context envContext = (Context) initContext.lookup("java:/comp/env"); // DataSource ds = (DataSource) envContext.lookup("jdbc/oracle"); // conn = ds.getConnection(); // } // catch(NamingException ne){ // ne.printStackTrace(); // } // catch(SQLException se){ // se.printStackTrace(); // } } public String sqlSearch(String str){ String sql=null; Properties properties = getProperties("sql.properties"); sql = properties.getProperty(str); return sql; } public ResultSet dbSelect(String sql){ ResultSet rs=null; try{ &n [1] [2] [3] [4] 下一页 |