刀剑神域桐人搞亚丝娜:懂weblogic的来

来源:百度文库 编辑:高校问答 时间:2024/04/28 05:49:24
这我照书打的为什么说我JNDI找不到
我的web启动了并且DataSourc也都配好了
请高手指教
package jdbcexample;

import java.sql.*;
import javax.naming.*;
import javax.sql.*;
import java.util.Properties;
import javax.rmi.PortableRemoteObject;

public class Example2 {
public Example2() {
try {
jbInit();
}
catch (Exception ex) {
ex.printStackTrace();
}
}

public static void main(String[] args) {
DataSource ds = null;
Context ctx = null;
Connection myConn = null;
try {
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 * from employee");

for (int j = 1; j <= myResultSet.getMetaData().getColumnCount(); j++) {
System.out.print(myResultSet.getMetaData().getColumnName(j) + "\t");
}
System.out.println();
while (myResultSet.next()) {
for (int j = 1; j <= myResultSet.getMetaData().getColumnCount(); j++) {
System.out.print(myResultSet.getObject(j) + "\t");
}
System.out.println();
}
myResultSet.close();
}
catch (SQLException e) {
System.out.println("Error code = " + e.getErrorCode());
System.out.println("Error message = " + e.getMessage());
}
finally {
try {
if (myStatement != null) {
myStatement.close();
}
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 = "weblogic";
String password = "weblogic";
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;
}
}

private void jbInit() throws Exception {
}

}