- /**
- *作者:devilishking
- *email:devilishking@126.com
- *
- */
- package com.dal;
- import javax.sql.DataSource;
- import javax.naming.*;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.util.*;
- import java.sql.*;
- public abstract class SQLHelper {
- /**
- * 连接数据库
- *
- * @return
- */
- private static Connection getConnect() {
- try {
- InitialContext context = new InitialContext();
- DataSource ds = (DataSource) context.lookup("java:/MSAccessDS");
- return ds.getConnection();
- } catch (Exception e) {
- return null;
- }
- }
- /**
- * 用于执行语句(eg:insert语句,update语句,delete语句)
- *
- * @param String
- * cmdtext,SQL语句
- * @param OracleParameter[]
- * parms,参数集合
- * @return int,SQL语句影响的行数
- */
- public static int ExecuteNonQuery(String cmdtext, String[] parms)
- throws Exception {
- PreparedStatement pstmt = null;
- Connection conn = null;
- try {
- conn = getConnect();
- pstmt = conn.prepareStatement(cmdtext);
- prepareCommand(pstmt, parms);
- return pstmt.executeUpdate();
- } catch (Exception e) {
- throw new Exception("executeNonQuery方法出错:" + e.getMessage());
- } finally {
- try {
- if (pstmt != null)
- pstmt.close();
- if (conn != null)
- conn.close();
- } catch (Exception e) {
- throw new Exception("执行executeNonQuery方法出错:" + e.getMessage());
- }
- }
- }
- /**
- * 用于获取结果集语句(eg:selete * from table)
- *
- * @param cmdtext
- * @param parms
- * @return ResultSet
- * @throws Exception
- */
- public static ArrayList ExecuteReader(String cmdtext, String[] parms)
- throws Exception {
- PreparedStatement pstmt = null;
- Connection conn = null;
- try {
- conn = getConnect();
- pstmt = conn.prepareStatement(cmdtext);
- prepareCommand(pstmt, parms);
- ResultSet rs = pstmt.executeQuery();
- ArrayList al = new ArrayList();
- ResultSetMetaData rsmd = rs.getMetaData();
- int column = rsmd.getColumnCount();
- while (rs.next()) {
- Object[] ob = new Object[column];
- for (int i = 1; i <= column; i++) {
- ob[i - 1] = rs.getObject(i);
- }
- al.add(ob);
- }
- rs.close();
- return al;
- } catch (Exception e) {
- throw new Exception("executeSqlResultSet方法出错:" + e.getMessage());
- } finally {
- try {
- if (pstmt != null)
- pstmt.close();
- if (conn != null)
- conn.close();
- } catch (Exception e) {
- throw new Exception("executeSqlResultSet方法出错:" + e.getMessage());
- }
- }
- }
- /**
- * 用于获取单字段值语句(用名字指定字段)
- *
- * @param cmdtext
- * SQL语句
- * @param name
- * 列名
- * @param parms
- * OracleParameter[]
- * @return Object
- * @throws Exception
- */
- public static Object ExecuteScalar(String cmdtext, String name,
- String[] parms) throws Exception {
- PreparedStatement pstmt = null;
- Connection conn = null;
- ResultSet rs = null;
- try {
- conn = getConnect();
- pstmt = conn.prepareStatement(cmdtext);
- prepareCommand(pstmt, parms);
- rs = pstmt.executeQuery();
- if (rs.next()) {
- return rs.getObject(name);
- } else {
- return null;
- }
- } catch (Exception e) {
- throw new Exception("executeSqlObject方法出错:" + e.getMessage());
- } finally {
- try {
- if (rs != null)
- rs.close();
- if (pstmt != null)
- pstmt.close();
- if (conn != null)
- conn.close();
- } catch (Exception e) {
- throw new Exception("executeSqlObject方法出错:" + e.getMessage());
- }
- }
- }
- /**
- * 用于获取单字段值语句(用序号指定字段)
- *
- * @param cmdtext
- * SQL语句
- * @param index
- * 列名索引
- * @param parms
- * OracleParameter[]
- * @return Object
- * @throws Exception
- */
- public static Object ExecuteScalar(String cmdtext, int index, String[] parms)
- throws Exception {
- PreparedStatement pstmt = null;
- Connection conn = null;
- ResultSet rs = null;
- try {
- conn = getConnect();
- pstmt = conn.prepareStatement(cmdtext);
- prepareCommand(pstmt, parms);
- rs = pstmt.executeQuery();
- if (rs.next()) {
- return rs.getObject(index);
- } else {
- return null;
- }
- } catch (Exception e) {
- throw new Exception("executeSqlObject方法出错:" + e.getMessage());
- } finally {
- try {
- if (rs != null)
- rs.close();
- if (pstmt != null)
- pstmt.close();
- if (conn != null)
- conn.close();
- } catch (Exception e) {
- throw new Exception("executeSqlObject方法出错:" + e.getMessage());
- }
- }
- }
- /**
- * @param pstmt
- * @param cmdtext
- * @param parms
- * Object[]
- * @throws Exception
- */
- private static void prepareCommand(PreparedStatement pstmt, String[] parms)
- throws Exception {
- try {
- if (parms != null) {
- for (int i = 0; i < parms.length; i++) {
- try {
- pstmt.setDate(i + 1, java.sql.Date.valueOf(parms[i]));
- } catch (Exception e) {
- try {
- pstmt
- .setDouble(i + 1, Double
- .parseDouble(parms[i]));
- } catch (Exception e1) {
- try {
- pstmt.setInt(i + 1, Integer.parseInt(parms[i]));
- } catch (Exception e2) {
- try {
- pstmt.setString(i + 1, parms[i]);
- } catch (Exception e3) {
- System.out
- .print("SQLHelper-PrepareCommand Err1:"
- + e3);
- }
- }
- }
- }
- }
- }
- } catch (Exception e1) {
- System.out.print("SQLHelper-PrepareCommand Err2:" + e1);
- }
- }
- }
/**
*作者:devilishking
*email:devilishking@126.com
*
*/
package com.dal;
import javax.sql.DataSource;
import javax.naming.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.*;
import java.sql.*;
public abstract class SQLHelper {
/**
* 连接数据库
*
* @return
*/
private static Connection getConnect() {
try {
InitialContext context = new InitialContext();
DataSource ds = (DataSource) context.lookup("java:/MSAccessDS");
return ds.getConnection();
} catch (Exception e) {
return null;
}
}
/**
* 用于执行语句(eg:insert语句,update语句,delete语句)
*
* @param String
* cmdtext,SQL语句
* @param OracleParameter[]
* parms,参数集合
* @return int,SQL语句影响的行数
*/
public static int ExecuteNonQuery(String cmdtext, String[] parms)
throws Exception {
PreparedStatement pstmt = null;
Connection conn = null;
try {
conn = getConnect();
pstmt = conn.prepareStatement(cmdtext);
prepareCommand(pstmt, parms);
return pstmt.executeUpdate();
} catch (Exception e) {
throw new Exception("executeNonQuery方法出错:" + e.getMessage());
} finally {
try {
if (pstmt != null)
pstmt.close();
if (conn != null)
conn.close();
} catch (Exception e) {
throw new Exception("执行executeNonQuery方法出错:" + e.getMessage());
}
}
}
/**
* 用于获取结果集语句(eg:selete * from table)
*
* @param cmdtext
* @param parms
* @return ResultSet
* @throws Exception
*/
public static ArrayList ExecuteReader(String cmdtext, String[] parms)
throws Exception {
PreparedStatement pstmt = null;
Connection conn = null;
try {
conn = getConnect();
pstmt = conn.prepareStatement(cmdtext);
prepareCommand(pstmt, parms);
ResultSet rs = pstmt.executeQuery();
ArrayList al = new ArrayList();
ResultSetMetaData rsmd = rs.getMetaData();
int column = rsmd.getColumnCount();
while (rs.next()) {
Object[] ob = new Object[column];
for (int i = 1; i <= column; i++) {
ob[i - 1] = rs.getObject(i);
}
al.add(ob);
}
rs.close();
return al;
} catch (Exception e) {
throw new Exception("executeSqlResultSet方法出错:" + e.getMessage());
} finally {
try {
if (pstmt != null)
pstmt.close();
if (conn != null)
conn.close();
} catch (Exception e) {
throw new Exception("executeSqlResultSet方法出错:" + e.getMessage());
}
}
}
/**
* 用于获取单字段值语句(用名字指定字段)
*
* @param cmdtext
* SQL语句
* @param name
* 列名
* @param parms
* OracleParameter[]
* @return Object
* @throws Exception
*/
public static Object ExecuteScalar(String cmdtext, String name,
String[] parms) throws Exception {
PreparedStatement pstmt = null;
Connection conn = null;
ResultSet rs = null;
try {
conn = getConnect();
pstmt = conn.prepareStatement(cmdtext);
prepareCommand(pstmt, parms);
rs = pstmt.executeQuery();
if (rs.next()) {
return rs.getObject(name);
} else {
return null;
}
} catch (Exception e) {
throw new Exception("executeSqlObject方法出错:" + e.getMessage());
} finally {
try {
if (rs != null)
rs.close();
if (pstmt != null)
pstmt.close();
if (conn != null)
conn.close();
} catch (Exception e) {
throw new Exception("executeSqlObject方法出错:" + e.getMessage());
}
}
}
/**
* 用于获取单字段值语句(用序号指定字段)
*
* @param cmdtext
* SQL语句
* @param index
* 列名索引
* @param parms
* OracleParameter[]
* @return Object
* @throws Exception
*/
public static Object ExecuteScalar(String cmdtext, int index, String[] parms)
throws Exception {
PreparedStatement pstmt = null;
Connection conn = null;
ResultSet rs = null;
try {
conn = getConnect();
pstmt = conn.prepareStatement(cmdtext);
prepareCommand(pstmt, parms);
rs = pstmt.executeQuery();
if (rs.next()) {
return rs.getObject(index);
} else {
return null;
}
} catch (Exception e) {
throw new Exception("executeSqlObject方法出错:" + e.getMessage());
} finally {
try {
if (rs != null)
rs.close();
if (pstmt != null)
pstmt.close();
if (conn != null)
conn.close();
} catch (Exception e) {
throw new Exception("executeSqlObject方法出错:" + e.getMessage());
}
}
}
/**
* @param pstmt
* @param cmdtext
* @param parms
* Object[]
* @throws Exception
*/
private static void prepareCommand(PreparedStatement pstmt, String[] parms)
throws Exception {
try {
if (parms != null) {
for (int i = 0; i < parms.length; i++) {
try {
pstmt.setDate(i + 1, java.sql.Date.valueOf(parms[i]));
} catch (Exception e) {
try {
pstmt
.setDouble(i + 1, Double
.parseDouble(parms[i]));
} catch (Exception e1) {
try {
pstmt.setInt(i + 1, Integer.parseInt(parms[i]));
} catch (Exception e2) {
try {
pstmt.setString(i + 1, parms[i]);
} catch (Exception e3) {
System.out
.print("SQLHelper-PrepareCommand Err1:"
+ e3);
}
}
}
}
}
}
} catch (Exception e1) {
System.out.print("SQLHelper-PrepareCommand Err2:" + e1);
}
}
}
Ⅰ.引言
使用该类目的就是让使用者更方便、更安全的对数据库的操作,既是除了在SQLHelper类以外的所有类将不用引用对数据库操作的任何类与语句,无须担心数据库的连接与关闭的问题。但是,该类仍然需要大量的调试与修改。故此,请使用者不断完善该类,以至于能与SQLHelper(.NET版)的功能并驾齐驱。
Ⅱ.注意事项
1. 使用前设置
手动修改以下代码段为当前连接数据库的代码段
private static Connection getConnect() {
try {
//修改代码段
} catch (Exception e) {
return null;
}
}
2. SQLHelper类支持数据库字段类型
1) 文本类型
2) 带符号整数类型
3) 双精度浮点数类型
4) 日期类型
注意:如果没有想要的类时,请手动添加到以下方法内
private static void prepareCommand(PreparedStatement pstmt, String[] parms)
throws Exception {
//代码段
}
另外,添加时请注意“从特殊到常用”原则,即将最特殊的类型放到try/catch语句的最高级的代码段区,将最常用的类型放到try/catch语句的最低级的代码段区。
Ⅲ.类方法说明
注意:正文内出现“【】”的区域在使用的时候必须进行相应替换,而“『』”的区域为替换的可选项。
1. SQLHelper.ExecuteNonQuery
作用:用于执行语句
返回类型:int
格式:
boolean state = false;
try {
String[] parms = {【参数区(如果类型为非String型,则"" + 变量)】};
int i = SQLHelper.ExecuteNonQuery(【SQL语句】, parms『可为null』);
if (i == 1) {
state = true;
}
} catch (Exception e) {
e.printStackTrace();
}
return state;
2. SQLHelper.ExecuteScalar
作用:用于获取单字段值语句
返回类型:Object
格式:
【变量类型】 【变量】 = 【初始化变量】
try {
String[] parms = { 【参数区(如果类型为非String型,则"" + 变量)】};
【变量】 = 『强制转换为变量的类型』SQLHelper.ExecuteScalar(【SQL语句】, 【列名或列名索引】, parms『可为null』)
.toString();
} catch (Exception e) {
e.printStackTrace();
}
return 【变量】;
3. SQLHelper.ExecuteReader
作用:用于获取结果集语句
返回类型:java.util.ArrayList
格式:
ArrayList al = new ArrayList();
try {
String[] parms = { 【参数区(如果类型为非String型,则"" + 变量)】};
al = SQLHelper.ExecuteReader(【SQL语句】, parms『可为null』);
al = getArrayListValues(al);
} catch (Exception e) {
errMessage = e.toString();
}
return al;
另外,添加以下两个函数:
//根据结果集获取其全部信息
private ArrayList getArrayListValues(ArrayList arrayList) throws Exception {
ArrayList al2 = new ArrayList();
for (int i = 0; i < arrayList.size(); i++) {
al2.add(getOneRowValues(i, new 【最终存放数据的变量类型】, arrayList));
}
return al2;
}
//根据行索引,获取其一行的所有信息
private TbUserBLL getOneRowValues(int i, 【最终存放数据的变量类型】【最终存放数据的变量】, ArrayList arrayList) throws Exception {
Object[] ob = (Object[]) arrayList.get(i);
【最终存放数据的变量】.setXX=『强制转换为其属性类型』ob[0].toString();
…
【最终存放数据的变量】.setXX=『强制转换为其属性类型』ob[X].toString();
return【最终存放数据的变量】;
}
Ⅳ.测试列表
注意:如果再对该类进行测试时,请按以下格式添加说明。
1.测试一
连接数据库方式:jdbc-odbc桥
使用数据库:Access
测试内容:对SQLHelper各方法的使用情况测试
测试人员:刘冀超
测试结果:成功。
Ⅴ.测试工具说明
1.数据库表
TbUser【用户表】
字段名 数据结构 允许空值 说 明
tbUserID int 否 用户编号
tbUserName varchar(20) 否 用户姓名
tbUserPwd varchar(20) 否 用户密码
tbUserRole int 否 用户角色
2.测试页面(Index.jsp)
说明:该页面包含了所有间接调用SQLHelper所提供的方法(即ExecuteNonQuery,ExecuteScalar,ExecuteReader)。如有需要请再以下标签前添加功能,
<tr>
<td align="center" class="STYLE3">测试结果</td>
</tr>
并按照内部格式将错误信息放入errMessage的变量内。