- ·上一篇文章:复习一下Java多线程
- ·下一篇文章:java统计两日期之间的天数
JDBC连接MySql数据库
-->
下载JDBC数据库驱动程序for mysql(比如:mysql-connector-java-5.1.22-bin.jar)
使用方法: 将此jar包复制到JDK的扩展目录中,比如C:\Program Files (x86)\Java\jdk1.8.0_60\jre\lib\ext
应用程序加载MySql的JDBC数据库驱动程序代码如下:
1 2 3 4 5 6 7 | Try{ Class.forName(“com.mysql.jdbc.Driver”); } Catch(Exception e){} |
建立连接
使用Java.sql中DriverManager类的类方法
Connection getConnection(java.lang.String);
此方法会抛出SQLException异常。返回Connection对象。
连接代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | Connection con; try{ String url="jdbc:mysql://127.0.0.1/k88_db1"; String user="root"; String password=""; con=DriverManager.getConnection(url, user, password); } Catch(SQLException e){} |
测试
首先打开mysql
net start mysql
测试写好的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | <%@page import="java.sql.SQLException"%> <%@page import="java.sql.DriverManager"%> <%@page import="java.sql.ResultSet"%> <%@page import="java.sql.Statement"%> <%@page import="java.sql.Connection"%> <%@ page contentType="text/html; charset=utf-8" %> <html> <body> <% Connection con; Statement sql; ResultSet rs; try{ Class.forName("com.mysql.jdbc.Driver"); } catch(Exception e){} try{ String url="jdbc:mysql://127.0.0.1/k88_db1"; String user="root"; String password=""; con=DriverManager.getConnection(url, user, password); sql=con.createStatement(); rs=sql.executeQuery("SELECT * FROM product"); out.print("<table border=2>"); out.print("<tr>"); out.print("<th width=100>"+"产品号"); out.print("<th width=100>"+"名称"); out.print("<th width=50>"+"生产日期"); out.print("<th width=50>"+"价格"); out.print("</tr>"); while(rs.next()){ out.print("<tr>"); out.print("<td>"+rs.getString(1)+"</td>"); out.print("<td>"+rs.getString(2)+"</td>"); out.print("<td>"+rs.getDate("madeTime")+"</td>"); out.print("<td>"+rs.getFloat("price")+"</td>"); out.print("</tr>"); } out.print("</table>"); con.close(); } catch(SQLException e){ out.print(e); } %> </body> </html> |
查询记录
和数据库建立连接后,就可以使用JDBC提供的api操作数据库了,比如查询,修改,更新数据库表。。。。
查询步骤:
(1)向数据库发送SQL查询语句
声明Statement对象,连接对象con调用createStatement()返回Statement对象。
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 | Statement sql; Try{ sql=con.createStatement(); } catch(SQLException e){ out.print(e); } |
(2)处理查询结果
有了SQL语句对象,就可以调用相应方法使用sql语句。查询结果将放入到ResultSet对象中。
ResultSet rs = sql.executeQuery(“SELECT * FROM tableName”);
遍历数据行next()方法,获取字段值 getXX()方法。
顺序查询
我们可以通过JDBC提供的api,可以在查询数据之前获取表中的各个字段名字和个数等,这样有助于编写可复用代码。
当和数据库建立连接后,使用连接对象con调用getMetaData()方法返回一个DatabaseMetaData对象。
DatabaseMetaData medata = con.getMetaData();
medata对象再调用getColumns()方法可以将表的字段信息以行列的形式存储在一个ResultSet对象中。
ResultSet tableMessage = metadata.getColumns(null,null,”product”,null);
代码示例:
“NewFile.jsp“
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="inquire.jsp" method="post"> 数据库名:<input type="text" name="dataBaseName" size=8> 表名:<input type="text" name="tableName" size=8> <br> Mysql用户名:<input type="text" name="user" size=6> <br> mysql密码:<input type="password" name="password" size=6>(默认为空) <br> <input type="submit" name="g" value="提交"> </form> </body> </html> |
“inquire.jsp”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib tagdir="/WEB-INF/tags" prefix="inquire"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% String database =request.getParameter("dataBaseName"); String tName=request.getParameter("tableName"); String id=request.getParameter("user"); String secret=request.getParameter("password"); %> <inquire:QueryTag dataBaseName="<%=database %>" tableName="<%=tName %>" user="<%=id %>" /> 在<%=biao %>表查询到记录:<%-- biao是tag文件返回的对象--%> <br> <%=queryResult %> <%--queryResult是tag文件返回的对象 --%> </body> </html> |
“QueryTag.tag”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | <%@tag import="java.sql.SQLException"%> <%@tag import="java.sql.DatabaseMetaData"%> <%@tag import="java.sql.DriverManager"%> <%@tag import="java.sql.ResultSet"%> <%@tag import="java.sql.Statement"%> <%@tag import="java.sql.Connection"%> <%@ tag language="java" pageEncoding="UTF-8"%> <%@ attribute name="dataBaseName" required="true" %> <%@ attribute name="tableName" required="true" %> <%@ attribute name="user" required="true" %> <%@ attribute name="password" required="false" %> <!-- tag文件接收jsp页面传递的数据 --> <%@ variable name-given="biao" scope="AT_END" %> <%@ variable name-given="queryResult" scope="AT_END" %> <% StringBuffer result=new StringBuffer(); try{ Class.forName("com.mysql.jdbc.Driver"); } catch(Exception e){ } Connection con; Statement sql; ResultSet rs; try{ result.append("<table border=1>"); String uri="jdbc:mysql://127.0.0.1/"+dataBaseName; con=DriverManager.getConnection(uri,user,""); DatabaseMetaData metadata=con.getMetaData(); ResultSet rs1=metadata.getColumns(null, null,tableName,null); int 字段个数=0; //字段个数 result.append("<tr>"); while(rs1.next()){ 字段个数++; String clumnName=rs1.getString(4); result.append("<td>"+clumnName+"</td>"); } result.append("</tr>"); sql=con.createStatement(); rs=sql.executeQuery("SELECT * FROM "+tableName); while(rs.next()){ result.append("<tr>"); for(int k=1;k<=字段个数;k++) result.append("<td>"+rs.getString(k)+"</td>"); result.append("</tr>"); } result.append("</table>"); con.close(); } catch(SQLException e){ out.println(e); } //返回对象 jspContext.setAttribute("queryResult",new String(result)); jspContext.setAttribute("biao", tableName); %> |
JDBC连接MySql数据库