Connection conn = null; Statement stmt = null; ResultSet rs = null; // 1. load driver try {Class. forName ("com. ibm. db2.jcc. DB2Driver ");/* Driver driver = new com. ibm. db2.jcc. DB2Driver (); DriverManager. registerDriver (driver); // This is of little significance, because the class has already created the DB2Driver object during loading and registered the */} catch (Exception e) with DriverManager) {e. printStackTrace ();} String url = "jdbc: db2: // 10.10.38.138: 50000/malltest"; String username = "db2inst1"; String p Assword = "db2inst1"; try {// 2. create Database Connection conn = DriverManager. getConnection (url, username, password); // 3. obtains the database operation object stmt = conn. createStatement (); // 4. obtain the result set rs = stmt.exe cuteQuery ("select * from ly. t_merinf where merid = 'm0000178' "); // 5. processing result set while (rs. next () {System. out. println (rs. getString ("mername") ;}} catch (SQLException e) {e. printStackTrace ();} finally {// close the result set if (rs! = Null) {try {rs. close () ;}catch (SQLException e) {}}// close the database operation object if (stmt! = Null) {try {stmt. close () ;}catch (SQLException e) {}}// close the database connection if (conn! = Null) {try {conn. close () ;}catch (SQLException e ){}}}
// 1. create a connection pool DataSource ds = null; try {Properties prop = new Properties (); // load the property file prop through the class path. load (DbcpTest. class. getClassLoader (). getResourceAsStream ("database/dbcp. properties "); // obtain the data source ds = BasicDataSourceFactory. createDataSource (prop);} catch (IOException e) {e. printStackTrace ();} catch (Exception e) {e. printStackTrace ();} Connection conn = null; Statement stmt = null; ResultSet rs = null; try {/ /2. obtain database connection conn = ds. getConnection (); // 3. create Database Operation object stmt = conn. createStatement (); // 4. obtain the result set rs = stmt.exe cuteQuery ("select * from ly. t_merinf where merid = 'm0000178' "); // 5. processing result set while (rs. next () {System. out. println (rs. getString ("mername") ;}} catch (SQLException e) {e. printStackTrace ();} finally {// close the result set if (rs! = Null) {try {rs. close () ;}catch (SQLException e) {}}// close the database operation object if (stmt! = Null) {try {stmt. close () ;}catch (SQLException e) {}}// close the database connection if (conn! = Null) {try {conn. close () ;}catch (SQLException e ){}}}
driverClassName=com.ibm.db2.jcc.DB2Driverurl=jdbc:db2://10.10.38.138:50000/malltestusername=db2inst1password=db2inst1initialSize=3maxActive=5maxIdle=3minIdle=1maxWait=30000
ComboPooledDataSource CPPS = new ComboPooledDataSource (); // load the database driver try {CPPS. setDriverClass ("com. ibm. db2.jcc. DB2Driver ");} catch (PropertyVetoException e1) {e1.printStackTrace ();} // set the address, user name, and password for accessing the database. setJdbcUrl ("jdbc: db2: // 10.10.38.138: 50000/malltest"); CPPS. setUser ("db2inst1"); CPPS. setPassword ("db2inst1"); // you can specify some C3P0 configurations. If this parameter is not set, the default value cpd0. setMinPoolSize (5); CPPS. setAcquireIncrement (5); CPPS. setMaxPool Size (20); CPPS. setMaxStatements (180); Connection conn = null; Statement stmt = null; ResultSet rs = null; try {// create a database Connection conn = CPPS. getConnection (); // get the database operation object stmt = conn. createStatement (); // The result set rs = stmt.exe cuteQuery ("select * from ly. t_merinf where merid = 'm0000178' "); // process the result set while (rs. next () {System. out. println (rs. getString ("mername") ;}} catch (SQLException e) {e. printStackTrace ();} finall Y {// close the result set if (rs! = Null) {try {rs. close () ;}catch (SQLException e) {}}// close the database operation object if (stmt! = Null) {try {stmt. close () ;}catch (SQLException e) {}}// close the database connection if (conn! = Null) {try {conn. close ();} catch (SQLException e) {}} try {CES. destroy (CPPS);} catch (SQLException e) {e. printStackTrace ();}}