JSP简单访问数据库

本文提供了一个Java数据库操作的示例代码,展示了如何通过Java连接MySQL数据库并进行创建和删除表的操作。此外,还介绍了使用JSP配置数据库连接参数的方法。

Java代码

  1. public class DBHelper {
  2. private String driverName;
  3. private String url;
  4. private String user;
  5. private String password;
  6. private Connection connection;
  7. private String createTableSql;
  8. private String dropTableSql;
  9. public void getConnection() {
  10. if (null == connection) {
  11. try {
  12. Class.forName(driverName);
  13. connection = DriverManager.getConnection(url, user, password);
  14. } catch (ClassNotFoundException | SQLException e) {
  15. e.printStackTrace();
  16. }
  17. }
  18. }
  19. public void closeConnection() {
  20. if (null != connection) {
  21. try {
  22. connection.close();
  23. connection = null; //connection.close won't set the connection to be null
  24. System.out.println("closed db connection.");
  25. } catch (SQLException e) {
  26. e.printStackTrace();
  27. }
  28. }
  29. }
  30. public void createTable() {
  31. getConnection();
  32. Statement stmt = null;
  33. try {
  34. stmt = connection.createStatement();
  35. stmt.execute(createTableSql);
  36. System.out.println("Executed sql [" + createTableSql + "] success.");
  37. } catch (SQLException e) {
  38. e.printStackTrace();
  39. }
  40. closeConnection();
  41. }
  42. public void dropTable() {
  43. getConnection();
  44. Statement stmt = null;
  45. try {
  46. stmt = connection.createStatement();
  47. stmt.execute(dropTableSql);
  48. System.out.println("Executed sql [" + dropTableSql + "] success.");
  49. } catch (SQLException e) {
  50. e.printStackTrace();
  51. }
  52. closeConnection();
  53. }
  54. public String getDriverName() {
  55. return driverName;
  56. }
  57. public void setDriverName(String driverName) {
  58. this.driverName = driverName;
  59. }
  60. public String getUrl() {
  61. return url;
  62. }
  63. public void setUrl(String url) {
  64. this.url = url;
  65. }
  66. public String getUser() {
  67. return user;
  68. }
  69. public void setUser(String user) {
  70. this.user = user;
  71. }
  72. public String getPassword() {
  73. return password;
  74. }
  75. public void setPassword(String password) {
  76. this.password = password;
  77. }
  78. public String getCreateTableSql() {
  79. return createTableSql;
  80. }
  81. public void setCreateTableSql(String createTableSql) {
  82. this.createTableSql = createTableSql;
  83. }
  84. public String getDropTableSql() {
  85. return dropTableSql;
  86. }
  87. public void setDropTableSql(String dropTableSql) {
  88. this.dropTableSql = dropTableSql;
  89. }
  90. }

  Java代码

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>Test connecting mysql db</title>
  8. </head>
  9. <body>
  10. <jsp:useBean id="dbHelper" class="com.jesse.jsp.bean.DBHelper" />
  11. <jsp:setProperty property="driverName" name="dbHelper" value="com.mysql.jdbc.Driver"/>
  12. <jsp:setProperty property="url" name="dbHelper" value="jdbc:mysql://192.168.1.104:3306/db_jesse?useUnicode=true&characterEncoding=GBK"/>
  13. <jsp:setProperty property="user" name="dbHelper" value="jesse"/>
  14. <jsp:setProperty property="password" name="dbHelper" value="jesse"/>
  15. <jsp:setProperty property="createTableSql" name="dbHelper" value="create table test_tbl(id int)"/>
  16. <jsp:setProperty property="dropTableSql" name="dbHelper" value="drop table test_tbl"/>
  17. <%!
  18. private int id = 0;
  19. %>
  20. <%
  21. System.out.println(this); //to check if the servlet is singleton
  22. id++;
  23. synchronized(com.jesse.jsp.bean.DBHelper.class) {
  24. dbHelper.createTable();
  25. System.out.println(id + " created the table.");
  26. Thread.sleep(5000);
  27. dbHelper.dropTable();
  28. System.out.println(id + " dropped the table.");
  29. }
  30. %>
  31. </body>
  32. </html>

  Java代码

  1. org.apache.jsp.pages.mysql_jsp@567e0fb8
  2. Executed sql [create table test_tbl(id int)] success.
  3. closed db connection.
  4. 1 created the table.
  5. org.apache.jsp.pages.mysql_jsp@567e0fb8
  6. Executed sql [drop table test_tbl] success.
  7. closed db connection.
  8. 2 dropped the table.
  9. Executed sql [create table test_tbl(id int)] success.
  10. closed db connection.
  11. 2 created the table.
  12. Executed sql [drop table test_tbl] success.
  13. closed db connection.
  14. 2 dropped the table.

  Note:本意是要让log打印出来是哪个session在执行,看到结果...又长知识了 <%! %> 查了下,觉得这种说法有道理,暂不深究: <%%>是代码段,在由jsp转换成Servlet后 <%%>中的代码是放在serive方法中,相当于doGet()和doPost()方法 <%!%>是jsp声明,用来定义属性和方法的,在由jsp转换成Servlet后 <%!%>中的代码是放serive方法之外的.

                                                                                                                                    技术分享:www.kaige123.com

转载于:https://my.oschina.net/kaige123/blog/754813

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值