如何不通过JNDI,使用DataSource连接DB2数据库

cheeloo 2002-03-15 03:57:38
自己根据贴子上连Oracle的例子,作了一下,没有成功。哪儿有问题呢?应该如何写呢?

/**
* A Simple DataSource sample without using JNDI.
*/

import java.sql.*;
import javax.sql.*;

import COM.ibm.db2.jdbc.*;

public class DataSource
{
public static void main (String args [])
throws SQLException
{
// Create a OracleDataSource instance explicitly
DB2DataSource ods = new DB2DataSource();

// Set the user name, password, driver type and network protocol
ods.setDatabaseName("PROJECT");


// Retrieve a connection
Connection conn = ods.getConnection("db2admin","admin");
getUserName(conn);
// Close the connection
conn.close();
conn = null;
}

static void getUserName(Connection conn)
throws SQLException
{
// Create a Statement
Statement stmt = conn.createStatement ();

// Select the ENAME column from the EMP table
ResultSet rset = stmt.executeQuery ("select col1 from test1");

// Iterate through the result and print the employee names
while (rset.next ())
System.out.println ("User name is " + rset.getString (1));

// Close the RseultSet
rset.close();
rset = null;

// Close the Statement
stmt.close();
stmt = null;
}
}


...全文
115 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
cheeloo 2002-03-31
  • 打赏
  • 举报
回复
应该可以,在已解决区里有联Oracle的例子,只不过我们用的是DB2。
zfluo 2002-03-31
  • 打赏
  • 举报
回复
你有没有看过提问的智慧的,什么叫做没有成功?
连接不到?取不到数据?还是其他的什么出错。
你连出了什么错都不给出来,别人怎么帮你啊?
怎么就是不肯花点时间学一下怎么提问,简直
就是在浪费大家的时间。
ahbill 2002-03-26
  • 打赏
  • 举报
回复
数据库性能的提高在于使用连接池
使用连接池在于配置DbPool和Data Source
程序中,一般都是通过JNDI查找DataSource对应的连接池对象啊?
这一百分,我是得不到了
关注!
cheeloo 2002-03-26
  • 打赏
  • 举报
回复
没有一个人能够回答吗?可是100分呢!
cheeloo 2002-03-26
  • 打赏
  • 举报
回复
没有一个人会回答吗?可是有100分呢!

23,404

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧