Topic: jsp+weblogic连接oracle数据库的几种方法 |
Print this page |
1.jsp+weblogic连接oracle数据库的几种方法 | Copy to clipboard |
Posted by: li_chuansong Posted on: 2003-07-22 12:52 1.通过oracle自带的jdbc即thin方法 须在weblogic下startWebLogic.cmd中set CLASSPATH=下加入oracle/jdbc驱动classes12.jar // 建立数据库的连接 // package com.yourCompany.yourApp; package db_beans; import javax.sql.DataSource; import javax.naming.*; import java.lang.*; import java.sql.*; import java.io.*; import java.util.*; public class connect_db_oracle{ public Connection con = null; public ResultSet ret=null; public Statement stmt=null; //此连接为采用oracle的jdbc直连接 public connect_db_oracle(){ try{ String dbdrive = "oracle.jdbc.driver.OracleDriver"; String dburl = "jdbc:oracle:thin:@qdgs-lcs-note:1521:study"; String userName = "cygs"; String pass = "cygs"; try{ DriverManager.registerDriver((java.sql.Driver)Class.forName(dbdrive).newInstance()); con= DriverManager.getConnection(dburl, userName, pass); }catch(Exception e){ System.out.println(e.getMessage()+e.toString()); } }catch(Exception ex){ ex.printStackTrace(); System.out.print(ex.toString()); return; } } //执行select语句,并返回结果集 public ResultSet execsql(String sql){ try { stmt=con.createStatement(); ret = stmt.executeQuery(sql); }catch(Exception e){ e.printStackTrace(); System.out.println(e.toString()); } return ret; } //执行insert,update,delete,ddl等,返回结果数或0 public int execupdate(String sql){ int recount=0; try { stmt=con.createStatement(); recount = stmt.executeUpdate(sql); }catch(Exception e){ e.printStackTrace(); System.out.println(e.toString()); } return recount; } //关闭打开的内容 public void closecon(){ try{ if (ret != null){ ret.close(); } if (stmt != null){ stmt.close(); } if (con != null){ con.close(); } }catch(Exception ex){ ex.printStackTrace(); } } } |
2.Re:jsp+weblogic连接oracle数据库的4种方法 [Re: li_chuansong] | Copy to clipboard |
Posted by: li_chuansong Posted on: 2003-07-23 08:54 2.采用weblogic的jdbc连接oracle // 建立数据库的连接 // package com.yourCompany.yourApp; package db_beans; import javax.sql.DataSource; import javax.naming.*; import java.lang.*; import java.sql.*; import java.io.*; import java.util.*; public class connect_db{ public Connection con = null; public ResultSet ret=null; public Statement stmt=null; //此连接为采用weblogic的jdbc直连接 public connect_db(){ try{ Properties props = new Properties(); props.put("user","cygs"); props.put("password","cygs"); props.put("server","study"); try{ Driver myDriver = (Driver) Class.forName("weblogic.jdbc.oci.Driver").newInstance(); con = myDriver.connect("jdbc:weblogic:oracle", props); }catch(Exception e){ System.out.println(e.getMessage()+e.toString()); } }catch(Exception ex){ ex.printStackTrace(); System.out.print(ex.toString()); return; } } //执行select语句,并返回结果集 public ResultSet execsql(String sql){ try { stmt=con.createStatement(); ret = stmt.executeQuery(sql); }catch(Exception e){ e.printStackTrace(); System.out.println(e.toString()); } return ret; } //执行insert,update,delete,ddl等,返回结果数或0 public int execupdate(String sql){ int recount=0; try { stmt=con.createStatement(); recount = stmt.executeUpdate(sql); }catch(Exception e){ e.printStackTrace(); System.out.println(e.toString()); } return recount; } //关闭打开的内容 public void closecon(){ try{ if (ret != null){ ret.close(); } if (stmt != null){ stmt.close(); } if (con != null){ con.close(); } }catch(Exception ex){ ex.printStackTrace(); } } } 3.采用weblogic的连接池jdbc用oracle thin // 建立数据库的连接 // package com.yourCompany.yourApp; package db_beans; import javax.sql.DataSource; import javax.naming.*; import java.lang.*; import java.sql.*; import java.io.*; import java.util.*; public class connect_pool_oracle{ public Connection con = null; public ResultSet ret=null; public Statement stmt=null; //此连接为采用weblogic的jdbc直连接 public connect_pool_oracle(){ try{ Properties props = new Properties(); props.put("connectionPoolID", "WebLogicOraclePool"); try{ Driver myDriver = (Driver)Class.forName("weblogic.jdbc.pool.Driver").newInstance(); con =myDriver.connect("jdbc:weblogic:pool", props); }catch(Exception e){ System.out.println(e.getMessage()+e.toString()); } }catch(Exception ex){ ex.printStackTrace(); System.out.print(ex.toString()); return; } } //执行select语句,并返回结果集 public ResultSet execsql(String sql){ try { stmt=con.createStatement(); ret = stmt.executeQuery(sql); }catch(Exception e){ e.printStackTrace(); System.out.println(e.toString()); } return ret; } //执行insert,update,delete,ddl等,返回结果数或0 public int execupdate(String sql){ int recount=0; try { stmt=con.createStatement(); recount = stmt.executeUpdate(sql); }catch(Exception e){ e.printStackTrace(); System.out.println(e.toString()); } return recount; } //关闭打开的内容 public void closecon(){ try{ if (ret != null){ ret.close(); } if (stmt != null){ stmt.close(); } if (con != null){ con.close(); } }catch(Exception ex){ ex.printStackTrace(); } } } 4.采用weblogic的连接池用weblogic的jdbc // 建立数据库的连接 // package com.yourCompany.yourApp; package db_beans; import javax.sql.DataSource; import javax.naming.*; import java.lang.*; import java.sql.*; import java.io.*; import java.util.*; public class connect_db_pool{ public Connection con = null; public ResultSet ret=null; public Statement stmt=null; //此连接为采用weblogic的jdbc直连接 public connect_db_pool(){ try{ Properties props = new Properties(); props.put("connectionPoolID", "WebLogicPool"); try{ Driver myDriver = (Driver)Class.forName("weblogic.jdbc.pool.Driver").newInstance(); //Driver myDriver = (Driver) Class.forName("weblogic.jdbc.oci.Driver").newInstance(); //con = myDriver.connect("jdbc:weblogic:oracle", props); con =myDriver.connect("jdbc:weblogic:pool", props); }catch(Exception e){ System.out.println(e.getMessage()+e.toString()); } }catch(Exception ex){ ex.printStackTrace(); System.out.print(ex.toString()); return; } } //执行select语句,并返回结果集 public ResultSet execsql(String sql){ try { stmt=con.createStatement(); ret = stmt.executeQuery(sql); }catch(Exception e){ e.printStackTrace(); System.out.println(e.toString()); } return ret; } //执行insert,update,delete,ddl等,返回结果数或0 public int execupdate(String sql){ int recount=0; try { stmt=con.createStatement(); recount = stmt.executeUpdate(sql); }catch(Exception e){ e.printStackTrace(); System.out.println(e.toString()); } return recount; } //关闭打开的内容 public void closecon(){ try{ if (ret != null){ ret.close(); } if (stmt != null){ stmt.close(); } if (con != null){ con.close(); } }catch(Exception ex){ ex.printStackTrace(); } } } |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |