代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
数据库操作
作者:
/ 发布于
2011/1/14
/
539
import java.lang.reflect.Method; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.ResultSetMetaData; import java.util.ArrayList; import java.util.List; import com.hewei.exception.MyException; public class DBHelp { // 注册数据库驱动 private final static String SQL_DRIVER = "com.microsoft.jdbc.sqlserver.SQLServerDriver"; // 数据库链接地址 private final static String SQL_URL = "jdbc:microsoft:sqlserver://127.0.0.1:7788;databaseName=myWork"; // 数据库用户名 private final static String SQL_NAME = "sa"; // 数据库密码 private final static String SQL_PASSWORD = ""; private Connection conn = null; private ResultSet rs = null; private PreparedStatement stmt = null; // 打开链接 public Connection getConnection() throws MyException { try { Class.forName(DBHelp.SQL_DRIVER); conn = DriverManager.getConnection(DBHelp.SQL_URL, DBHelp.SQL_NAME, DBHelp.SQL_PASSWORD); } catch (ClassNotFoundException e) { throw new MyException("数据库链接异常!"); } catch (SQLException e) { throw new MyException("数据库链接异常!"); } return conn; } // 关闭资源 public void releaseSource(ResultSet rs, PreparedStatement stmt, Connection conn) throws MyException { try { if (null != rs) { rs.close(); } if (null != stmt) { stmt.close(); } if (!conn.isClosed() && null != conn) { conn.close(); } } catch (Exception ex) { throw new MyException("数据库关闭异常!"); } } // 通用增加删除修改单条记录 public boolean extceNoQuery(String sql, Object[] obj) throws MyException { try { conn = this.getConnection(); conn.setAutoCommit(false); stmt = conn.prepareStatement(sql); if (null != obj) { for (int i = 0; i < obj.length; i++) { stmt.setObject(i + 1, obj[i]); } int rowCount = stmt.executeUpdate(); if (rowCount == 1) { conn.commit(); } else { conn.rollback(); } return rowCount == 1; } else { return false; } } catch (Exception ex) { throw new MyException("数据库异常!"); } finally { releaseSource(rs, stmt, conn); } } // 查询方法(返回一条记录) public Object queryObject(String sql, Object[] obj, Class clazz) throws MyException { try { Object object = null; String colName = ""; String methodName = ""; conn = this.getConnection(); stmt = conn.prepareStatement(sql); for (int i = 0; null != obj && i < obj.length; i++) { stmt.setObject(i + 1, obj[i]); } rs = stmt.executeQuery(); ResultSetMetaData resultSetMetaData = rs.getMetaData(); int colNumber = resultSetMetaData.getColumnCount(); String[] cols = new String[colNumber];// 拿到要查询的列 for (int k = 1; k <= colNumber; k++) { cols[k - 1] = resultSetMetaData.getColumnName(k); } Method[] methods = clazz.getDeclaredMethods(); if (rs.next()) { object = clazz.newInstance(); for (int n = 0; n < cols.length; n++) { methodName = cols[n]; colName = "set" + cols[n]; for (Method ms : methods) { if (ms.getName().equals(colName)) { ms.invoke(object, rs.getObject(methodName)); } } } } return object; } catch (Exception ex) { ex.printStackTrace(); return null; } finally { releaseSource(rs, stmt, conn); } } // 通用数据列表方法(返回多条记录) public List queryList(String sql, Object[] obj, Class clazz) throws MyException { try { List listObject = new ArrayList(); Object object = null; String methodName = ""; String colName = ""; conn = this.getConnection(); stmt = conn.prepareStatement(sql); if(null != obj){ for (int i = 0; i < obj.length; i++) { // System.out.println("jieguo"+obj[i]); stmt.setObject(i + 1, obj[i]); } } rs = stmt.executeQuery(); if (null != rs) { ResultSetMetaData resultSetMetaData = rs.getMetaData(); int colNumber = resultSetMetaData.getColumnCount(); String[] cols = new String[colNumber];// 拿到要查询的列 for (int k = 1; k <= colNumber; k++) { cols[k - 1] = resultSetMetaData.getColumnLabel(k); } Method[] methods = clazz.getDeclaredMethods(); while (rs.next()) { object = clazz.newInstance(); for (int m = 0; m < cols.length; m++) { colName = "set" + cols[m]; // System.out.println("colName=="+colName); for (Method ms : methods) { methodName = ms.getName(); // System.out.println("methodName=="+methodName); if (methodName.equals(colName)) { // System.out.println(rs.getObject(cols[m])); ms.invoke(object, rs.getObject(cols[m])); } } } listObject.add(object); } } else { return null; } if (null != listObject && listObject.size() > 0) { return listObject; } else { return null; } } catch (Exception ex) { ex.printStackTrace(); return null; // throw new MyException("数据库查询列表异常!"); } finally { releaseSource(rs, stmt, conn); } } /* * * 根据某个属性查询某个单个字段 */ public Object getBeanProptyByPropty(String sql, Object[] obj) { try { conn = this.getConnection(); stmt = conn.prepareStatement(sql); if (null != obj) { for (int i = 0; i < obj.length; i++) { stmt.setObject(i + 1, obj[i]); } } rs = stmt.executeQuery(); if (rs.next()) { return rs.getObject(1); } } catch (Exception ex) { } return null; } } </div>
试试其它关键字
数据库操作
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
贡献的其它代码
Label
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3