TestCode2
??1
package
?onlyfun.caterpillar;
??2
??3
import
?javax.servlet.
*
;
??4
import
?java.util.
*
;
??5
import
?java.sql.
*
;
??6
??7
public
?
class
?Functions?
{
??8
????
private
?
static
?Connection?conn?
=
?
null
;
??9
?10
????
public
?
static
?
boolean
?filled_out(ServletRequest?req)?
{
?11
????????Enumeration?form_vars?
=
?req.getParameterNames();
?12
????????
while
(form_vars.hasMoreElements())?
{
?13
????????????String?var?
=
?(String)?form_vars.nextElement();
?14
????????????
if
(req.getParameter(var).length()?
==
?
0
)
?15
????????????????
return
?
false
;
?16
????????}
?17
????????
return
?
true
;
?18
????}
?19
?20
????
public
?
static
?
boolean
?valid_email(String?address)?
{
?21
????????
if
(address.matches(
"
^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*$
"
))
?22
????????????
return
?
true
;
?23
????????
else
?24
????????????
return
?
false
;
?25
????}
?26
?27
????
public
?
static
?Connection?getDBConnection(ServletContext?context)?
{
?28
????????
if
(conn?
==
?
null
)?
{
?29
????????????String?sqluri?
=
?
"
jdbc:mysql://localhost:3306/
"
?
+
?context.getInitParameter(
"
sqldb
"
)?
+
?
"
?useUnicode=true&characterEncoding=big5
"
;
?30
????????????String?sqluser?
=
??context.getInitParameter(
"
sqluser
"
);
?31
????????????String?sqlpwd?
=
??context.getInitParameter(
"
sqlpwd
"
);
?32
????????????
try
?
{
?33
????????????????Class.forName(context.getInitParameter(
"
sqldriver
"
));
?34
????????????????conn?
=
?DriverManager.getConnection(sqluri,?sqluser,?sqlpwd);
?35
????????????}
?36
????????????
catch
(Exception?e)?
{
?37
????????????}
?38
????????}
?39
?40
????????
return
?conn;
?41
????}
?42
?43
????
public
?
static
?
boolean
?login(ServletContext?application,?String?username,?String?passwd)?
{
?44
????????
try
?
{
?45
????????????Connection?conn?
=
?getDBConnection(application);
?46
????????????ResultSet?result?
=
?conn.createStatement().executeQuery(
?47
???????????????????
"
SELECT?*?FROM?users?WHERE?username='
"
?
+
?username?
+
?
"
'?and?passwd=password('
"
?
+
?passwd?
+
?
"
')
"
);
?48
????????????
return
?result.next();
?49
????????}
?50
????????
catch
(Exception?e)?
{
?51
????????}
?52
????????????????????????????????????????????????????????????????????????????????????????????????
?53
????????
return
?
false
;
?54
????}
?55
?56
????
public
?
static
?
boolean
?change_password(ServletContext?application,?String?username,?String?old_password,?String?new_password)?
{
?57
????????
if
(login(application,?username,?old_password))?
{
?58
????????????Connection?conn?
=
?getDBConnection(application);
?59
????????????
if
(conn?
==
?
null
)?
{
?60
????????????????
return
?
false
;
?61
????????????}
?62
?63
????????????
try
?
{
?64
????????????????
int
?result?
=
?conn.createStatement().executeUpdate(
"
UPDATE?users?SET?passwd=password('
"
?
+
?new_password?
+
?
"
')?WHERE?username='
"
?
+
?username?
+
?
"
'
"
);
?65
?66
????????????????
if
(result?
==
?
0
)?
{
?67
????????????????????
return
?
false
;
?68
????????????????}
?69
????????????????
else
?
{
?70
????????????????????
return
?
true
;
?71
????????????????}
?72
?73
????????????}
?
catch
(SQLException?e)?
{}
?74
????????}
?75
?76
????????
return
?
false
;
?77
????}
?78
?79
????
public
?
static
?
boolean
?add_bm(ServletContext?application,?String?username,?String?new_url)?
{
?80
????????Connection?conn?
=
?getDBConnection(application);
?81
????????
if
(conn?
==
?
null
)?
{
?82
????????????
return
?
false
;
?83
????????}
?84
????????
try
?
{????????
?85
????????????
//
?乓琌狡
?86
????????????ResultSet?result?
=
?conn.createStatement().executeQuery(
"
SELECT?*?FROM?bookmark?WHERE?username='
"
?
+
?username?
+
?
"
'?AND?bm_URL='
"
?
+
?new_url?
+
?
"
'
"
);
?87
?88
????????????
if
(result.next())
?89
????????????????
return
?
false
;
?90
?91
????????????
int
?dn?
=
?conn.createStatement().executeUpdate(
"
INSERT?INTO?bookmark?VALUES('
"
?
+
?username?
+
?
"
','
"
?
+
?new_url?
+
?
"
')
"
);
?92
?93
????????????
if
(dn?
==
?
0
)
?94
????????????????
return
?
false
;
?95
????????}
?96
????????
catch
(SQLException?e)
{
?97
????????????
return
?
false
;
?98
????????}
?99
????????
return
?
true
;
100
????}
101
102
????
public
?
static
?
boolean
?delete_bm(ServletContext?application,?String?username,?String?url)?
{
103
????????Connection?conn?
=
?getDBConnection(application);
104
????????
if
(conn?
==
?
null
)?
{????
105
????????????
return
?
false
;
106
????????}
107
????????
try
?
{
108
????????????
int
?dn?
=
?conn.createStatement().executeUpdate(
"
DELETE?FROM?bookmark?WHERE?username='
"
?
+
?username?
+
?
"
'?AND?bm_URL='
"
?
+
?url?
+
?
"
'
"
);
109
????????????
if
(dn?
==
?
0
)
110
????????????????
return
?
false
;
111
????????}
112
????????
catch
(SQLException?e)
{
113
????????????
return
?
false
;
114
????????}
115
????????
return
?
true
;
116
????}
117
}
118

??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
