在Eclipse3.1.1访问Weblogic 9.0中数据源的JDBC简单应用

1,启动Eclipse,新建java项目命名为TestJDBC,并创建单独的源文件夹和输出文件夹.完成项目创建.
2,在项目根目录创建文件夹lib,将mysql-connector-java-5.0.7-bin.jar,%BEA_HOME%/weblogic90/server/lib/目录下weblogic.jar及xbean.jar复制到该文件夹下,右击项目,选择属性,在构建路径中,通过添加外部JAR添加类库文件.
3,新建类testJDBC,源程序如下:
package myjdbc;
 
import java.io.UnsupportedEncodingException;
import java.sql.*;
import javax.sql.*;
import java.util.*;
import javax.naming.*;
 
public class testJDBC {
 
       /**
        * @param args
        */
       public static void main(String[] args) {
              // TODO 自动生成方法存根
              DataSource ds=null;
              Context ctx=null;
              Connection con=null;
              Statement st=null;
              ResultSet rs=null;
              Properties p=new Properties();
              //Hashtable ht=new Hashtable();
             
              p.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
              p.put(Context.PROVIDER_URL,"t3://localhost:7001");
             
              try {
                     ctx=new InitialContext(p);
                            //ctx=new InitialContext(ht);  通过哈希表也可以初始化上下文
                     ds=(javax.sql.DataSource)ctx.lookup("MySQLDS");
                     //注:此处MySQLDS是Weblogic中配置的数据源JNDI名,具体配置详见另篇博文
                     con=ds.getConnection();
                     st=con.createStatement();
                     rs=st.executeQuery("select * from book");
                    
                     System.out.println("书号              作者              书名              价格");                 
                     while (rs.next()) {
                            System.out.print(rs.getString("id"));
                            try {
                                   System.out.print("         "+new String( rs.getString("author").getBytes("ISO-8859-1"),"GB2312"));
                                   System.out.print("         "+new String( rs.getString("name").getBytes("ISO-8859-1"),"GB2312"));
                            } catch (UnsupportedEncodingException e) {
                                   // TODO 自动生成 catch 块
                                   e.printStackTrace();
                            }
                            System.out.println("       "+rs.getInt("price"));
                           
                     }
                     rs.close();
              } catch (SQLException e) {
                     // TODO: handle exception
                     e.printStackTrace();
              }catch(NamingException e){
                     e.printStackTrace();
              }
              finally{
                     try {
                            if (st!=null) {
                                   rs.close();
                            }
                            if (con!=null) {
                                   con.close();
                            }
                     } catch (SQLException e) {
                            // TODO: handle exception
                            e.printStackTrace();
                     }
              }    
       }
 
}
4,启动MySQL及Weblogic服务器,运行testJDBC.java文件.控制台出现如下:
书号    作者    书名    价格
1001        Benson      家常菜1000例 25
1002        顾晓繁      天天美食 30
1003        戴美晨      饮食与美容   36
1004        艾中国      假日与旅游   42
本文欢迎转载,转载请注明原出处!
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页