jdbc连接db2实现

1.

 

 

无法在request间传值

               //response.sendRedirect("datagrid.jsp");

 

               可在request间传值

RequestDispatcher rd =request.getRequestDispatcher("datagrid.jsp");

rd.forward(request, response);

2.配置c 标签

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_4.xsd" id="WebApp_ID" version="2.4">

  <display-name>do</display-name>

  <welcome-file-list>

    <welcome-file>index.htm</welcome-file>

    <welcome-file>index.jsp</welcome-file>

  </welcome-file-list>

  <taglib>  

  <taglib-uri>http://java.sun.com/jstl/core</taglib-uri>  

  <taglib-location>/WEB-INF/tld/c.tld</taglib-location>  

</taglib>  

  <servlet>

    <servlet-name>ViewUserInfo</servlet-name>

    <servlet-class>com.butone.servlet.ViewUserInfo</servlet-class>

  </servlet>

  <servlet-mapping>

    <servlet-name>ViewUserInfo</servlet-name>

    <url-pattern>/ViewUserInfo</url-pattern>

  </servlet-mapping>

 

 

</web-app>

 

获得值

<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %> 


<c:forEach items="${list}" var="list">

 

 <c:out value="${list.ID}" /><c:out value="${list.USERNAME}" /><br/>

 

</c:forEach>

 

3.db2 8 jdbc

/*

 * 创建日期 2012-5-19

 *

 * 更改所生成文件模板为

 * 窗口 > 首选项 > Java > 代码生成 > 代码和注释

 */

package com.butone.common;

 

/**

 * @author Administrator

 *

 * 更改所生成类型注释的模板为

 * 窗口 > 首选项 > Java > 代码生成 > 代码和注释

 */

import java.io.IOException;

import java.io.InputStream;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.ResultSetMetaData;

import java.sql.SQLException;

import java.util.ArrayList;

import java.util.Collections;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import java.util.Properties;

 

public class CommonJdbc {

 

// jdbc连接对象

private Connection conn;

// 操作对象

private PreparedStatement pst;

// 结果集对象

private ResultSet rs;

 

// 配置文件对象

// 返回配置文件对象

public static Properties getProperties() {

Properties prop = new Properties();

InputStream in = null;

try {

in = CommonJdbc.class.getResourceAsStream("jdbcconfig.properties");

// in =

// TestMysql.class.getResourceAsStream("jdbc_mysqlCofing.properties");

prop.load(in);

} catch (Exception e) {

e.printStackTrace();

return null;

} finally {

 

if (in != null) {

try {

in.close();

} catch (IOException eio) {

eio.printStackTrace();

}

}

}

return prop;

}

 

/*

* 打开数据库并创建连接对象 true 成功 false失败

*/

public boolean openConn() {

boolean isPass = false;

// 从配置文件获得jdbc链接

Properties props = getProperties();

 

String driver = props.getProperty("driver");

String url = props.getProperty("url");

String username = props.getProperty("username");

String password = props.getProperty("password");

try {

Class.forName(driver);

conn = DriverManager.getConnection(url, username, password);

isPass = true;

} catch (ClassNotFoundException nfe) {

closeAll();

nfe.printStackTrace();

System.out.println("Database connection false!");

isPass = false;

} catch (SQLException se) {

closeAll();

se.printStackTrace();

isPass = false;

}

return isPass;

}

 

/*

* 新增和修改 操作一张表

*/

public boolean excuteUpdate(String sql) {

boolean isPassed = false;

// 判断数据库是否连通

if (openConn()) {

try {

conn.setAutoCommit(false);

pst = conn.prepareStatement(sql);

pst.execute();

conn.commit();

isPassed = true;

} catch (SQLException e) {

// TODO Auto-generated catch block

System.out.print("SQL:" + sql);

e.printStackTrace();

} finally {

closeAll();

}

}

return isPassed = true;

}

 

/*

* 新增或者更新多个表 数组

*/

public boolean excuteUpdate(String[] sql) {

boolean isPassed = false;

if (openConn()) {

 

try {

conn.setAutoCommit(false);

for (int i = 0; i < sql.length; i++) {

pst = conn.prepareStatement(sql[i]);

pst.execute();

conn.commit();

isPassed = true;

}

 

} catch (SQLException e) {

// TODO Auto-generated catch block

try {

conn.rollback();

} catch (SQLException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

for (int i = 0; i < sql.length; i++) {

System.out.println("SQL:" + sql);

}

e.printStackTrace();

} finally {

closeAll();

}

}

return isPassed;

}

 

/*

* 新增或者更新多个表 List

*/

public boolean excuteUpdate(List sql) {

boolean isPassed = false;

if (openConn()) {

 

try {

conn.setAutoCommit(false);

for (int i = 0; i < sql.size(); i++) {

pst = conn.prepareStatement(sql.get(i).toString());

pst.execute();

conn.commit();

isPassed = true;

}

 

} catch (SQLException e) {

// TODO Auto-generated catch block

try {

conn.rollback();

} catch (SQLException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

for (int i = 0; i < sql.size(); i++) {

System.out.println("SQL:" + sql);

}

e.printStackTrace();

} finally {

closeAll();

}

}

return isPassed;

}

 

/*

* 返回结果集ResuletSet

*/

 

public ResultSet executeQuery(String sql) {

ResultSet rs = null;

if (openConn()) {

try {

pst = conn.prepareStatement(sql);

rs = pst.executeQuery();

} catch (SQLException e) {

// TODO Auto-generated catch block

closeAll();

System.out.print("SQL:" + sql);

System.out.println("ERROR:Databse disconnected!");

e.printStackTrace();

}

}

return rs;

}

 

/**

* get list 

* @param sql

* @return

*/

public List executeQueryList(String sql) {

ResultSet rs = null;

ArrayList list = new ArrayList();

if (openConn()) {

try {

pst = conn.prepareStatement(sql);

rs = pst.executeQuery();

if (rs == null)

return Collections.EMPTY_LIST;

// 结构信息,比如字段数、字段名等

ResultSetMetaData md = rs.getMetaData();

// 对象中的列数

int columnCount = md.getColumnCount();

Map rowData = new HashMap();

while (rs.next()) {

rowData = new HashMap(columnCount);

for (int i = 1; i <= columnCount; i++) {

rowData.put(md.getColumnName(i), rs.getObject(i));

}

list.add(rowData);

}

System.out.println("list:" + list.toString());

 

} catch (SQLException e) {

closeAll();

System.out.print("SQL:" + sql);

System.out.println("ERROR:Databse disconnected!");

e.printStackTrace();

}

}

return list;

}

 

/*

* closeAll()关闭所有数据库链接对象

*/

public void closeAll() {

if (conn != null) {

try {

conn.close();

conn = null;

} catch (SQLException sqle) {

sqle.printStackTrace();

}

}

if (pst != null) {

try {

conn.close();

conn = null;

} catch (SQLException sqle) {

sqle.printStackTrace();

}

}

if (rs != null) {

try {

rs.close();

rs = null;

} catch (SQLException sqle) {

sqle.printStackTrace();

}

}

}

 

}


jdbcconfig.properties

driver=com.ibm.db2.jcc.DB2Driver

url=jdbc:db2://localhost:50000/jmyxcs

username=erpyxusr 

password=erpyxusr 

 

4.db2 jar

db2jcc_license_cu.jar

db2jcc.jar

jstl.jar

standard.jar

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值