本帖最后由 王贺 于 2013-11-23 16:50 编辑
- <DIV id=cnblogs_post_body style="WORD-SPACING: 0px; FONT: 14px/28px 'ms shell dlg'; TEXT-TRANSFORM: none; COLOR: rgb(0,0,0); TEXT-INDENT: 0px; WHITE-SPACE: normal; LETTER-SPACING: normal; BACKGROUND-COLOR: rgb(255,255,255); WORD-WRAP: break-word; orphans: auto; widows: auto; webkit-text-stroke-width: 0px">
- <P style="MARGIN: 10px auto; TEXT-INDENT: 0px; WORD-WRAP: break-word">package sqlHelper;</P>
- <P style="MARGIN: 10px auto; TEXT-INDENT: 0px; WORD-WRAP: break-word">import java.sql.*;
- import java.util.ArrayList;</P>
- <P style="MARGIN: 10px auto; TEXT-INDENT: 0px; WORD-WRAP: break-word">public class JdbcSqlHelper {
- </P>
- <P style="MARGIN: 10px auto; TEXT-INDENT: 0px; WORD-WRAP: break-word">private String odbcStr;//Odbc字符串
- //定义odbcStr写入输出
- public String getOdbcStr() {
- return odbcStr;
- }</P>
- <P style="MARGIN: 10px auto; TEXT-INDENT: 0px; WORD-WRAP: break-word">public void setOdbcStr(String odbcStr) {
- this.odbcStr = odbcStr;
- }
- private Connection conn=null;//定义连接变量
- private Statement stat=null;//定义Statement 变量
- //定义函数用于连接数据库
- void connSql()
- {
- try {
- Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
- try {
- conn=DriverManager.getConnection("jdbc:odbc:"+odbcStr);
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- } catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- /// <summary>
- /// 关闭数据库连接
- /// </summary>
- void closeSqlconnAndStatement()
- {
- try {
- if(conn.isClosed()==false)
- conn.close();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- try {
- stat.close();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- //一般数据操作方法
- /// <summary>
- /// 一般查询函数,返回值ResultSet类型
- /// </summary>
- /// <param name="sqlStr">执行字符串</param>
- public ResultSet selectData(String sqlStr)//查询数据,反回数据表数据
- {
- connSql();
- ResultSet rs = null;
- try {
- if(conn.isClosed()==false){
- stat=conn.createStatement();
- rs=stat.executeQuery(sqlStr);
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return rs;
- }
- /// <summary>
- /// 一般插入函数,返回值为影响的行数,调用update_insert_delete_Function函数
- /// </summary>
- /// <param name="sqlStr">执行字符串</param>
- public int insertData(String sqlStr)// 插入数据操作,返回插入行数
- {
- return update_insert_delete_Function(sqlStr);
- }
- /// <summary>
- /// 一般更新函数,返回值为影响的行数,调用update_insert_delete_Function函数
- /// </summary>
- /// <param name="sqlStr">执行字符串</param>
- public int updateDate(String sqlStr)//更新数据操作,返回更新行数
- {
- return update_insert_delete_Function(sqlStr);
- }
- /// <summary>
- /// 一般删除函数,返回值为影响的行数,调用update_insert_delete_Function函数
- /// </summary>
- /// <param name="sqlStr">执行字符串</param>
- public int deleteDate(String sqlStr)//删除数据操作,返回删除行数
- {
- return update_insert_delete_Function(sqlStr);
- }
- /// <summary>
- /// 为一般增删改提供公用方法
- /// </summary>
- /// <param name="sqlStr">执行字符串</param>
- int update_insert_delete_Function(String sqlStr)//更新、插入、删除公用方法
- {
- connSql();
- int i=0;
- try {
- stat=conn.createStatement();
- i=stat.executeUpdate(sqlStr);
- closeSqlconnAndStatement();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return i;
- }</P>
- <P style="MARGIN: 10px auto; TEXT-INDENT: 0px; WORD-WRAP: break-word">//.....................................................................................................
- //......................................................................................................
- //........................................................................................................
- //存储过程对数据操作
- CallableStatement cs=null;//定义存储过程的执行字符串连接
- /// <summary>
- /// 关闭数据库连接和执行字符串连接,
- /// 在执行完selectStoreData函数,
- /// 然后调用此函数以断开连接;
- /// </summary>
- public void closeConnAndCallableStatement()
- {
- try {
- cs.close();
- conn.close();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- /// <summary>
- /// 存储查询函数
- /// </summary>
- /// <param name="storeSqlStr">存储过程字符串</param>
- /// <param name="al">存储过程的参数集合</param></P>
- <P style="MARGIN: 10px auto; TEXT-INDENT: 0px; WORD-WRAP: break-word">public ResultSet selectStoreData(String storeSqlStr,ArrayList<Object> al) throws SQLException
- {
- connSql();
- cs=conn.prepareCall(storeSqlStr);
- checkType(al);
- ResultSet rs=cs.executeQuery();
- return rs;
- }
- /// <summary>
- /// 判断参数集合的数据类型
- /// </summary>
- /// <param name="al">存储过程的参数集合</param>
- void checkType(ArrayList<Object> al) throws NumberFormatException, SQLException
- {<SPAN class=Apple-converted-space> </SPAN>
- for(int i=0;i<al.size();i++)
- {
- if(al.get(i) instanceof Integer){
- cs.setInt(i+1, Integer.parseInt((String) al.get(i)));
- }
- else if(al.get(i) instanceof String){
- cs.setString(i+1,(String) al.get(i));
- }
- else if(al.get(i) instanceof Float){
- cs.setFloat(i+1, Float.parseFloat((String)al.get(i)));
- }
- else if(al.get(i) instanceof Double){
- cs.setDouble(i+1, Double.parseDouble((String)al.get(i)));
- }
- else if(al.get(i) instanceof Long){
- cs.setLong(i+1, Long.parseLong((String)al.get(i)));
- }
- else if(al.get(i) instanceof Date){
- cs.setDate(i+1, Date.valueOf((String)al.get(i)));
- }
- else if(al.get(i) instanceof Time){
- cs.setTime(i+1, Time.valueOf((String)al.get(i)));
- }
- else if(al.get(i) instanceof Timestamp)
- {
- cs.setTimestamp(i+1, Timestamp.valueOf((String)al.get(i)));
- }
- else
- {
- cs.setObject(i+1, al.get(i));
- }
- }
- }
- /// <summary>
- /// 存储插入函数
- /// </summary>
- /// <param name="storeSqlStr">存储过程字符串</param>
- /// <param name="al">存储过程的参数集合</param>
- public int insertStoreData(String storeSqlStr,ArrayList<Object> al) throws SQLException
- {
- return update_insert_delete_Store_Function(storeSqlStr, al);
- }
- /// <summary>
- /// 存储更新函数
- /// </summary>
- /// <param name="storeSqlStr">存储过程字符串</param>
- /// <param name="al">存储过程的参数集合</param>
- public int updateStoreData(String storeSqlStr,ArrayList<Object> al) throws SQLException
- {
- return update_insert_delete_Store_Function(storeSqlStr, al);
- }
- /// <summary>
- /// 存储删除函数
- /// </summary>
- /// <param name="storeSqlStr">存储过程字符串</param>
- /// <param name="al">存储过程的参数集合</param>
- public int deleteStoreData(String storeSqlStr,ArrayList<Object> al) throws SQLException
- {
- return update_insert_delete_Store_Function(storeSqlStr, al);
- }
- /// <summary>
- /// 更新、插入、删除公用方法
- /// </summary>
- /// <param name="storeSqlStr">存储过程字符串</param>
- /// <param name="al">存储过程的参数集合</param>
- int update_insert_delete_Store_Function(String storeSqlStr,ArrayList<Object> al)
- {
- connSql();
- try {
- cs=conn.prepareCall(storeSqlStr);
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- try {
- checkType(al);//调用判断参数类型
- } catch (NumberFormatException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- int i = 0;
- try {
- i = cs.executeUpdate();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- closeConnAndCallableStatement();//关闭数据库连接
- return i;
- }
- </P>
- <P style="MARGIN: 10px auto; TEXT-INDENT: 0px; WORD-WRAP: break-word">
- }</P></DIV>
- <DIV id=MySignature style="MARGIN-TOP: 10px; WORD-SPACING: 0px; FONT: 14px/28px 'ms shell dlg'; TEXT-TRANSFORM: none; COLOR: rgb(0,0,0); TEXT-INDENT: 0px; WHITE-SPACE: normal; LETTER-SPACING: normal; BACKGROUND-COLOR: rgb(255,255,255); WORD-WRAP: break-word; orphans: auto; widows: auto; webkit-text-stroke-width: 0px"></DIV>
- <DIV class=clear style="CLEAR: both; WORD-SPACING: 0px; FONT: 14px/28px 'ms shell dlg'; TEXT-TRANSFORM: none; COLOR: rgb(0,0,0); TEXT-INDENT: 0px; WHITE-SPACE: normal; LETTER-SPACING: normal; BACKGROUND-COLOR: rgb(255,255,255); WORD-WRAP: break-word; orphans: auto; widows: auto; webkit-text-stroke-width: 0px"></DIV>
- <DIV id=blog_post_info_block style="MARGIN-TOP: 20px; WORD-SPACING: 0px; FONT: 14px/28px 'ms shell dlg'; TEXT-TRANSFORM: none; COLOR: rgb(0,0,0); TEXT-INDENT: 0px; WHITE-SPACE: normal; LETTER-SPACING: normal; BACKGROUND-COLOR: rgb(255,255,255); WORD-WRAP: break-word; orphans: auto; widows: auto; webkit-text-stroke-width: 0px">
- <DIV id=blog_post_info style="WORD-WRAP: break-word">
- </DIV></DIV>
复制代码 |
|