📄 example1.java
字号:
package jdbcexample;import java.sql.*;import javax.naming.*;import javax.sql.*;import java.util.Properties;import javax.rmi.PortableRemoteObject;public class Example1 { public static void main(String[] args) { DataSource ds = null;Context ctx = null;Connection myConn = null;try { /* 获得WebLogic ServerJNDI初始上下文信息 */ ctx = getInitialContext(); /* 建立数据源对象 */ ds = (javax.sql.DataSource) ctx.lookup("myDataSource");}catch (Exception E) { System.out.println("Init Error: " + E);}Statement myStatement=null;ResultSet myResultSet=null;try { //建立连接 myConn = ds.getConnection(); // 建立语句对象 myStatement = myConn.createStatement(); //建立结果集对象 myResultSet = myStatement.executeQuery( "SELECT full_name from employee" ); //遍历结果集对象,访问每一条记录,输出full_name字段 while(myResultSet.next()) { System.out.println("the employee full name is " + myResultSet.getString("full_name")); } //关闭结果集 myResultSet.close();}catch (SQLException e) { System.out.println("Error code = " + e.getErrorCode()); System.out.println("Error message = " + e.getMessage());}finally { try { // close the Statement object using the close() method if (myStatement != null) { myStatement.close(); } // close the Connection object using the close() method if (myConn != null) { myConn.close(); } } catch (SQLException e) { System.out.println("Error code = " + e.getErrorCode()); System.out.println("Error message = " + e.getMessage()); }} } private static Context getInitialContext() throws Exception { String url = "t3://localhost:7001"; String user = "system"; String password = "security"; Properties properties = null; try { properties = new Properties(); properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory"); properties.put(Context.PROVIDER_URL, url); if (user != null) { properties.put(Context.SECURITY_PRINCIPAL, user); properties.put(Context.SECURITY_CREDENTIALS, password == null ? "" : password); } return new InitialContext(properties); } catch(Exception e) { throw e; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -