时间:2021-07-01 10:21:17 帮助过:25人阅读
首先下载mysql-5.0.96-winx64,安装过程如下图所示。
1.安装MySQL 5.0
2.选择手动配置、服务类型、通用多功能型和安装路径
3.设置数据库访问量连接数为15、端口为3306(代码中设置URL用到)、编码方式为utf-8
4.设置默认超级root用户的用户名和密码,最后安装成功
安装MySQL 5.0成功后,进行数据库的简单操作。
1.运行MySQL输入默认用户密码123456
为统一并简化Java语言操作各种数据库,Sun公司提供了JDBC框架,用于所有Java应用以统一的方式连接数据库。从适用于企业级Oracle、DB2、SQL Server,到中型应用MySQL、Oracle XE,最后适用于小型个人应用的Access、FoxPro等。JDBC(Java DataBase Connectivity,Java数据库连接)通过使用数据库厂家提供的数据库JDBC驱动器类,可以连接到任何流程的数据库上。
使用前一篇文章Servlet中的例子,在JSP中使用JDBC查询数据,其核心操作如下。参考hongten博客,地址如下:
http://www.cnblogs.com/hongten/archive/2011/03/29/1998311.html
1.加载JDBC驱动程序(MySQL驱动)
2.提供JDBC连接的URL
- Class.forName("com.mysql.jdbc.Driver") ;
3.创建数据库的连接
- //驱动程序名
- String driverName = "com.mysql.jdbc.Driver";
- //数据库用户名
- String userName = "root";
- //密码
- String userPasswd = "123456";
- //数据库名
- String dbName = "test01";
- //表名
- String tableName = "student";
- //联结字符串
- String url = "jdbc:mysql://localhost:3306/" + dbName + "?user="
- + userName + "&password=" + userPasswd;
4.创建一个Statement
- Connection connection = DriverManager.getConnection(url);
7.关闭JDBC对象
- // 此方法比较高效 列是从左到右编号的,并且从列1开始
- while(rs.next()){
- String name = rs.getString("name") ;
- String pass = rs.getString(1) ;
- }
需要在项目TestServlet文件夹TestServlet\WebRoot\WEB-INF\lib复制mysql-connector-java-5.1.15-bin.jar包文件。然后修改success.jsp代码。具体代码如下:
- //释放连接方法 con ps rs
- public static void release(Connection con,Statement ps,ResultSet rs){
- try{
- if(rs!=null){ // 关闭记录集
- rs.close();
- }
- if(ps!=null){ // 关闭声明
- ps.close();
- }
- if(con!=null){ // 关闭连接对象
- con.close();
- }
- }catch (Exception e) {
- e.printStackTrace();
- }
- }
运行效果如下图所示:(可参考第二篇文章 (二)配置Servlet及简单实现表单提交)
- <%@ page language="java" import="java.sql.*,java.io.*,java.util.*" pageEncoding="UTF-8"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- <!-- 参考博文 http://blog.csdn.net/believejava/article/details/39111823 -->
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>验证成功界面</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- <style type="text/css">
- table {
- border: 2px #CCCCCC solid;
- width: 360px;
- }
- td,th {
- height: 30px;
- border: #CCCCCC 1px solid;
- }
- </style>
- </head>
- <body>
- 界面表单提交跳转成功 <br>
- <a href="index.jsp">返回</a>
- <%
- //驱动程序名
- String driverName = "com.mysql.jdbc.Driver";
- //数据库用户名
- String userName = "root";
- //密码
- String userPasswd = "123456";
- //数据库名
- String dbName = "test01";
- //表名
- String tableName = "student";
- //联结字符串
- String url = "jdbc:mysql://localhost:3306/" + dbName + "?user="
- + userName + "&password=" + userPasswd;
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- Connection connection = DriverManager.getConnection(url);
- Statement statement = connection.createStatement();
- String sql = "SELECT * FROM " + tableName;
- ResultSet rs = statement.executeQuery(sql);
- %>
- <br>
- <br>
- <table align="center">
- <tr>
- <th>
- <%
- out.print("学号");
- %>
- </th>
- <th>
- <%
- out.print("姓名");
- %>
- </th>
- <th>
- <%
- out.print("专业");
- %>
- </th>
- </tr>
- <%
- while (rs.next()) {
- %>
- <tr>
- <td>
- <%
- out.print(rs.getString(1));
- %>
- </td>
- <td>
- <%
- out.print(rs.getString(2));
- %>
- </td>
- <td>
- <%
- out.print(rs.getString(3));
- %>
- </td>
- </tr>
- <%
- }
- %>
- </table>
- <div align="center">
- <br> <br> <br>
- <%
- out.print("数据查询成功,恭喜你");
- %>
- </div>
- <%
- rs.close();
- statement.close();
- connection.close();
- %>
- </body>
- </html>
最后希望文章对你有所帮助,这篇文章是讲述JSP连接MySQL数据库,下一篇文章准备讲述Java文件和JSP文件之间相互操作数据库。如果文章有不足或错误的地方,还请海涵!这四篇文章基本就涵盖了Java网址的基础知识,你也可以实现简单的JSP网站了。
(By:Eastmount 2015-5-12 半夜2点 http://blog.csdn.net/eastmount/)
Java+MyEclipse+Tomcat (三)配置MySQL及查询数据显示在JSP网页中
标签:java myeclipse jsp网站 数据库配置 mysql