//这是web.xml的代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>application</param-name>
<param-value>ApplicationResources_ch</param-value>
</init-param>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>dbInit</servlet-name>
<servlet-class>cn.ty.Db.DBInitServlet</servlet-class>
<init-param>
<param-name>driverClass</param-name>
<param-value>org.gjt.mm.mysql.Driver</param-value>
</init-param>
<init-param>
<param-name>URL</param-name>
<param-value>jdbc:mysql://localhost:3306/gb?autoReconnect=true</param-value>
</init-param>
<init-param>
<param-name>maxActive</param-name>
<param-value>4</param-value>
</init-param>
<init-param>
<param-name>maxWait</param-name>
<param-value>5000</param-value>
</init-param>
<init-param>
<param-name>maxIdle</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>user</param-name>
<param-value>root</param-value>
</init-param><load-on-startup>1</load-on-startup><init-param>
<param-name>password</param-name>
<param-value></param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping> <filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>cn.ty.Share.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GB2312</param-value>
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<servlet-name>action</servlet-name>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- Struts Tag Library Descriptors -->
<taglib>
<taglib-uri>/bean</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib> <taglib>
<taglib-uri>/html</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib> <taglib>
<taglib-uri>/logic</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib> <taglib>
<taglib-uri>/template</taglib-uri>
<taglib-location>/WEB-INF/struts-template.tld</taglib-location>
</taglib>
</web-app>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>application</param-name>
<param-value>ApplicationResources_ch</param-value>
</init-param>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>dbInit</servlet-name>
<servlet-class>cn.ty.Db.DBInitServlet</servlet-class>
<init-param>
<param-name>driverClass</param-name>
<param-value>org.gjt.mm.mysql.Driver</param-value>
</init-param>
<init-param>
<param-name>URL</param-name>
<param-value>jdbc:mysql://localhost:3306/gb?autoReconnect=true</param-value>
</init-param>
<init-param>
<param-name>maxActive</param-name>
<param-value>4</param-value>
</init-param>
<init-param>
<param-name>maxWait</param-name>
<param-value>5000</param-value>
</init-param>
<init-param>
<param-name>maxIdle</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>user</param-name>
<param-value>root</param-value>
</init-param><load-on-startup>1</load-on-startup><init-param>
<param-name>password</param-name>
<param-value></param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping> <filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>cn.ty.Share.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GB2312</param-value>
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<servlet-name>action</servlet-name>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- Struts Tag Library Descriptors -->
<taglib>
<taglib-uri>/bean</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib> <taglib>
<taglib-uri>/html</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib> <taglib>
<taglib-uri>/logic</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib> <taglib>
<taglib-uri>/template</taglib-uri>
<taglib-location>/WEB-INF/struts-template.tld</taglib-location>
</taglib>
</web-app>
package cn.ty.Db;import java.sql.SQLException;
import java.sql.Connection;
import javax.sql.DataSource;
import java.lang.Integer;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.ServletConfig;import org.apache.commons.dbcp.BasicDataSourceFactory;
import org.apache.commons.dbcp.BasicDataSource;import cn.ty.Db.ConnectionPool;/**
* @author ty
*初始化连接池
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class DBInitServlet extends HttpServlet {
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
} /**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init(ServletConfig config) throws ServletException {
// Put your code here
super.init(config);
try{
BasicDataSource bDS = new BasicDataSource();
bDS.setDriverClassName(getInitParameter("driverClass"));
bDS.setUrl(getInitParameter("URL"));
bDS.setUsername(getInitParameter("user"));
bDS.setPassword(getInitParameter("password"));
bDS.setMaxActive(Integer.parseInt(getInitParameter("maxActive")));
bDS.setMaxIdle(Integer.parseInt(getInitParameter("maxIdle")));
bDS.setMaxWait(Integer.parseInt(getInitParameter("maxWait")));
bDS.getConnection();
ConnectionPool.init(bDS);
}catch(Exception e){
e.printStackTrace();
throw new ServletException("无法找到数据源");
}
}}
package cn.ty.Db;import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
/**
* @author ty
*连接池
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class ConnectionPool {
private DataSource ds;
private static ConnectionPool conPool; public ConnectionPool(DataSource ds) {
this.ds = ds;
}
public static void init(DataSource ds){
conPool = new ConnectionPool(ds);
}
public static ConnectionPool getInstance(){
if(conPool == null){
throw new IllegalStateException("未初始化连接池");
}
return conPool;
}
public Connection getConnection() throws SQLException {
return ds.getConnection();
}}
/*
* Created on 2005-6-3
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package cn.ty.Db;import cn.ty.Db.ConnectionPool;import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.Statement;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
/**
* @author ty
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class DBControl {
private Connection con = null;
private ConnectionPool getConPool;
private Statement stmt;
public DBControl(){
getConPool = ConnectionPool.getInstance();
getCon();//得到连接
try{
stmt = con.createStatement();
}catch(SQLException sex){
sex.printStackTrace();
}
}
private synchronized Connection getCon(){
try{
con = getConPool.getConnection();
}catch(Exception e){
e.printStackTrace();
System.out.println("无法连接");
return con = null;
}
return con;
}
/**
* 查询语句的执行
* @param mySql
* @return 数据集
*/
public ResultSet SelectSql(String mySql){
ResultSet rs = null;
try{
rs = stmt.executeQuery(mySql);
return rs;
}catch(Exception e){
System.out.println("sql错误:"+mySql);
System.out.println("数据库查询错误:"+e.getMessage());
return null;
}
}
/**
* 执行SQL语句:update、delete、insert into
* @param exeSql 要执行的sql语句
* @return如果返回true成功,false失败
*/
public boolean ExecuteSql(String exeSql){
boolean bReturn = false;
try{
stmt.execute(exeSql);
bReturn = true;
}catch(Exception e){
System.out.println("sql错误:"+exeSql);
System.out.println("数据库查询错误:"+e.toString());
}
return bReturn;
}
/**
* 用于得到记录数
* @param countSql
* @return
*/
public int RowCount(String countSql){
int rC = 0;
ResultSet rs = null;
try{
rs = stmt.executeQuery(countSql);
if(rs.next()){
rC = rs.getInt(1);
}
rs.close();
rs = null;
}catch(Exception e){
System.out.println("sql错误:"+countSql);
System.out.println("数据库查询错误:"+e.toString());
}finally{
if(rs != null){
try{
rs.close();
rs = null;
}catch(Exception e){
e.printStackTrace();
}
}
}
return rC;
}
/**
* 返回PreparedStatement对象
* @param sql
* @return
*/
public PreparedStatement getPrepareStmt(String sql){
PreparedStatement preStmt = null;
try{
preStmt = con.prepareStatement(sql);
}catch(Exception e){
e.printStackTrace();
}
return preStmt;
}
/**
* 必须调用
*
*/
public void close(){
if(con != null){
try{
con.close();
con = null;
}catch(Exception e){
e.printStackTrace();
}
}
if(stmt!= null){
try{
stmt.close();
stmt = null;
}catch(Exception e){
e.printStackTrace();
}
}
} }
如:
import cn.ty.Db.DBControl;
.....
DBControl db = new DBControl();
ResultSet rs = null;
try{
rs = db.selectSql("select * ....");
while(rs.next){
...
}
rs.close;
rs=null;
}catch(Exception e){
e....;
}finally{
try{
db.close;//记住要关闭
}catch(Exception e){}
}
以上就是我的那些东西,希望大家能帮我看看改改,谢谢
下面是我的代码,你可以借鉴下
* Created on 2005-3-30
*/
package com.milan.common;import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.CallableStatement;import com.milan.common.DB;/**
* <p>* Title: MILAN.CommonFUN* </p>
* <p>* Description: 实现数据库操作*</p>
* <p> * <strong>例1:事务型更新 </strong> * </p>
* <br> CommonFUN commonfun = new CommonFUN();
* <br> commonfun.setAutoCommit(false);
* <br> commonfun.executeQuery("insert into orders values(11,11,1,1,null)");
* <br> commonfun.commit();</br>
* <p> * <strong>例2:非事务型更新 </strong> * </p>
* <br> CommonFUN commonfun = new CommonFUN();
* <br> commonfun.executeQuery("insert into orders values(22,22,1,1,null)");
* <br> //此时已提交到数据库
* <p>
* <strong>例3:预编译型查询 </strong>
* </p>
* <br> commonfun.prepareStatement("select user_id, order_id from t_orders where order_id =?");
* <p> //欲使结果集可前后滚动时调用本语句</p>
* <br>commonfun.setResultSetScrollType(ResultSet.TYPE_SCROLL_INSENSITIVE);
* <p>//欲使结果集可修改时调用本语句</p>
* <br>commonfun.setResultSetUpdateType(ResultSet.CONCUR_UPDATABLE);
* commonfun.executeQuery("insert into orders values(11,11,1,1,null)"); </p>
* <br> commonfun.setParameter(1, 999);
* <br> rs = commonfun.executeQuery();
* </p>
* <p>
* while (rs.next())
* </p>
* <br>{
* <br>}
* <p>
* <strong>例4:存储过程调用,设置出入参方式 </strong>
* </p> * <br>commonfun.prepareCall("up_lxl_test( ?1, ?2 )"); </p>
* <br> * commonfun.setParameter(1, "test"); <br>
* <br >commonfun.registerOutParameter(2, java.sql.Types.VARCHAR);<br>
* <br> commonfun.executeProcedure(); </p>
* <br> result = commonfun.getString(2); </p>
* <p> <strong>例5:存储过程调用,直接传入参数方式 </strong> </p>
* <p>* Copyright:
* <p>* Company:
* </p>
*
* @author
* @version 1.0
*/
public class CommonFUN{ //结果集属性定义
private int resultSetScrollType = ResultSet.TYPE_FORWARD_ONLY; //默认只能往前滚
private int resultSetUpdateType = ResultSet.CONCUR_READ_ONLY; //默认只读
private boolean AUTO_COMMIT = true; //默认自动提交事务
private Connection connection = null;
private PreparedStatement preparedStatement = null;
private Statement statement = null;
private CallableStatement callableStatement = null;
private boolean isOpen = false;
private DB db = DB.getInstance(); public CommonFUN() throws MilanException, SQLException
{
connect();
} /**
* 根据配置文件建立数据库连接, 当调用了close()后, 可调用本方法重建连接
*
* @throws MilanException
* @throws SQLException
*/
public void connect() throws MilanException, SQLException
{
connection = db.getConnection(); if(connection == null)
{
throw new MilanException(MilanException.ERR_CONNECT_DB_SERVER);
} this.isOpen = true;
} /**
* 使结果集可前后滚动, 在执行 prepareStatement(String SQL) 方法前调用
*
* @param iScrollType
* int
*/
public synchronized void setResultSetScrollType(int iScrollType)
{
resultSetScrollType = iScrollType;
} /**
* 使结果集可修改, 在执行 prepareStatement(String SQL) 方法前调用
*
* @param iUpdateType
* int
*/
public synchronized void setResultSetUpdateType(int iUpdateType)
{
resultSetUpdateType = iUpdateType;
} /**
* 设置是否事务型语句,如果入参为 false,则必须调用 commit()/rollback() 语句以 提交/回退 数据
*
* @param autoCommit
* boolean
* @throws SQLException
*/
public synchronized void setAutoCommit(boolean autoCommit) throws SQLException
{
connection.setAutoCommit(autoCommit);
} /**
* 执行预编译型语句时先调用本方法,再调用以下语句 <br>
* setParameter(0, arg_value); <br>
* ... <br>
* setParameter(n, arg_value); <br>
* executeQuery();
*
* @param SQL
* String
* @throws SQLException
*/
public synchronized void prepareStatement(String SQL) throws SQLException
{
if(preparedStatement != null)
{
preparedStatement.close();
preparedStatement = null;
} if(callableStatement != null)
{
callableStatement.close();
callableStatement = null;
} preparedStatement = connection.prepareStatement(SQL, resultSetScrollType,
resultSetUpdateType);
} public synchronized void prepareCall(String procedure) throws SQLException
{
if(preparedStatement != null)
{
preparedStatement.close();
preparedStatement = null;
} if(callableStatement != null)
{
callableStatement.close();
callableStatement = null;
} callableStatement = connection.prepareCall(procedure.trim().startsWith("{") ? procedure
: "{call " + procedure + "}");
} public synchronized boolean executeProcedure() throws MilanException, SQLException
{
boolean bResult = false; if(callableStatement == null)
{
throw new MilanException(MilanException.ERR_OBJECT_UNINIT);
} bResult = callableStatement.execute(); return bResult;
} public boolean executeProcedure(String procedure) throws SQLException
{
boolean bResult = false; callableStatement = connection.prepareCall(procedure.trim().startsWith("{") ? procedure
: "{call " + procedure + "}"); bResult = callableStatement.execute();
callableStatement.close();
callableStatement = null; return bResult;
}
* 执行更新语句: delete, update, insert into等
*
* @throws MilanException
* @throws SQLException
* @return int
*/
public synchronized int executeUpdate() throws MilanException, SQLException
{
int iReturn = 0; if(preparedStatement == null)
{
throw new MilanException(MilanException.ERR_OBJECT_UNINIT);
} iReturn = preparedStatement.executeUpdate();
preparedStatement.close();
preparedStatement = null; return iReturn;
} /**
* 执行更新语句: delete, update, insert into等
*
* @param executeSQL
* String
* @throws SQLException
* @return int
*/
public int executeUpdate(String executeSQL) throws SQLException
{
int iReturn = 0; statement = connection.createStatement();
iReturn = statement.executeUpdate(executeSQL);
statement.close();
statement = null; return iReturn;
} /**
* 执行预编译型查询语句: select * from t_key_code where order_id =?
*
* @throws MilanException
* @throws SQLException
* @return ResultSet
*/
public synchronized ResultSet executeQuery() throws MilanException, SQLException
{
if(preparedStatement == null)
{
throw new MilanException(MilanException.ERR_OBJECT_UNINIT);
} ResultSet rs = preparedStatement.executeQuery(); return rs;
} /**
* 执行查询语句, 如: select * from t_order where order_id = 999 会关闭之前已打开的
* ResultSet, 如果需保持多个活动 ResultSet, 请调用: public void executeQuery(String
* inSql, ResultSet rs)
*
* @param selectSQL
* String
* @throws SQLException
* @return ResultSet
*/
public ResultSet executeQuery(String selectSQL) throws SQLException
{
if(statement != null)
{
statement.close();
statement = null;
} statement = connection.createStatement(resultSetScrollType, resultSetUpdateType); return statement.executeQuery(selectSQL);
} /**
* 执行查询语句, 如: select * from t_key_code where order_id = 999 调用本方法后, 必须调用
* db.close(ResultSet) 以关闭 ResultSet, 而不能简单的关闭: ResultSet.close()
*
* @param selectSQL
* String
* @param rs
* ResultSet
* @throws SQLException
*/
public void executeQuery(String selectSQL, ResultSet rs) throws SQLException
{
if(rs != null)
{
rs.getStatement().close();
rs.close();
} rs = connection.createStatement(resultSetScrollType, resultSetUpdateType).executeQuery(
selectSQL);
} /**
* 如果调用了setAutoCommit(false)语句,可调用本方法以提交数据
*
* @throws SQLException
*/
public synchronized void commit() throws SQLException
{
connection.commit();
} /**
* 如果调用了setAutoCommit(false)语句,可调用本方法以回退数据
*/
public synchronized void rollback()
{
try
{
connection.rollback();
}
catch (SQLException se)
{
//回滚时失败,不再进行任何处理;
}
} /**
* 将select语句的执行结果转化为字符串数组
*
* @param selectSQL
* String
* @return String[][]
*/
public String[][] getQueryResult(String selectSQL) throws SQLException
{
int iRowCount = 0;
int iColCount = 0;
int iScrollType = resultSetScrollType; String orderSet[][] = new String[0][0];
ResultSet rset = null; if(selectSQL == null || selectSQL.equals(""))
{
return orderSet;
} // if (Constant.DEBUG)
// Log.writeText(Constant.LOG_FILE, selectSQL); try
{
setResultSetScrollType(ResultSet.TYPE_SCROLL_INSENSITIVE); //让结果集可前后滚动
rset = executeQuery(selectSQL); rset.last(); iRowCount = rset.getRow();
iColCount = rset.getMetaData().getColumnCount(); orderSet = new String[iRowCount][iColCount]; for(int i = iRowCount; i > 0; i--)
{
for(int j = 0; j < iColCount; j++)
{
orderSet[i - 1][j] = rset.getString(j + 1);
} rset.previous();
}
}
finally
{
setResultSetScrollType(iScrollType); //让结果集回到旧的状态
} return orderSet;
}
/**
* 取得记录集的第一个单元
* @param selectSQL
* @return
* @throws MilanException
* @throws SQLException
*/
public String getQueryValue(String selectSQL) throws MilanException, SQLException
{
return getQueryResult(selectSQL)[0][0];
} /**
* 取得记录集的第一个单元
* @param selectSQL
* @return
* @throws MilanException
* @throws SQLException
*/
public String[] getQueryValues(String selectSQL) throws MilanException, SQLException
{
return getQueryResult(selectSQL)[0];
} public synchronized void registerOutParameter(int paramIndex, int sqlType) throws SQLException,
MilanException
{
if(callableStatement == null)
{
throw new MilanException(MilanException.ERR_OBJECT_UNINIT);
} callableStatement.registerOutParameter(paramIndex, sqlType);
}
* setParameter(int, ...)系列函数对PrepareStatement语句进行入参设置
*/
public synchronized void setParameter(int paramIndex, String paramValue) throws SQLException,
MilanException
{
if(preparedStatement != null)
preparedStatement.setString(paramIndex, paramValue);
else
if(callableStatement != null)
callableStatement.setString(paramIndex, paramValue);
else
throw new MilanException(MilanException.ERR_OBJECT_UNINIT);
} public synchronized void setParameter(int paramIndex, double paramValue)
throws SQLException, MilanException
{
if(preparedStatement != null)
preparedStatement.setDouble(paramIndex, paramValue);
else
if(callableStatement != null)
callableStatement.setDouble(paramIndex, paramValue);
else
throw new MilanException(MilanException.ERR_OBJECT_UNINIT);
} public synchronized void setParameter(int paramIndex, float paramValue)
throws SQLException, MilanException
{
if(preparedStatement != null)
preparedStatement.setFloat(paramIndex, paramValue);
else
if(callableStatement != null)
callableStatement.setFloat(paramIndex, paramValue);
else
throw new MilanException(MilanException.ERR_OBJECT_UNINIT);
} public synchronized void setParameter(int paramIndex, boolean paramValue) throws SQLException,
MilanException
{
if(preparedStatement != null)
preparedStatement.setBoolean(paramIndex, paramValue);
else
if(callableStatement != null)
callableStatement.setBoolean(paramIndex, paramValue);
else
throw new MilanException(MilanException.ERR_OBJECT_UNINIT);
} public synchronized void setParameter(int paramIndex, int paramValue) throws SQLException,
MilanException
{
if(preparedStatement != null)
preparedStatement.setInt(paramIndex, paramValue);
else
if(callableStatement != null)
callableStatement.setInt(paramIndex, paramValue);
else
throw new MilanException(MilanException.ERR_OBJECT_UNINIT);
} public synchronized void setParameter(int paramIndex, long paramValue) throws SQLException,
MilanException
{
if(preparedStatement != null)
preparedStatement.setLong(paramIndex, paramValue);
else
if(callableStatement != null)
callableStatement.setLong(paramIndex, paramValue);
else
throw new MilanException(MilanException.ERR_OBJECT_UNINIT);
} public synchronized void setParameter(int paramIndex, short paramValue) throws SQLException,
MilanException
{
if(preparedStatement != null)
preparedStatement.setShort(paramIndex, paramValue);
else
if(callableStatement != null)
callableStatement.setShort(paramIndex, paramValue);
else
throw new MilanException(MilanException.ERR_OBJECT_UNINIT);
}
/**
* 将二进制码保存入数据库,新增方法,导入了java.sql.Blob,包,
* TODO:考虑是否导入com.mySQL.jdbc.Blob包配合相应的数据库驱动;需测试
* @param paramIndex
* @param paramValue
* @throws SQLException
* @throws MilanException
*/
public synchronized void setParameter(int paramIndex, File file) throws SQLException,
MilanException
{
FileInputStream is;
try
{
is = new FileInputStream(file);
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
throw new MilanException("文件加载失败!:" + e.getStackTrace().toString());
}
if(preparedStatement != null)
preparedStatement.setBinaryStream(paramIndex, is, (int)file.length());
else
if(callableStatement != null)
callableStatement.setBinaryStream(paramIndex, is, (int)file.length());
else
throw new MilanException(MilanException.ERR_OBJECT_UNINIT);
} /**
* getTypes(getInt、getString)系列函数取得存储过程的出参
*
* @param outParamIndex
* int
* @throws SQLException
* @return int
*/
public int getInt(int outParamIndex) throws SQLException
{
return callableStatement.getInt(outParamIndex);
} public String getString(int outParamIndex) throws SQLException
{
return callableStatement.getString(outParamIndex);
} public double getDouble(int outParamIndex) throws SQLException
{
return callableStatement.getDouble(outParamIndex);
} public float getFloat(int outParamIndex) throws SQLException
{
return callableStatement.getFloat(outParamIndex);
} public long getLong(int outParamIndex) throws SQLException
{
return callableStatement.getLong(outParamIndex);
} /**
* 释放数据库相关资源
*/
public void close(ResultSet rs)
{
try
{
Statement st = rs.getStatement();
st.close();
st = null;
// rs.next();
}
catch (SQLException ex)
{
Log.writeText(Constant.CF_LOG_FILE, ex);
}
}
* 释放数据库相关资源
*/
public void close()
{
try
{
if(callableStatement != null)
{
callableStatement.close();
callableStatement = null;
} if(preparedStatement != null)
{
preparedStatement.close();
preparedStatement = null;
} if(statement != null)
{
statement.close();
statement = null;
} if(connection != null)
{
db.closeConnection(connection);
} }
catch (SQLException se)
{
Log.writeText(Constant.CF_LOG_FILE, se);
} this.isOpen = false;
} public boolean isOpen()
{
return this.isOpen;
}