accessdatasource.java~3~
来自「100多M的J2EE培训内容」· JAVA~3~ 代码 · 共 60 行
JAVA~3~
60 行
package com.cwj.DatabaseAccessDemo;
//运行本文件请确认是否已经将“Weblogic 8.x Client”添加至
import javax.naming.*;
import java.util.Properties;
import java.sql.*;
import java.util.*;
//import com.beasys.jndi.WLEInitialContextFactory;
import weblogic.jndi.*;
public class AccessDataSource {
javax.sql.DataSource myDataSource = null;
Connection conn = null;
Statement stat = null;
ResultSet result = null;
public void initialize() {
Context ctx = null;
Hashtable ht = new Hashtable();
ht.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
ht.put(Context.PROVIDER_URL, "t3://192.168.0.1:7001");
try {
ctx = new InitialContext(ht);
javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup("SQLServer");
java.sql.Connection conn = ds.getConnection();
// You can now use the conn object to create
// a Statement object to execute
// SQL statements and process result sets:
Statement stmt = conn.createStatement();
result = stmt.executeQuery("select * from customers");
// Do not forget to close the statement and connection objects
// when you are finished:
while (result.next()) {
System.out.println(result.getString(2));
}
stmt.close();
conn.close();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
ctx.close();
}
catch (Exception e) {
// a failure occurred
}
}
}
public static void main(String args[]) {
AccessDataSource obj = new AccessDataSource();
obj.initialize();
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?