PreparedStatement stmt = null;
ResultSet rs = null;
String sql;
String m_password="";
sql = "SELECT"
+ " DB2ADMIN.YHDL.PWD"
+ " FROM"
+ " DB2ADMIN.YHDL"
+ " WHERE"
+ " ("
+ " ( "
+ " DB2ADMIN.YHDL.ID = '"+m_id.trim()+"'"
+ " )"
+ " )";
stmt = con.prepareStatement( sql );
rs = stmt.executeQuery();
// Access query results
while (rs.next())
{
m_password=rs.getString(1);
m_password=m_password.trim();
if (rs.wasNull())
System.out.print("NULL");
else
System.out.print(m_password);
}
if(m_password.equals(m_pwd.trim()))
{
result[0] =1;
}
else
{
result[0] =0;
}
// close open resources
if (rs != null) rs.close();
if (stmt != null) stmt.close();
if (con != null) con.close();
// set return parameter
//result[0] = result[0];
}
}
JAVA数据库链接(JDBC)
DB2 的Java 支持包括JDBC,一个以供应商为中心的动态SQL 接口,它通过标准的Java方法提供对应用程序的数据存取。JDBC 与DB2 CLI 相似之处在于您不必预编译或联编JDBC 程序。使用JDBC 编写的应用程序只使用动态SQL。
JDBC编程步骤:
1建立与数据库的连接:
Class.forName("Com.ibm.db2.jdbc.net.DB2Driver");
connection con=DriverManager.getConnection(url);
2.创建Statement对象:
Statement stmt=con.createStatement();
3执行查询语句:
ResultSet rs=stmt.execQuery("SQL语句");
4.获取和设置选项:
ResultSetMetaData rsmd=rs.getMetaData();
int numCols=rsmd.getColumnCount()获取结果集总列数;
rsmd.getColumnLabel(i))获取记录值;
setMaxRows :设置结果集能容纳的最多行数.
setQueryTimeout:设置一个语句执行等待的时间.
setEscapeProcessing:通知驱动程序如何处理转义字符.
5.关闭Statement
stmt.clost();
调用层接口(CLI)
CLI不是一种新的查询语言,它只不过是应用程序可利用SQL语句去提交事务处理的一种简单接口,对数据库的查询和修改,仍要使用SQL语言编写,包括CLI函数的调用。
调用层接口(CLI)为DB2所提供的处理动态SQL语句的两种机制之一,即在应用程序首次运行时,动态SQL语句提交给数据库系统,CLI依赖一组函数调用,可嵌入主语言中.