宸ュ??璁捐?★?
????浣跨?ㄥ伐??璁捐?★?浣垮???颁??ㄧ?ラ???蜂?瀛?绫绘??璋?锛??????版?剧ず涓????伴?昏???浣???绂伙?渚夸?淇??广??
瀹?渚?锛???AO璁捐?℃ā寮?涓????ュ伐??璁捐??
DAOFactory.java:浣跨?ㄥ伐??璁捐?¤?剧疆瀛?绫? view source <embed id="highlighter_772179_clipboard" title="copy to clipboard" type="application/x-shockwave-flash" width="16" height="16" src="http://mgc.ahau.edu.cn/common/SyntaxHighlighter/scripts/clipboard.swf" allowscriptaccess="always" wmode="transparent" flashvars="highlighterId=highlighter_772179" menu="false"></embed> print ?
01.
package
?
mgc.dao.factory;
02.
?
03.
import
?
mgc.dao.test.*;
04.
import
?
mgc.dao.test.impl.*;
05.
public
?
class
?
DAOFactory?{
06.
????
public
?
static
?
MemberDAO?getMemberDAOInstance()?{
07.
????????
return
?
new
?
MemberDAOImpl();
08.
????
}
09.
}
MemberDAO.java:?版??搴???浣??ュ?g被
view source <embed id="highlighter_76257_clipboard" title="copy to clipboard" type="application/x-shockwave-flash" width="16" height="16" src="http://mgc.ahau.edu.cn/common/SyntaxHighlighter/scripts/clipboard.swf" allowscriptaccess="always" wmode="transparent" flashvars="highlighterId=highlighter_76257" menu="false"></embed> print ?
01.
package
?
mgc.dao.test;
02.
?
03.
import
?
java.util.*;
04.
import
?
mgc.dao.vo.*;
05.
//瑙?瀹???浣?member琛ㄧ???ㄩ?ㄦ?规?
06.
public
?
interface
?
MemberDAO?{
07.
????
//???ユ?版??
08.
????
public
?
void
?
insert(Member?member)?
throws
?
Exception;
09.
????
//淇??规?版??
10.
????
public
?
void
?
update(Member?member)?
throws
?
Exception;
11.
????
//???ゆ?版??
12.
????
public
?
void
?
delete(
int
?
id)?
throws
?
Exception;
13.
????
//??ID?ヨ??/code>
14.
????
public
?
Member?queryById(
int
?
id)?
throws
?
Exception;
15.
????
//?ㄩ?ㄦ?ヨ??/code>
16.
????
public
?
List?queryAll()?
throws
?
Exception;
17.
}
Member.java:VO瀵硅薄绫? view source <embed id="highlighter_308880_clipboard" title="copy to clipboard" type="application/x-shockwave-flash" width="16" height="16" src="http://mgc.ahau.edu.cn/common/SyntaxHighlighter/scripts/clipboard.swf" allowscriptaccess="always" wmode="transparent" flashvars="highlighterId=highlighter_308880" menu="false"></embed> print ?
01.
package
?
mgc.dao.vo;
02.
?
03.
public
?
class
?
Member?{
04.
????
private
?
int
?
id;
05.
????
private
?
String?username;
06.
????
private
?
String?password;
07.
????
?
08.
????
public
?
void
?
setId(
int
?
id){
09.
????????
this
.id=id;
10.
????
}
11.
????
public
?
void
?
setUsername(String?username){
12.
????????
this
.username=username;
13.
????
}
14.
????
public
?
void
?
setPassword(String?password){
15.
????????
this
.password=password;
16.
????
}
17.
????
public
?
int
?
getId(){
18.
????????
return
?
this
.id;
19.
????
}
20.
????
public
?
String?getUsername(){
21.
????????
return
?
this
.username;
22.
????
}
23.
????
public
?
String?getPassword(){
24.
????????
return
?
this
.password;
25.
????
}
26.
}
DatabaseConnection.java:?版??搴?杩??ョ被
view source <embed id="highlighter_592693_clipboard" title="copy to clipboard" type="application/x-shockwave-flash" width="16" height="16" src="http://mgc.ahau.edu.cn/common/SyntaxHighlighter/scripts/clipboard.swf" allowscriptaccess="always" wmode="transparent" flashvars="highlighterId=highlighter_592693" menu="false"></embed> print ?
01.
package
?
mgc.dao.test.dbc;
02.
?
03.
import
?
java.sql.*;
04.
?
05.
public
?
class
?
DataBaseConnection?{
06.
????
?
07.
????
private
?
final
?
String?DBDRIVER=
"sun.jdbc.odbc.JdbcOdbcDriver"
;
08.
????
private
?
final
?
String?DBURL=
"jdbc:odbc:member"
;
09.
????
private
?
Connection?conn=
null
;
10.
????
?
11.
????
public
?
DataBaseConnection()?{
12.
????????
try
{
13.
????????????
Class.forName(DBDRIVER);
14.
????????????
this
.conn=DriverManager.getConnection(DBURL);
15.
????????
}
16.
????????
catch
(Exception?e){}
17.
????
}
18.
????
?
19.
????
public
?
Connection?getConnection()?{
20.
????????
return
?
this
.conn;
21.
????
}
22.
????
?
23.
????
public
?
void
?
close()?{
24.
????????
try
{
25.
????????????
this
.conn.close();
26.
????????
}
27.
????????
catch
(Exception?e){}
28.
????
}
29.
}
MemberDAOImpl.java:?版??搴???浣?绫? ?
001.
package
?
mgc.dao.test.impl;
002.
?
003.
import
?
java.util.*;
004.
import
?
java.sql.*;
005.
import
?
mgc.dao.vo.*;
006.
import
?
mgc.dao.test.*;
007.
import
?
mgc.dao.test.dbc.*;
008.
?
009.
public
?
class
?
MemberDAOImpl?
implements
?
MemberDAO?{
010.
????
?
011.
????
//???ユ?版??
012.
????
public
?
void
?
insert(Member?member)?
throws
?
Exception?{
013.
????????
String?sql=
"Insert?INTO?member?(username,password)?VALUES?(?,?)"
;
014.
????????
PreparedStatement?pstmt=
null
;
015.
????????
DataBaseConnection?dbc=
null
;
016.
????????
try
{
017.
????????????
//杩??ユ?版??搴?
018.
????????????
dbc=
new
?
DataBaseConnection();
019.
????????????
pstmt=dbc.getConnection().prepareStatement(sql);
020.
????????????
pstmt.setString(
1
,?member.getUsername());
021.
????????????
pstmt.setString(
2
,?member.getPassword());
022.
????????????
//??浣??版??搴?
023.
????????????
pstmt.executeUpdate();
024.
????????????
pstmt.close();
025.
????????
}
026.
????????
catch
(Exception?e)?{
027.
????????????
throw
?
new
?
Exception(e);
028.
????????
}
029.
????????
finally
{
030.
????????????
//?抽???版??搴?
031.
????????????
dbc.close();
032.
????????
}
033.
????
}
034.
????
?
035.
????
//淇??规??浣?
036.
????
public
?
void
?
update(Member?member)?
throws
?
Exception?{
037.
????????
String?sql=
"Update??member?SET?username=?,password=??Where?ID=?"
;
038.
????????
PreparedStatement?pstmt=
null
;
039.
????????
DataBaseConnection?dbc=
null
;
040.
????????
try
{
041.
????????????
//杩??ユ?版??搴?
042.
????????????
dbc=
new
?
DataBaseConnection();
043.
????????????
pstmt=dbc.getConnection().prepareStatement(sql);
044.
????????????
pstmt.setString(
1
,?member.getUsername());
045.
????????????
pstmt.setString(
2
,?member.getPassword());
046.
????????????
pstmt.setInt(
3
,member.getId());
047.
????????????
//??浣??版??搴?
048.
????????????
pstmt.executeUpdate();
049.
????????????
pstmt.close();
050.
????????
}
051.
????????
catch
(Exception?e)?{
052.
????????????
throw
?
new
?
Exception(e);
053.
????????
}
054.
????????
finally
{
055.
????????????
//?抽???版??搴?
056.
????????????
dbc.close();
057.
????????
}
058.
????
}
059.
????
?
060.
????
public
?
void
?
delete(
int
?
id)?
throws
?
Exception?{
061.
????????
String?sql=
"Delete?FROM?member?Where?ID=?"
;
062.
????????
PreparedStatement?pstmt=
null
;
063.
????????
DataBaseConnection?dbc=
null
;
064.
????????
?
065.
????????
try
{
066.
????????????
//杩??ユ?版??搴?
067.
????????????
dbc=
new
?
DataBaseConnection();
068.
????????????
pstmt=dbc.getConnection().prepareStatement(sql);
069.
????????????
pstmt.setInt(
1
,?id);
070.
????????????
//??浣??版??搴?
071.
????????????
pstmt.executeUpdate();
072.
????????????
pstmt.close();
073.
????????
}
074.
????????
catch
(Exception?e)?{
075.
????????????
throw
?
new
?
Exception(e);
076.
????????
}
077.
????????
finally
{
078.
????????????
//?抽???版??搴?
079.
????????????
dbc.close();
080.
????????
}
081.
????
}
082.
????
?
083.
????
public
?
Member?queryById(
int
?
id)?
throws
?
Exception?{
084.
????????
String?sql=
"Select?*?FROM?member?Where?ID=?"
;
085.
????????
PreparedStatement?pstmt=
null
;
086.
????????
DataBaseConnection?dbc=
null
;
087.
????????
ResultSet?rs=
null
;
088.
????????
Member?member=
null
;
089.
????????
try
{
090.
????????????
//杩??ユ?版??搴?
091.
????????????
dbc=
new
?
DataBaseConnection();
092.
????????????
pstmt=dbc.getConnection().prepareStatement(sql);
093.
????????????
pstmt.setInt(
1
,?id);
094.
????????????
//??浣??版??搴?
095.
????????????
rs=pstmt.executeQuery();
096.
????????????
if
(rs.next())?{
097.
????????????????
//灏??ヨ?㈢???淇?瀛???ember瀵硅薄涓?
098.
????????????????
member=
new
?
Member();
099.
????????????????
member.setUsername(rs.getString(
"username"
));
100.
????????????????
member.setPassword(rs.getString(
"password"
));
101.
????????????
}
102.
????????????
rs.close();
103.
????????????
pstmt.close();
104.
????????
}
105.
????????
catch
(Exception?e)?{
106.
????????????
throw
?
new
?
Exception(e);
107.
????????
}
108.
????????
finally
{
109.
????????????
//?抽???版??搴?
110.
????????????
dbc.close();
111.
????????
}
112.
????????
return
?
member;
113.
????
}
114.
????
?
115.
????
public
?
List?queryAll()?
throws
?
Exception?{
116.
????????
String?sql=
"Select?*?FROM?member"
;
117.
????????
PreparedStatement?pstmt=
null
;
118.
????????
DataBaseConnection?dbc=
null
;
119.
????????
ResultSet?rs=
null
;
120.
????????
List?all=
new
?
ArrayList();
121.
????????
try
{
122.
????????????
//杩??ユ?版??搴?
123.
????????????
dbc=
new
?
DataBaseConnection();
124.
????????????
pstmt=dbc.getConnection().prepareStatement(sql);
125.
????????????
//??浣??版??搴?
126.
????????????
rs=pstmt.executeQuery();
127.
????????????
while
(rs.next())?{
128.
????????????????
//灏??ヨ?㈢???淇?瀛???ember瀵硅薄涓?
129.
????????????????
Member?member=
new
?
Member();
130.
????????????????
member.setUsername(rs.getString(
"username"
));
131.
????????????????
member.setPassword(rs.getString(
"password"
));
132.
????????????????
all.add(member);
133.
????????????
}
134.
????????????
rs.close();
135.
????????????
pstmt.close();
136.
????????
}
137.
????????
catch
(Exception?e)?{
138.
????????????
throw
?
new
?
Exception(e);
139.
????????
}
140.
????????
finally
{
141.
????????????
//?抽???版??搴?
142.
????????????
dbc.close();
143.
????????
}
144.
????????
return
?
all;
145.
????
}
146.
}
dao.jsp:???版?剧ず椤甸?? view source <embed id="highlighter_126215_clipboard" title="copy to clipboard" type="application/x-shockwave-flash" width="16" height="16" src="http://mgc.ahau.edu.cn/common/SyntaxHighlighter/scripts/clipboard.swf" allowscriptaccess="always" wmode="transparent" flashvars="highlighterId=highlighter_126215" menu="false"></embed> print ?
01.
<%@page?contentType="text/html;charset=GB2312"?%>
02.
<%@page?import="java.util.*"?%>
03.
<%@page?import="mgc.dao.factory.*"?%>
04.
<%@page?import="mgc.dao.test.impl.*"?%>
05.
<%@page?import="mgc.dao.vo.*"?%>
06.
<
html
>
07.
??
<
head
>
08.
????
<
title
>dao.jsp</
title
>