之前写了一大段,还说了一下具体JDBC连接数据库API的具体包的基本知识,哪知道自己手残不小心按了删除按钮。结果去看自动保存记录时,就只剩下下面这段了。好吧,其实最主要最核心的也就是下面这点了。具体如下:
例如,以下连接MySQL数据库范例:
Class.forName("com.mtsql.jdbc.Driver"); //加载MySQL数据库驱动程序
String url = "jdbc:mysql://localhost:3306/DataBase‘S Name"; //MySQL具体数据连接地址
Connection con = DriverManager.getConnection(url,user,password);//余指定数据建立连接
Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CUNCUR_UPDATATABLE);//进行数据库操作
首先,按照上面的实例,我给大家一个在Myeclipse上直接连接MySQL数据库的实例。这里我用的数据库名称是fine_food_system,我的数据库用户名和密码均为root。具体代码如下:
1 package MySQL; 2 3 import java.sql.*; 4 5 public class jdbctest { 6 public static void main(String args[]){ 7 String url="jdbc:mysql://127.0.0.1:3306/fine_food_system"; //建立具体数据连接地址 8 Connection con=null; //数据库连接,目前设置为空 9 Statement sm=null; //数据库查询状态,目前设置为空 10 ResultSet rs=null; //数据库操作执行结果存取,目前设置为空 11 try{ 12 Class.forName("com.mysql.jdbc.Driver");//加载MySQL数据库驱动程序 13 System.out.println("加载驱动程序成功"); 14 } 15 catch(Exception e){ 16 System.out.println("Can not load driver"); 17 return; 18 } 19 20 try{ 21 con = DriverManager.getConnection(url,"root","root"); //连接MySQL数据库 22 sm = con.createStatement(); //特定的容器,用来执行相关SQL语句 23 rs = sm.executeQuery("select * from client"); //执行括号中SQL语句,结果保存到rs中 24 System.out.println("Result of SELECT Statement"); 25 while(rs.next()){ //rs中查询结果可能包含多行,每执行一次循环,自动搜索下一行结果 26 String clientNo = rs.getString("clientNo"); //获取查询结果中客户编号 27 String clientName = rs.getString("clientName"); //获取查询结果中客户姓名 28 String clientTel = rs.getString("clientTel"); 29 String clientSite = rs.getString("clientSite"); //获取查询结果在客户地点 30 String clientPwd = rs.getString("clentPwd"); 31 System.out.println("clientNo:"+clientNo); 32 System.out.println("clientName:"+clientName); 33 System.out.println("clientTel:"+clientTel); 34 System.out.println("clientSite:"+clientSite); 35 System.out.println("clientPwd:"+clientPwd); 36 System.out.println(); 37 } 38 rs.close(); 39 sm.close(); 40 con.close(); 41 } 42 catch (SQLException ex){ 43 System.out.println("SQL异常!!!"); 44 } 45 } 46 47 }
执行结果如下:
加载驱动程序成功
Result of SELECT Statement
clientNo:1001
clientName:柳真
clientTel:15927175020
clientSite:中南民族大学
clientPwd:123456
上面说了在Myeclipse上直接连接MySQL数据问题了,那么下面就是重点了——JSP连接数据库,JSQ连接数据库步骤有以下几点:
(1)开启MySQL中,tomcat服务
(2)启动MySQL数据服务(前面直接连接MySQL数据当然也要连接了)
(3)为自己新建JSP所在项目添加服务Tomcat(不清楚的同学可以百度查一查)
(4)在浏览器中输入具体URL即可看到最终的结果
具体代码如下:
<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="GBK"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP ‘jdbcMysql.jsp‘ starting page</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"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <% String url="jdbc:mysql://127.0.0.1:3306/fine_food_system"; Connection con=null; Statement sm=null; ResultSet rs=null; try{ Class.forName("com.mysql.jdbc.Driver"); out.println("加载驱动程序成功"); } catch(Exception e){ out.println("Can not load driver"); return; } try{ con = DriverManager.getConnection(url,"root","root"); sm = con.createStatement(); rs = sm.executeQuery("select * from client"); System.out.println("Result of SELECT Statement"); while(rs.next()){ String clientNo = rs.getString("clientNo"); String clientName = rs.getString("clientName"); String clientTel = rs.getString("clientTel"); String clientSite = rs.getString("clientSite"); String clientPwd = rs.getString("clentPwd"); out.print("<br>"); out.print("<br>"); out.println("clientNo:"+clientNo); out.print("<br>"); out.println("clientName:"+clientName); out.print("<br>"); out.println("clientTel:"+clientTel); out.print("<br>"); out.println("clientSite:"+clientSite); out.print("<br>"); out.println("clientPwd:"+clientPwd); out.print("<br>"); out.print("<br>"); out.println(); } rs.close(); sm.close(); con.close(); } catch (SQLException ex){ System.out.println("SQL异常!!!"); } %> This is my JSP page. <br> </body> </html>
运行结果如下:
上面的实例是直接把Java代码放在JSP中运行的结果。以下一个示例是,经过改动后,以表格形式输出结果的示例,具体如下:
代码:
<%@ page language="java" import="java.util.*,java.sql.*,com.mysql.jdbc.Driver" pageEncoding="GBK"%> <% String driverName="com.mysql.jdbc.Driver"; String userName="root"; String userPwd="root"; String dbName="fine_food_system"; String url="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPwd; Class.forName(driverName).newInstance(); Connection conn=DriverManager.getConnection(url); Statement stmt=conn.createStatement(); String sql="select * from client"; ResultSet rs=stmt.executeQuery(sql); %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>JSP连接数据库实例</title> </head> <body> <center> client‘s information is:<br><br> <table border="1" cellspacing="0"> <tr> <th>clientNo</th> <th>clientName</th> <th>clientTel</th> <th>clientSite</th> <th>clientPwd</th> </tr> <%while(rs.next()){ %> <tr> <td><%=rs.getString("clientNo") %></td> <td><%= rs.getString("clientName") %></td> <td><%= rs.getString("clientTel")%></td> <td><%= rs.getString("clientSite") %></td> <td><%= rs.getString("clentPwd") %></td> </tr> <%} %> </table> </center> </body> </html> <% conn.close(); stmt.close(); rs.close(); %>
运行结果如下:
以上就是上周我自己在学习JSP连接MySQL数据库的收获,在记录自己学习过程的同时,也希望能够帮助到其他同学^~^