《J2EE应用开发(WebLogic+JBuilder)》代码

源代码在线查看: example1.java

软件大小: 1605 K
上传用户: MOMO89752118
关键词: WebLogic JBuilder J2EE 应用开发
下载地址: 免注册下载 普通下载 VIP

相关代码

				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;				         }				       }								}			

相关资源