Chinaunix首页 | 论坛 | 博客
  • 博客访问: 114724
  • 博文数量: 31
  • 博客积分: 1216
  • 博客等级: 中尉
  • 技术积分: 300
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-26 17:34
文章分类

全部博文(31)

文章存档

2016年(1)

2011年(2)

2010年(7)

2009年(9)

2008年(12)

我的朋友

分类: Java

2008-10-05 21:13:56

JDBC API

连接数据库的四种方式
Type 1:JDBC-ODBC
Type 2:JDBC-Native
Type 3:JDBC-Net Bridge
Type 4:All Java JDBC Driver

1、直接访问数据库
import java.sql.*;
try{
   class.forName("com.pointbase.jdbc.jdbcUniversalDriver");
   Connection con=DriverManager.getConnect("jdbc:pointbase;//server:port/MyDatabase");
   Statement stmt=con.createStatement();
   String sql="select * from mytable";
   ResultSet res=stmt.executeQuery(sql);
   while(res.next()){
        String col1=res.getString("MYCOLUMN1");
        int col2=res.getInt("MYCOLUMN2");   
   }catch(Exception e){...}
}
连接池Connection Pool
消除频繁建立连接所需的负载
是用于管理数据库连接的管理对象
提供可共享,安全的连接
2、使用DataSource:
先在JNDI中查找到它
然后从DataSource获得一个连接
示例:
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
try{
   Context ctx=new InitialContext();
   DataSource ds=(DataSource)ctx.lookup("TestDataSource");
   Connection con=ds.getConnection();
   Statement stmt=con.createStatement();
   String sql="select * from mytable";
   ResultSet res=stmt.executeQuery("sql");
   while(res.next()){
         String col1=res.getString("MYCOLUMN1");
   }
   con.close();
}catch(Exception e){...}

阅读(736) | 评论(0) | 转发(0) |
0

上一篇:J2EE之JNDI

下一篇:门户网站运维abc

给主人留下些什么吧!~~