咖啡豆(JavaBean)•常用JavaBean
来源:互联网 发布:sql 存储过程 编辑:程序博客网 时间:2024/04/28 01:41
数据库操作封装JavaBean
在使用Hibernate之前常常使用这个JavaBean,类似于Net中的sqlHelper。
[java] view plaincopyprint?
- package beans;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class DBUtil {
- /**
- * 取得一个数据库连接
- * @return
- * @throws SQLException
- * @throws InstantiationException
- * @throws IllegalAccessException
- * @throws ClassNotFoundException
- */
- public Connection getConnection() throws SQLException,
- InstantiationException, IllegalAccessException,
- ClassNotFoundException {
- Connection conn = null;
- //加载数据库驱动类
- Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver")
- .newInstance();
- //数据库连接URL
- String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";
- //数据库用户名
- String user = "sa";
- //数据库密码
- String password = "1985315";
- //根据数据库参数取得一个数据库连接
- conn = DriverManager.getConnection(url, user, password);
- return conn;
- }
- /**
- * 根据传入的SQL语句返回一个结果集
- * @param sql
- * @return
- * @throws Exception
- */
- public ResultSet select(String sql) throws Exception {
- Connection conn = null;
- Statement stmt = null;
- ResultSet rs = null;
- try {
- conn = getConnection();
- stmt = conn.createStatement();
- rs = stmt.executeQuery(sql);
- return rs;
- } catch (SQLException sqle) {
- throw new SQLException("select data exception: "
- + sqle.getMessage());
- } catch (Exception e) {
- throw new Exception("System e exception: " + e.getMessage());
- }
- }
- /**
- * 根据传入的SQL语句向数据库增加一条记录
- * @param sql
- * @throws Exception
- */
- public void insert(String sql) throws Exception {
- Connection conn = null;
- PreparedStatement ps = null;
- try {
- conn = getConnection();
- ps = conn.prepareStatement(sql);
- ps.executeUpdate();
- } catch (SQLException sqle) {
- throw new Exception("insert data exception: " + sqle.getMessage());
- } finally {
- try {
- if (ps != null) {
- ps.close();
- }
- } catch (Exception e) {
- throw new Exception("ps close exception: " + e.getMessage());
- }
- }
- try {
- if (conn != null) {
- conn.close();
- }
- } catch (Exception e) {
- throw new Exception("connection close exception: " + e.getMessage());
- }
- }
- /**
- * 根据传入的SQL语句更新数据库记录
- * @param sql
- * @throws Exception
- */
- public void update(String sql) throws Exception {
- Connection conn = null;
- PreparedStatement ps = null;
- try {
- conn = getConnection();
- ps = conn.prepareStatement(sql);
- ps.executeUpdate();
- } catch (SQLException sqle) {
- throw new Exception("update exception: " + sqle.getMessage());
- } finally {
- try {
- if (ps != null) {
- ps.close();
- }
- } catch (Exception e) {
- throw new Exception("ps close exception: " + e.getMessage());
- }
- }
- try {
- if (conn != null) {
- conn.close();
- }
- } catch (Exception e) {
- throw new Exception("connection close exception: " + e.getMessage());
- }
- }
- /**
- * 根据传入的SQL语句删除一条数据库记录
- * @param sql
- * @throws Exception
- */
- public void delete(String sql) throws Exception {
- Connection conn = null;
- PreparedStatement ps = null;
- try {
- conn = getConnection();
- ps = conn.prepareStatement(sql);
- ps.executeUpdate();
- } catch (SQLException sqle) {
- throw new Exception("delete data exception: " + sqle.getMessage());
- } finally {
- try {
- if (ps != null) {
- ps.close();
- }
- } catch (Exception e) {
- throw new Exception("ps close exception: " + e.getMessage());
- }
- }
- try {
- if (conn != null) {
- conn.close();
- }
- } catch (Exception e) {
- throw new Exception("connection close exception: " + e.getMessage());
- }
- }
- }
package beans;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class DBUtil {/** * 取得一个数据库连接 * @return * @throws SQLException * @throws InstantiationException * @throws IllegalAccessException * @throws ClassNotFoundException */public Connection getConnection() throws SQLException,InstantiationException, IllegalAccessException,ClassNotFoundException {Connection conn = null;//加载数据库驱动类Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();//数据库连接URLString url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";//数据库用户名String user = "sa";//数据库密码String password = "1985315";//根据数据库参数取得一个数据库连接 conn = DriverManager.getConnection(url, user, password);return conn;}/** * 根据传入的SQL语句返回一个结果集 * @param sql * @return * @throws Exception */public ResultSet select(String sql) throws Exception {Connection conn = null;Statement stmt = null;ResultSet rs = null;try {conn = getConnection();stmt = conn.createStatement();rs = stmt.executeQuery(sql);return rs;} catch (SQLException sqle) {throw new SQLException("select data exception: "+ sqle.getMessage());} catch (Exception e) {throw new Exception("System e exception: " + e.getMessage());} }/** * 根据传入的SQL语句向数据库增加一条记录 * @param sql * @throws Exception */public void insert(String sql) throws Exception {Connection conn = null;PreparedStatement ps = null;try {conn = getConnection();ps = conn.prepareStatement(sql);ps.executeUpdate();} catch (SQLException sqle) {throw new Exception("insert data exception: " + sqle.getMessage());} finally {try {if (ps != null) {ps.close();}} catch (Exception e) {throw new Exception("ps close exception: " + e.getMessage());}}try {if (conn != null) {conn.close();}} catch (Exception e) {throw new Exception("connection close exception: " + e.getMessage());}}/** * 根据传入的SQL语句更新数据库记录 * @param sql * @throws Exception */public void update(String sql) throws Exception {Connection conn = null;PreparedStatement ps = null;try {conn = getConnection();ps = conn.prepareStatement(sql);ps.executeUpdate();} catch (SQLException sqle) {throw new Exception("update exception: " + sqle.getMessage());} finally {try {if (ps != null) {ps.close();}} catch (Exception e) {throw new Exception("ps close exception: " + e.getMessage());}}try {if (conn != null) {conn.close();}} catch (Exception e) {throw new Exception("connection close exception: " + e.getMessage());}}/** * 根据传入的SQL语句删除一条数据库记录 * @param sql * @throws Exception */public void delete(String sql) throws Exception {Connection conn = null;PreparedStatement ps = null;try {conn = getConnection();ps = conn.prepareStatement(sql);ps.executeUpdate();} catch (SQLException sqle) {throw new Exception("delete data exception: " + sqle.getMessage());} finally {try {if (ps != null) {ps.close();}} catch (Exception e) {throw new Exception("ps close exception: " + e.getMessage());}}try {if (conn != null) {conn.close();}} catch (Exception e) {throw new Exception("connection close exception: " + e.getMessage());}}}
分页操作JavaBean
在操作报表的时候常常用到,方便分页显示。
[java] view plaincopyprint?
- package beans;
- public class Page {
- private int totalPage;//总页数
- private int currentPage;//当前页数
- private int totalRecord;//总的记录条数
- private int currentRecord;//当前记录的条数
- private int pageSize = 6;//每页显示的记录数量,这里默认每页显示6条
- public int getCurrentPage() {
- return currentPage;
- }
- public void setCurrentPage(int currentRecord,int pageSize ) {
- //如果当前记录数除以每页显示条数可以整除,商就是当前的页码
- if(currentRecord%pageSize == 0)
- {
- currentPage = currentRecord/pageSize;
- }else
- {
- //如果当前记录数除以每页显示条数不能整除,商加1才是当前的页码
- currentPage = currentRecord/pageSize+1;
- }
- }
- public int getCurrentRecord() {
- return currentRecord;
- }
- public void setCurrentRecord(int currentRecord) {
- this.currentRecord = currentRecord;
- }
- public int getPageSize() {
- return pageSize;
- }
- public void setPageSize(int pageSize) {
- this.pageSize = pageSize;
- }
- public int getTotalPage() {
- return totalPage;
- }
- public void setTotalPage(int totalRecord,int pageSize) {
- //如果总记录数除以每页显示条数可以整除,商就是总页码
- if(totalRecord%pageSize == 0)
- {
- totalPage = totalRecord/pageSize;
- }else
- {
- //如果总记录数除以每页显示条数不能整除,商加1才是总页码
- totalPage = totalRecord/pageSize+1;
- }
- }
- public int getTotalRecord() {
- return totalRecord;
- }
- public void setTotalRecord(int totalRecord) {
- this.totalRecord = totalRecord;
- }
- }
0 0
- 咖啡豆(JavaBean)•常用JavaBean
- 咖啡豆(JavaBean)•常用JavaBean
- 咖啡豆(JavaBean)•香
- 咖啡豆(JavaBean)•属性私有,方法共有
- 咖啡豆(JavaBean)·JSP中使用JavaBean
- 完美的“咖啡豆”——JavaBean技术
- JavaBean
- JAVABEAN
- JavaBean
- javabean
- JAVABEAN
- javabean
- javabean
- JavaBean
- JavaBean
- javabean
- JavaBean
- JavaBean
- yii页面的 表单元素
- ios objective c 的基本知识
- Leetcode【33】:Search for a Range
- github学习(pro git)---第二章
- ios 表情各大编码对照表
- 咖啡豆(JavaBean)•常用JavaBean
- ios 让你的Xcode更加高效 分享0
- java学习笔记6、7
- 常用HTML标签美化博文
- XDOJ1177 - 直线划分平面
- Elasticsearch升级到1.2的不兼容变化-系统和设置
- 高仿CSDN社区树形图
- Spring的IoC
- 利用后缀数组找字符串中相同且长度最长的字符串