如何不通过JNDI,使用DataSource连接DB2数据库
自己根据贴子上连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;
}
}