Examples.java
资源名称:某公司的java培训教材 [点击查看]
上传用户:dinglihq
上传日期:2013-02-04
资源大小:99958k
文件大小:7k
源码类别:
Java编程
开发平台:
Java
- package bible.jdbc;
- import java.sql.*;
- import java.util.Properties;
- import javax.naming.*;
- /**
- * Class Examples
- *
- *
- * @author
- * @version %I%, %G%
- */
- public class Examples {
- /**
- * Method getDirectConnection
- *
- *
- * @param whichDB
- * @param serverName
- * @param databaseName
- * @param userName
- * @param password
- *
- * @return
- *
- */
- public static Connection getDirectConnection(String whichDB,
- String serverName, String databaseName, String userName,
- String password) {
- Driver myDriver = null;
- Connection myConnection = null;
- String driverClassName = null;
- String driverURL = null;
- if (whichDB.equals("SQLServer")) {
- driverClassName = "weblogic.jdbc.mssqlserver4.Driver";
- driverURL = "jdbc:weblogic:mssqlserver4";
- } else if (whichDB.equals("Oracle")) {
- driverClassName = "weblogic.jdbc.oci.Driver";
- driverURL = "jdbc:weblogic:oracle";
- }
- try {
- myDriver = (Driver) Class.forName(driverClassName).newInstance();
- Properties myProperties = new Properties();
- myProperties.put("user", userName);
- myProperties.put("password", password);
- myProperties.put("server", serverName);
- myProperties.put("db", databaseName);
- myConnection = myDriver.connect(driverURL, myProperties);
- System.out.println("Direct connection via " + whichDB + ":"
- + serverName + " obtained.");
- } catch (Exception e) {
- e.printStackTrace();
- }
- return myConnection;
- }
- /**
- * Method getPooledConnection
- *
- *
- * @param poolName
- * @param dsJNDIName
- *
- * @return
- *
- */
- public static Connection getPooledConnection(String poolName,
- String dsJNDIName) {
- Driver myDriver = null;
- Connection myConnection = null;
- javax.sql.DataSource myDataSource = null;
- if (poolName.equals("")) {
- try {
- Context myContext = new InitialContext();
- myDataSource = (javax.sql.DataSource) myContext.lookup(dsJNDIName);
- } catch (Exception e) {
- e.printStackTrace();
- }
- try {
- myConnection = myDataSource.getConnection();
- System.out.println("Pooled connection via " + dsJNDIName
- + " obtained.");
- return myConnection;
- } catch (Exception e) {
- e.printStackTrace();
- }
- } else {
- try {
- myDriver =
- (Driver) Class.forName("weblogic.jdbc.pool.Driver").newInstance();
- Properties myProperties = new Properties();
- myProperties.put("connectionPoolID", poolName);
- myConnection = myDriver.connect("jdbc:weblogic:pool", myProperties);
- System.out.println("Pooled connection via " + poolName
- + " obtained.");
- return myConnection;
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- return null;
- }
- /**
- * Method getJTSConnection
- *
- *
- * @param poolName
- *
- * @return
- *
- */
- public static Connection getJTSConnection(String poolName) {
- Connection myConnection = null;
- try {
- Driver myDriver =
- (Driver) Class.forName("weblogic.jdbc.jts.Driver").newInstance();
- myConnection = myDriver.connect("jdbc:weblogic:jts:" + poolName, null);
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- return myConnection;
- }
- }
- /**
- * Method getOracleData
- *
- *
- * @param theConnection
- *
- */
- public static void getOracleData(Connection theConnection) {
- try {
- Statement myStatement = theConnection.createStatement();
- ResultSet rs =
- myStatement.executeQuery("SELECT * FROM EMP ORDER BY EMPNO");
- while (rs.next()) {
- System.out.println(rs.getInt("EMPNO") + " "
- + rs.getString("ENAME") + " "
- + rs.getString("JOB"));
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * Method getOracleData
- *
- *
- */
- public static void getOracleData() {
- Connection myConnection = null;
- myConnection = getPooledConnection("OraclePool", "");
- getOracleData(myConnection);
- closeConnection(myConnection);
- }
- /**
- * Method getSQLServerData
- *
- *
- * @param theConnection
- *
- */
- public static void getSQLServerData(Connection theConnection) {
- try {
- Statement myStatement = theConnection.createStatement();
- ResultSet rs =
- myStatement.executeQuery("select * from authors");
- while (rs.next()) {
- System.out.println(rs.getString("au_id") + " "
- + rs.getString("au_lname") + " "
- + rs.getString("au_fname"));
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * Method closeConnection
- *
- *
- * @param theConnection
- *
- */
- public static void closeConnection(Connection theConnection) {
- try {
- theConnection.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * Method main
- *
- *
- * @param args
- *
- */
- public static void main(String args []) {
- if (false) {
- Connection myConnection = null;
- myConnection = getDirectConnection("Oracle", "ORADEV1.ZEEWARE.COM", "",
- "SCOTT", "tiger");
- getOracleData(myConnection);
- closeConnection(myConnection);
- myConnection = getPooledConnection("OraclePool", "");
- getOracleData(myConnection);
- closeConnection(myConnection);
- myConnection = getPooledConnection("", "OraclePoolDataSource");
- getOracleData(myConnection);
- closeConnection(myConnection);
- myConnection = getDirectConnection("SQLServer", "192.168.0.1", "pubs",
- "sa", "password");
- getSQLServerData(myConnection);
- closeConnection(myConnection);
- myConnection = getPooledConnection("SQLServerPool", "");
- getSQLServerData(myConnection);
- closeConnection(myConnection);
- myConnection = getPooledConnection("", "SQLServerPoolDataSource");
- getSQLServerData(myConnection);
- closeConnection(myConnection);
- }
- }
- }
- /*--- Formatted in Bible Style on Thu, Sep 6, '01 ---*/
- /*------ Formatted by Jindent 3.24 Gold 1.02 --- http://www.jindent.de ------*/