JavaBean组件-数据库+JavaBean(四)

桃扇骨 2021-10-01 05:56 490阅读 0赞

封装了一个数据库的类DBAccess.java

  1. package mb.dao;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. public class DBAccess {
  9. // 数据库连接
  10. private Connection conn = null;
  11. // sql语句执行容器
  12. private Statement stmt = null;
  13. // 查询结果集
  14. private ResultSet rs = null;
  15. // 预定义sql语句执行容器
  16. private PreparedStatement pstmt = null;
  17. public DBAccess() {
  18. }
  19. /** 数据库连接方法,获得Connection对象 **/
  20. public Connection getConn() {
  21. if (conn == null) {
  22. getConnection();
  23. }
  24. return conn;
  25. }
  26. /** 数据库连接方法,获得Statement对象 **/
  27. public void getConnection() {
  28. try {
  29. // 显式地加载JDBC本地API驱动程序
  30. Class.forName("com.mysql.jdbc.Driver")
  31. .newInstance();
  32. // olLearn为数据库名称
  33. String url = "jdbc:mysql://localhost:3306/ollearn?useUnicode=true&characterEncoding=utf-8";
  34. String user = "root";
  35. String pwd = "123";
  36. // 创建Connection对象con
  37. this.conn = DriverManager.getConnection(url, user, pwd);
  38. // 根据conn对象创建sql语句执行容器对象stmt
  39. this.stmt = this.conn.createStatement();
  40. } catch (Exception e) {
  41. e.printStackTrace();
  42. }
  43. }
  44. /** 数据库查询方法,获得ResultSet对象 **/
  45. public ResultSet query(String strSql) {
  46. ResultSet rs = null;
  47. try {
  48. // 这个是没预编译sql语句的查询
  49. rs = stmt.executeQuery(strSql);
  50. } catch (SQLException e) {
  51. e.printStackTrace();
  52. }
  53. return rs;
  54. }
  55. /** 数据库查询方法,利用查询预处理对象,获得ResultSet对象 **/
  56. public ResultSet query(PreparedStatement pstmt) {
  57. this.pstmt = pstmt;
  58. ResultSet rs = null;
  59. try {
  60. rs = this.pstmt.executeQuery();
  61. } catch (SQLException e) {
  62. e.printStackTrace();
  63. }
  64. return rs;
  65. }
  66. /** 关闭连接 **/
  67. public void closeConnection() {
  68. try {
  69. if (rs != null) {
  70. rs.close();
  71. rs = null;
  72. }
  73. if (stmt != null) {
  74. stmt.close();
  75. stmt = null;
  76. }
  77. if (conn != null) {
  78. conn.close();
  79. conn = null;
  80. }
  81. if (pstmt != null) {
  82. pstmt.close();
  83. pstmt = null;
  84. }
  85. } catch (SQLException e) {
  86. e.printStackTrace();
  87. }
  88. }
  89. }

jsp中:

  1. <jsp:useBean id="term" class="mb.service.termService" scope="page" />
  2. <jsp:getProperty name="term" property="currTerm"/>

业务处理类termService.java

  1. package mb.service;
  2. import java.sql.ResultSet;
  3. import mb.dao.DBAccess;
  4. public class termService {
  5. public termService() {
  6. }
  7. public String getCurrTerm()
  8. {
  9. String sCurrTerm = "";
  10. DBAccess dba=new DBAccess();
  11. try{
  12. if(dba.getConn()!=null){
  13. String sSql="select * from term where iFlag=1";
  14. ResultSet rs=dba.query(sSql);
  15. while(rs!=null&&rs.next()){
  16. if(rs.getString("sTname")!=null){
  17. sCurrTerm = rs.getString("sTname");
  18. }
  19. }
  20. }
  21. }catch (Exception e){
  22. e.printStackTrace();
  23. }finally{
  24. dba.closeConnection();
  25. }
  26. return sCurrTerm;
  27. }
  28. }

在这里:jsp中的currTerm在bean中是没有这个属性的,但是有这个方法,所以可以得到结果

发表评论

表情:
评论列表 (有 0 条评论,490人围观)

还没有评论,来说两句吧...

相关阅读

    相关 JavaBean

    1、内省了解 JavaBean JavaBean 是一种特殊的 Java 类,主要用于传递数据信息,这种java 类中的方法主要 用于访问私有的字段,且方法名符合某种命名规