第一步:在openfund工程下建立包edu.fzu.openfund.utils。在edu.fzu.openfund.utils包下新建类DBUtils,实现getConn和releaseResource方法。
public class DBUtils {
private static final String CONN_URL = "jdbc:mysql://localhost:3306/openfunddb";
private static final String USERNAME = "root";
private static final String PASSWORD = "123456";
public static Connection getConn()
{
Connection conn = null;
try
{
Class.forName("com.mysql.jdbc.Driver");
conn= DriverManager.getConnection(CONN_URL,USERNAME,PASSWORD);
} catch (ClassNotFoundException e)
{
e.printStackTrace();
} catch (SQLException e)
{
e.printStackTrace();
}
return conn;
}
public static void releaseResource(Connection conn, PreparedStatement pstmt,ResultSet rset)
{
try
{
if(conn!=null)
conn.close();
if(pstmt!=null)
pstmt.close();
if(rset!=null)
rset.close();
} catch (SQLException e)
{
e.printStackTrace();
}
}
}
第二步:在
openfund工程下建立包edu.fzu.openfund.dao。在edu.fzu.openfund.dao下新建接口ClientDao、新建类ClientDaoJDBCImpl实现ClientDao接口。
public class ClientDaoJDBCImpl implements ClientDao {
private static String insertSql = "insert into client value(?,?,?,?,?,?,?,?)";
public int createClient(Client client) {
try {
Connection conn=DBUtils.getConn();
PreparedStatement pstmt=conn.prepareStatement(insertSql);
pstmt.setString(1, client.getIDCardNO());
pstmt.setString(2, client.getClientName());
pstmt.setString(3, client.getClientSex());
pstmt.setString(4, client.getClientPhone());
pstmt.setString(5, client.getClientAddress());
pstmt.setString(6, client.getClientEmail());
pstmt.setString(7, client.getClientHobby());
Date date =new Date(client.getClientCreateDate().getTime());
pstmt.setDate(8, date);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
第三步:在openfund工程下建立包edu.fzu.openfund.service。新建接口ClientService、新建类ClientServiceImpl实现ClientService接口。
public class ClientServiceImpl implements ClientService {
public int createClient(Client client) {
ClientDao clientDao = new ClientDaoJDBCImpl();
List list=clientDao.getClientByID(client.getIDCardNO());
if(list!=null)
return 0;
else
return clientDao.createClient(client);
}
第四步:修改
ClientMgrServlet类的代码。
else if (mid == 2) {
ClientService clientService=new ClientServiceImpl();
List all =clientService.getAllClient();
request.setAttribute("list",all);
request.getRequestDispatcher("/Client/showClients.jsp?rid=0").forward(request, response);
}
第五步:
编写
showClients.jsp。
<body>
<center>
<font size=7>客户详情列表</font>
<table border="1">
<tr>
<td>身份证号</td>
<td>姓名</td>
<td>性别</td>
<td>手机号码</td>
<td>地址</td>
<td>电子邮箱</td>
<td>爱好</td>
<td>创建日期</td>
<td>操作</td>
</tr>
<%
List all = (ArrayList) request.getAttribute("list");
Client client = null;
Iterator it = all.iterator();
while (it.hasNext()) {
client = (Client) it.next();
%>
<tr>
<td><%=client.getIDCardNO()%></td>
<td><%=client.getClientName()%></td>
<td><%=client.getClientSex()%></td>
<td><%=client.getClientPhone()%></td>
<td><%=client.getClientAddress()%></td>
<td><%=client.getClientEmail()%></td>
<td><%=client.getClientHobby()%></td>
<td><%=client.getClientCreateDate()%></td>
<td>
<form name="f">
<input type="button" name="Delete" value="删除" οnclick=Del>
<input type="button" name="Edit" value="修改" οnclick=Edit>
</form>
</td>
</tr>
<%
}
%>
</table>
</body>
第六步:编写left.jsp。
<span class="STYLE3"><a href="clientMgr?mid=0" target="contentFrame">新客户开户</span>
<span class="STYLE3"><a href="clientMgr?mid=2" target="contentFrame">客户信息列表</span>