example6_1.jsp
1 <%@ page import="java.sql.*" %><%--
2 User: cltt
3 Date: 2018/9/27
4 Time: 21:14
5 --%>
6 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
7 <html>
8 <head>
9 <title>Title</title>
10 </head>
11 <body>
12 <%
13 Connection con;
14 Statement sql;
15 ResultSet rs;
16 Class.forName("com.mysql.cj.jdbc.Driver");
17 try {
18 DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
19 String url = "jdbc:mysql://localhost:3306/warehouse?characterEncoding=UTF-8&serverTimezone=UTC";
20 String user = "root";
21 String password = "123";
22 con = DriverManager.getConnection(url,user,password);
23 sql = con.createStatement();
24 rs = sql.executeQuery("select * from product");
25 out.print("<table border=2>");
26 out.print("<tr>");
27 out.print("<td width=100>"+"产品号");
28 out.print("<td width=100>"+"名称");
29 out.print("<td width=50>"+"生产日期");
30 out.print("<td width=50>"+"价格");
31 out.print("</tr>");
32 while (rs.next()) {
33 out.print("<tr>");
34 out.print("<td width=100>"+rs.getString(1)+"</td>");
35 out.print("<td width=100>"+rs.getString(2)+"</td>");
36 out.print("<td width=100>"+rs.getDate("madeTime")+"</td>");
37 out.print("<td width=50>"+rs.getFloat("price")+"</td>");
38 out.print("</tr>");
39 }
40 out.print("</table>");
41 con.close();
42 } catch (Exception e) {
43 out.print(e);
44 }
45 %>
46 </body>
47 </html>
p150
问题1:JDBC 和mysql 的版本都是8.0.12
问题2:JDBC 换了后要重新建数据库warehouse
问题3:在初始化时的目录要正确。
问题4:tomcat要重启。
example6_2.jsp
<%--
User: cltt
Date: 2018/9/28
Time: 17:36
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="inquire.jsp" method="post">
输入数据库名:<input type="text" name="dataBaseName" size="8">
输入表的名字:<input type="text" name="tableName" size="8">
<br>输入用户名:<input type="text" name="user" size="6">(默认是root)
输入密码:<input type="text" name="password" size="6">(默认是空)
<br><input type="submit" name="g" value="提交">
</form>
</body>
</html>
inquire.jsp
<%--
Created by IntelliJ IDEA.
User: 陈良
Date: 2018/11/2
Time: 20:54
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="inquire" tagdir="/WEB-INF/tags/6" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
String database = request.getParameter("dataBaseName");
String tableName = request.getParameter("tableName");
String user = request.getParameter("user");
String password = request.getParameter("password");
%>
<inquire:QueryTag dataBaseName="<%=database%>" tableName="<%=tableName%>" user="<%=user%>" password="<%=password%>" />
在<%=biao%>表查询到记录:
<br><%=queryResult%>
</body>
</html>
QueryTag.tag
<%@ tag pageEncoding="UTF-8" %>
<%@ tag import="java.sql.*" %>
<%@ attribute name="dataBaseName" required="true" %>
<%@ attribute name="tableName" required="true" %>
<%@ attribute name="user" required="true" %>
<%@ attribute name="password" required="false" %>
<%@ variable name-given="biao" scope="AT_END" %>
<%@ variable name-given="queryResult" scope="AT_END" %>
<%
StringBuffer result = new StringBuffer();
try {
result.append("<table border=1>");
DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
String url = "jdbc:mysql://localhost:3306/" +dataBaseName+
"?serverTimezone=UTC&characterEncoding=UTF-8";
// System.out.println(url);
Connection con = DriverManager.getConnection(url,user,password);
DatabaseMetaData metaData = con.getMetaData();
ResultSet rs1 = metaData.getColumns(null,null,tableName,null);
int count = 0; //字段个数
result.append("<tr>");
while (rs1.next()) {
count ++;
result.append("<td>"+rs1.getString(4)+"</td>");
}
result.append("</tr>");
Statement sql = con.createStatement();
ResultSet rs = sql.executeQuery("select * from "+tableName);
while (rs.next()) {
result.append("<tr>");
for(int k = 1; k <= count; k ++)
result.append("<td>"+rs.getString(k)+"</td>");
result.append("</tr>");
}
result.append("</table>");
con.close();
} catch (Exception ee) {
result.append("请输入正确的用户名和密码");
}
jspContext.setAttribute("queryResult",new String(result));
jspContext.setAttribute("biao",tableName);
%>
example6_3.jsp
1 <%--
2 Created by IntelliJ IDEA.
3 User: 陈良
4 Date: 2018/11/3
5 Time: 10:19
6 To change this template use File | Settings | File Templates.
7 --%>
8
9 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
10 <html><body bgcolor = cyan><font size = 2>
11 <form action="random.jsp" method="post">
12 输入数据库名:<input type="text" name="databaseName" size="8"><!-- 大小写不能弄错了 -->
13 <br> 输入表的名字:<input type="text" name="tableName" size="15">
14 <br>输入用户名:<input type="text" name="user" size="6">(默认是root)
15 <br>输入密码:<input type="password" name="password" size="6">(默认是空)<!-- password 要有引号 -->
16 <br>输入查询的记录数: <input type = "text" name = "count" value = 3 >
17 <br><input type="submit" name="g" value="提交">
18 </form>
19 </font>
20 </body>
21 </html>
random.jsp
1 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
2 <%@ taglib prefix="inquire" tagdir="/WEB-INF/tags/6" %>
3 <html>
4 <head>
5 <title>Title</title>
6 </head>
7 <body>
8 <%
9 String dName = request.getParameter("databaseName");
10 String tName = request.getParameter("tableName");
11 String id = request.getParameter("user");
12 String secret = request.getParameter("password");
13 String n = request.getParameter("count");
14 %>
15 <inquire:RandomQurty databaseName="<%=dName%>" tableName="<%=tName%>" user="<%=id%>" count="<%=n%>" password="<%=secret%>" />
16 在<%=biao%>表随机查询到<%=randomCount%>条记录:
17 <br><%=quertResult%>
18 </body>
19 </html>
1 <%-- RandomQurty.tag --%>
2 <%@ tag import="java.util.*" %>
3 <%@ tag import="java.sql.*" %>
4 <%@ tag pageEncoding="UTF-8" %>
5 <%@ attribute name="databaseName" required="true" %>
6 <%@ attribute name="tableName" required="true" %>
7 <%@ attribute name="user" required="true" %>
8 <%@ attribute name="password" required="true" %><!-- true 一定要传值 ,false 就可以不传值 -->
9 <%@ attribute name="count" required="true" %>
10 <%@ variable name-given="biao" scope="AT_END" %>
11 <%@ variable name-given="quertResult" scope="AT_END" %>
12 <%@ variable name-given="randomCount" scope="AT_END" %>
13
14 <%
15 Vector vector = new Vector();
16 StringBuffer result = new StringBuffer();
17 int n = 0;
18 try {
19 result.append("<table border=1>");
20 DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
21 String url = "jdbc:mysql://localhost:3306/" +databaseName+
22 "?serverTimezone=UTC&characterEncoding=UTF-8";
23 Connection con = DriverManager.getConnection(url,user,password);
24 DatabaseMetaData metaData = con.getMetaData();
25 ResultSet rs1 = metaData.getColumns(null,null,tableName,null);
26 int ans = 0; //字段个数
27 result.append("<tr>");
28 while (rs1.next()) {
29 ans ++;
30 result.append("<td>"+rs1.getString(4)+"</td>");
31 }
32 result.append("</tr>");
33 Statement sql = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
34 ResultSet rs = sql.executeQuery("select * from "+tableName);
35 rs.last();
36 int rowNumber = rs.getRow();
37 int number = rowNumber;
38 for(int i = 1; i <= number; i ++) vector.add(new Integer(i));
39 int m = Math.min(Integer.parseInt(count), number);
40 n = m;
41 while (m > 0) {
42 int i = (int)(Math.random()*vector.size());
43 int index = ((Integer)vector.elementAt(i)).intValue();
44 rs.absolute(index);
45 result.append("<tr>");
46 for(int k = 1; k <= ans; k ++) result.append("<td>"+rs.getString(k)+"</td>");
47 result.append("</tr>");
48 m --;
49 vector.removeElementAt(i);
50 }
51 result.append("</table>");
52 con.close();
53 } catch (Exception e) {
54 result.append("请输入正确的用户名和密码");
55 }
56 jspContext.setAttribute("quertResult",new String(result));
57 jspContext.setAttribute("biao",tableName);
58 jspContext.setAttribute("randomCount",String.valueOf(n));
59 %>
example6_4.jsp
<%--
Created by IntelliJ IDEA.
User: 陈良
Date: 2018/11/5
Time: 19:31
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<form action="byNumber.jsp" method="post">
根据产品号查询
<br>输入产品号:<input type="text" name="number">
<input type="submit" value="提交" name="g">
</form>
<form action="byPrice.jsp" method="post">
根据价格查询:
<br>价格在<input type="text" name="priceMin" size="5">至
<input type="text" name="priceMax" size="5">之间
<input type="submit" value="提交">
</form>
</body>
</html>
1 序号 快捷键 作用
2 1 CTRL + F 查找
3 2 F3 查找下一个
4 3 SHIFT + F3 查找上一个
5 4 CTRL + R 替换
6 5 CTRL + SHIFT + F 指定路径下查找
7 6 CTRL + SHIFT + R 指定路径下替换
8 ---------------------