datasource.java

来自「Java示例100」· Java 代码 · 共 61 行

JAVA
61
字号
/** * A Simple DataSource sample without using JNDI. * Please compare to DataSourceJNDI.java * * Please use jdk1.2 or later version */// You need to import the java.sql package to use JDBCimport java.sql.*;import javax.sql.*;import oracle.jdbc.*;import oracle.jdbc.pool.OracleDataSource;public class DataSource{  public static void main (String args [])    throws SQLException  {    // Create a OracleDataSource instance explicitly    OracleDataSource ods = new OracleDataSource();    // Set the user name, password, driver type and network protocol    ods.setUser("hr");    ods.setPassword("hr");    ods.setDriverType("oci8");    ods.setNetworkProtocol("ipc");    // Retrieve a connection    Connection conn = ods.getConnection();    getUserName(conn);    // Close the connection    conn.close();    conn = null;  }  static void getUserName(Connection conn)       throws SQLException  {    // Create a Statement    Statement stmt = conn.createStatement ();    // Select the USER column from the dual table    ResultSet rset = stmt.executeQuery ("select USER from dual");    // Iterate through the result and print the USER    while (rset.next ())      System.out.println ("User name is " + rset.getString (1));    // Close the RseultSet    rset.close();    rset =  null;    // Close the Statement    stmt.close();    stmt = null;  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?