本文实例为大家分享了javaweb实现简单查询商品功能的具体代码,供大家参考,具体内容如下
customerservlet.java
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
package com.subing.web;
import java.io.ioexception;
import java.io.printwriter;
import javax.servlet.servletexception;
import javax.servlet.annotation.webservlet;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
@webservlet ( "/customerservlet" )
public class customerservlet extends httpservlet {
private static final long serialversionuid = 1l;
private sqldemo sql = null ;
private final static string s1 = "<h1>欢迎进入管理页面</h1><form action='customerservlet' method='post'> "
+ "精确查询:<input type='text' name='jqmess'/><br>"
+ "模糊查询:<input type='text' name='mhmess'/><br>"
+ "<input type='submit' value='提交' name='sub'/>"
+ "</form>" ;
// 登录的时候进行验证
private boolean isloginprov(string userinfo, string password) {
if (userinfo != null && userinfo.length() > 0 && password != null
&& password.length() > 0 ) {
return true ;
}
return false ;
}
public customerservlet() throws exception {
super ();
sql = new sqldemo(); // 进行数据库访问的类
// todo auto-generated constructor stub
}
/**
* @see httpservlet#doget(httpservletrequest request, httpservletresponse
* response)
*/
protected void doget(httpservletrequest request,
httpservletresponse response) throws servletexception, ioexception {
// todo auto-generated method stub
this .dopost(request, response);
}
/**
* @see httpservlet#dopost(httpservletrequest request, httpservletresponse
* response)
*/
protected void dopost(httpservletrequest request,
httpservletresponse response) throws servletexception, ioexception {
response.setcontenttype( "text/html;charset=gb2312" );
request.setcharacterencoding( "gb2312" );
printwriter pw = response.getwriter();
string sub = request.getparameter( "sub" );
string login = request.getparameter( "login" );
if (login != null && login.length() > 0 ) {
string admin_id = request.getparameter( "admin_id" );
string password = request.getparameter( "password" );
if (isloginprov(admin_id, password)) {
try {
if (sql.loginverify(admin_id, password)) {
pw.println(s1);
} else {
pw.println( "<h1>登录失败!</h2>2秒自动跳转到登录页面!" );
response.setheader( "refresh" , "2;url=login.html" );
}
} catch (exception e) {
e.printstacktrace();
}
} else {
pw.println( "<h1>登录失败!</h2>5秒自动跳转到登录页面!" );
response.setheader( "refresh" , "5;url=login.html" );
}
} else if (sub != null && sub.length() > 0 ) {
pw.println(s1);
string jqmess = request.getparameter( "jqmess" );
string mhmess = request.getparameter( "mhmess" );
if (jqmess != null && jqmess.length() > 0 ) {
try {
string s = sql.getjqmess(jqmess);
string mess[] = s.split( "," );
string html = "<table border='5'>" + "<tr>" + "<th>id号码</th>"
+ "<th>商品名称</th>" + "<th>商品价格</th>"
+ "<th>商品库存数量</th>" + "<th>商品描述</th>" ;
string main = "<tr>" + "<td>" + mess[ 0 ] + "</td>" + "<td>"
+ mess[ 1 ] + "</td>" + "<td>" + mess[ 2 ] + "</td>"
+ "<td>" + mess[ 3 ] + "</td>" + "<td>" + mess[ 4 ]
+ "</td></tr></table>" ;
string head = html + main;
pw.println(head);
} catch (exception e) {
e.printstacktrace();
}
} else if (mhmess != null && mhmess.length() > 0 ) {
try {
string head = "" ;
string html = "<table border='5'>" + "<tr>" + "<th>id号码</th>"
+ "<th>商品名称</th>" + "<th>商品价格</th>"
+ "<th>商品库存数量</th>" + "<th>商品描述</th>" ;
head += html;
string s = sql.getmhmess(mhmess);
string m[] = s.split( ",," );
for ( int i = 0 ; i < m.length; i++) {
string mess[] = m[i].split( "," );
string main = "<tr>" + "<td>" + mess[ 0 ] + "</td>"
+ "<td>" + mess[ 1 ] + "</td>" + "<td>" + mess[ 2 ]
+ "</td>" + "<td>" + mess[ 3 ] + "</td>" + "<td>"
+ mess[ 4 ] + "</td></tr>" ;
head += main;
}
head += "</table>" ;
pw.println(head);
} catch (exception e) {
e.printstacktrace();
}
}
}
}
}
|
数据库访问类:
sqldemo.java
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
package com.subing.web;
import java.sql.connection;
import java.sql.drivermanager;
import java.sql.preparedstatement;
import java.sql.resultset;
public class sqldemo {
private connection conn = null ;
private preparedstatement preparedstatement = null ;
public sqldemo() throws exception {
conn = getconnection();
}
private connection getconnection() throws exception {
string driverclass = "com.mysql.jdbc.driver" ;
string url = "jdbc:mysql:///shop?useunicode=true&characterencoding=gb2312" ;
string user = "root" ;
string password = "12345" ;
// 注册加载驱动
class .forname(driverclass);
// 获取连接
connection conn = drivermanager.getconnection(url, user, password);
system.out.println(conn);
return conn;
}
// 登录的时候 进行验证
public boolean loginverify(string userinfo, string password)
throws exception {
string sql = "select * from admin where admin_id = ?" ;
preparedstatement = conn.preparestatement(sql);
preparedstatement.setstring( 1 , userinfo);
resultset rs = preparedstatement.executequery();
if (rs.next()) {
if (rs.getstring( "password" ).equals(password)) {
system.out.println( "成功!" );
return true ;
}
}
system.out.println( "失败!" );
return false ;
}
public string getjqmess(string admin_id) throws exception {
string s = "" ;
string sql = "select * from product1 where product_id = ? or product_name like ?"
+ "or product_price like ?"
+ "or product_num like ?"
+ "or product_describe like ?" ;
preparedstatement = conn.preparestatement(sql);
preparedstatement.setstring( 1 , admin_id);
preparedstatement.setstring( 2 , admin_id);
preparedstatement.setstring( 3 , admin_id);
preparedstatement.setstring( 4 , admin_id);
preparedstatement.setstring( 5 , admin_id);
//查询到记录的时候,返回一个resultset,也处理了该方法查找失败的时候返回null的情况
resultset rs = preparedstatement.executequery();
while (rs.next()) {
s = rs.getint( 1 ) + "," + rs.getstring( 2 ) + "," + rs.getint( 3 ) + ","
+ rs.getint( 4 ) + "," + rs.getstring( 5 );
}
return s;
}
public string getmhmess(string admin_id) throws exception {
string mess = "" ;
string sql = "select * from product1 where product_id like ? or product_name like ? or product_price like ? or product_num like ?"
+ "or product_describe like ?" ;
preparedstatement = conn.preparestatement(sql);
preparedstatement.setstring( 1 , "%" + admin_id + "%" );
preparedstatement.setstring( 2 , "%" + admin_id + "%" );
preparedstatement.setstring( 3 , "%" + admin_id + "%" );
preparedstatement.setstring( 4 , "%" + admin_id + "%" );
preparedstatement.setstring( 5 , "%" + admin_id + "%" );
resultset rs = preparedstatement.executequery();
while (rs.next()) {
string s = rs.getint( 1 ) + "," + rs.getstring( 2 ) + ","
+ rs.getint( 3 ) + "," + rs.getint( 4 ) + "," + rs.getstring( 5 );
mess += s + ",," ;
}
return mess;
}
public static void main(string[] args) throws exception {
sqldemo sqldemo = new sqldemo();
string s = sqldemo.getmhmess( "xi" );
string m[] = s.split( ",," );
for ( int i = 0 ; i < m.length; i++) {
system.out.println(m[i]);
}
}
}
|
html文件:
login.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd" >
<html>
<head>
<meta http-equiv= "content-type" content= "text/html; charset=utf-8" >
<title>insert title here</title>
</head>
<body>
<h1>登录</h1>
<form action= "customerservlet" method= "post" >
账号:<input type= "text" name= "admin_id" />
密码:<input type= "password" name= "password" />
<input type= "submit" value= "登录" name= "login" />
</form>
</body>
</html>
|
数据库里面的表数据
运行效果
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/ACM_TH/article/details/50066287