JSP:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>后台管理系统</TITLE>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META content="" name=keywords>
<META content=name=description>
<STYLE type=text/css>
.neon {
FILTER: glow(color = #002E60, strength = 3)
}DIV {
WIDTH: 70px
}BODY {
MARGIN: 0px
}BODY {
MARGIN-TOP: 0px;
SCROLLBAR-FACE-COLOR: #005fc5;
FONT-SIZE: 12px;
BACKGROUND: #ffffff;
SCROLLBAR-HIGHLIGHT-COLOR: #799ae1;
SCROLLBAR-SHADOW-COLOR: #799ae1;
SCROLLBAR-3DLIGHT-COLOR: #005fc5;
SCROLLBAR-ARROW-COLOR: #ffffff;
SCROLLBAR-TRACK-COLOR: #aabfec;
SCROLLBAR-DARKSHADOW-COLOR: #799ae1
}
</STYLE>
<LINK href="<%=path%>/images/duan_1.css" type=text/css rel=stylesheet>
<META content="MSHTML 6.00.2800.1106" name=GENERATOR>
<style type="text/css">
<!--
.style6 {
COLOR: #0000ff
}.STYLE7 {
COLOR: #003366;
font-size: 12px;
}
-->
</style>
<script type="text/javascript">
function dosubmit() {
var th = document.form1;
if (th.username.value == "") {
alert("用户名不能为空!!");
th.username.focus();
return;
}
if (th.realname.value == "") {
alert("姓名不能为空!!");
th.realname.focus();
return;
}
if (th.pswd.value == "") {
alert("密码不能为空!!");
th.pswd.focus();
return;
}
th.action="<%=path%>/servlet/RegisterAction";
th.submit();
}
</script>
</HEAD>
<BODY bgColor=#ffffff
onLoad="MM_preloadImages('<%=path%>/images/ok_2.jpg','<%=path%>/images/fh_2.jpg')">
<form action="" name="form1" method="post">
<TABLE height=470 cellSpacing=0 cellPadding=0 width=580 align=center
border=0>
<TBODY>
<TR>
<TD colSpan=3 height=9></TD>
</TR>
<TR>
<TD vAlign=top width=8 background="<%=path%>/images/dhpddw.gif"
rowSpan=2>
<!--DWLayoutEmptyCell-->
</TD>
<TD width=743 background="<%=path%>/images/h-1.gif" height=9></TD>
<TD width=9 height=9>
<IMG height=9 src="<%=path%>/images/jiao.gif" width=9>
</TD>
</TR>
<TR>
<TD vAlign=top align=right width=743 height=452>
<TABLE cellSpacing=0 cellPadding=0 width=556 border=0>
<!--DWLayoutTable-->
<TBODY>
<TR>
<TD vAlign=bottom width=548 height=27>
<IMG height=10 src="<%=path%>/images/jt2.gif" width=10>
<span class="lbt">用户注册</span>
</TD>
<TD width=8 rowSpan=3>
</TD>
</TR>
<TR>
<TD background="<%=path%>/images/ht.gif" height=22></TD>
</TR> <TR>
<TD class=unnamed1 vAlign=top height=9>
<TABLE width="99%" border=0 cellPadding=4 cellSpacing=1
bgColor=#0867b3>
<TBODY>
<TR bgColor=#ffffff height=20>
<TD width=14% noWrap class="STYLE7">
用户名
</TD>
<TD width=24% valign="top" noWrap>
<INPUT class=text2 maxLength=20 size=18 name="username"
minLength="1">
</TD>
<TD width=62% noWrap>
<span class="STYLE7">必须填写!</span>
</TD>
</TR>
<TR bgColor=#ffffff height=20>
<TD height="4" noWrap>
<span class="STYLE7">姓 名</span>
</TD>
<TD height="4" valign="top" noWrap>
<INPUT class=text2 maxLength=20 size=18 name="realname"
minLength="1">
</TD>
<TD height="4" noWrap>
<span class="STYLE7">必须填写!</span>
</TD>
</TR>
<TR bgColor=#ffffff height=20>
<TD height="2" noWrap>
<span class="STYLE7">密码 </span>
</TD>
<TD height="2" valign="top" noWrap>
<INPUT type="password" class=text2 maxLength=20 size=18
name="pswd" minLength="1">
</TD>
<TD height="2" noWrap>
<span class="STYLE7">必填项</span>
</TD>
</TR>
</TBODY>
</TABLE>
<br>
</TD>
</TR>
<TR>
<TD height=20 align="center">
<a href="javascript:dosubmit();"><img
src="<%=path%>/images/ok_1.jpg" name="Image8" width="60"
height="22" border="0">
</a>
<a href="<%=path%>/index.jsp"><img
src="<%=path%>/images/fh_1.jpg" name="Image9" width="60"
height="22" border="0">
</a>
</TD>
<TD></TD>
</TR>
</TBODY>
</TABLE>
</TD>
<TD width=9 background="<%=path%>/images/s-1.gif"></TD>
</TR>
</TBODY>
</TABLE>
</form>
<IFRAME name=top align=default src="<%=path%>/bottom.jsp"
frameBorder=0 width=100% scrolling=no height=88>
<h1>
</h1>
</IFRAME>
</BODY>
</HTML>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>后台管理系统</TITLE>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META content="" name=keywords>
<META content=name=description>
<STYLE type=text/css>
.neon {
FILTER: glow(color = #002E60, strength = 3)
}DIV {
WIDTH: 70px
}BODY {
MARGIN: 0px
}BODY {
MARGIN-TOP: 0px;
SCROLLBAR-FACE-COLOR: #005fc5;
FONT-SIZE: 12px;
BACKGROUND: #ffffff;
SCROLLBAR-HIGHLIGHT-COLOR: #799ae1;
SCROLLBAR-SHADOW-COLOR: #799ae1;
SCROLLBAR-3DLIGHT-COLOR: #005fc5;
SCROLLBAR-ARROW-COLOR: #ffffff;
SCROLLBAR-TRACK-COLOR: #aabfec;
SCROLLBAR-DARKSHADOW-COLOR: #799ae1
}
</STYLE>
<LINK href="<%=path%>/images/duan_1.css" type=text/css rel=stylesheet>
<META content="MSHTML 6.00.2800.1106" name=GENERATOR>
<style type="text/css">
<!--
.style6 {
COLOR: #0000ff
}.STYLE7 {
COLOR: #003366;
font-size: 12px;
}
-->
</style>
<script type="text/javascript">
function dosubmit() {
var th = document.form1;
if (th.username.value == "") {
alert("用户名不能为空!!");
th.username.focus();
return;
}
if (th.realname.value == "") {
alert("姓名不能为空!!");
th.realname.focus();
return;
}
if (th.pswd.value == "") {
alert("密码不能为空!!");
th.pswd.focus();
return;
}
th.action="<%=path%>/servlet/RegisterAction";
th.submit();
}
</script>
</HEAD>
<BODY bgColor=#ffffff
onLoad="MM_preloadImages('<%=path%>/images/ok_2.jpg','<%=path%>/images/fh_2.jpg')">
<form action="" name="form1" method="post">
<TABLE height=470 cellSpacing=0 cellPadding=0 width=580 align=center
border=0>
<TBODY>
<TR>
<TD colSpan=3 height=9></TD>
</TR>
<TR>
<TD vAlign=top width=8 background="<%=path%>/images/dhpddw.gif"
rowSpan=2>
<!--DWLayoutEmptyCell-->
</TD>
<TD width=743 background="<%=path%>/images/h-1.gif" height=9></TD>
<TD width=9 height=9>
<IMG height=9 src="<%=path%>/images/jiao.gif" width=9>
</TD>
</TR>
<TR>
<TD vAlign=top align=right width=743 height=452>
<TABLE cellSpacing=0 cellPadding=0 width=556 border=0>
<!--DWLayoutTable-->
<TBODY>
<TR>
<TD vAlign=bottom width=548 height=27>
<IMG height=10 src="<%=path%>/images/jt2.gif" width=10>
<span class="lbt">用户注册</span>
</TD>
<TD width=8 rowSpan=3>
</TD>
</TR>
<TR>
<TD background="<%=path%>/images/ht.gif" height=22></TD>
</TR> <TR>
<TD class=unnamed1 vAlign=top height=9>
<TABLE width="99%" border=0 cellPadding=4 cellSpacing=1
bgColor=#0867b3>
<TBODY>
<TR bgColor=#ffffff height=20>
<TD width=14% noWrap class="STYLE7">
用户名
</TD>
<TD width=24% valign="top" noWrap>
<INPUT class=text2 maxLength=20 size=18 name="username"
minLength="1">
</TD>
<TD width=62% noWrap>
<span class="STYLE7">必须填写!</span>
</TD>
</TR>
<TR bgColor=#ffffff height=20>
<TD height="4" noWrap>
<span class="STYLE7">姓 名</span>
</TD>
<TD height="4" valign="top" noWrap>
<INPUT class=text2 maxLength=20 size=18 name="realname"
minLength="1">
</TD>
<TD height="4" noWrap>
<span class="STYLE7">必须填写!</span>
</TD>
</TR>
<TR bgColor=#ffffff height=20>
<TD height="2" noWrap>
<span class="STYLE7">密码 </span>
</TD>
<TD height="2" valign="top" noWrap>
<INPUT type="password" class=text2 maxLength=20 size=18
name="pswd" minLength="1">
</TD>
<TD height="2" noWrap>
<span class="STYLE7">必填项</span>
</TD>
</TR>
</TBODY>
</TABLE>
<br>
</TD>
</TR>
<TR>
<TD height=20 align="center">
<a href="javascript:dosubmit();"><img
src="<%=path%>/images/ok_1.jpg" name="Image8" width="60"
height="22" border="0">
</a>
<a href="<%=path%>/index.jsp"><img
src="<%=path%>/images/fh_1.jpg" name="Image9" width="60"
height="22" border="0">
</a>
</TD>
<TD></TD>
</TR>
</TBODY>
</TABLE>
</TD>
<TD width=9 background="<%=path%>/images/s-1.gif"></TD>
</TR>
</TBODY>
</TABLE>
</form>
<IFRAME name=top align=default src="<%=path%>/bottom.jsp"
frameBorder=0 width=100% scrolling=no height=88>
<h1>
</h1>
</IFRAME>
</BODY>
</HTML>
package com.product.dbutil.register.action;import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import com.product.dbutil.register.dao.RegisterDao;
import com.product.dbutil.register.service.RegisterService;
public class RegisterAction extends HttpServlet { private RegisterService service; /**
* Constructor of the object.
*/
public RegisterAction() {
super();
} /**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
} /**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { this.doPost(request, response);//第53行
} /**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { String path = request.getContextPath();
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
String username = request.getParameter("username");
String pswd = request.getParameter("pswd");
String realname = request.getParameter("realname");
List<Object> params = new ArrayList<Object>();
params.add(username);
params.add(pswd);
params.add(realname);
boolean flag = service.registerUser(params);
if (flag) //第85行 {
response.sendRedirect(path + "/index.jsp");
}
out.flush();
out.close();
} /**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occurs
*/
public void init() throws ServletException {
// Put your code here
service = new RegisterDao();
}}
RegisterDao:
package com.product.dbutil.register.dao;import java.util.List;import com.product.dbutil.jdbc.JdbcUtils;
import com.product.dbutil.register.service.RegisterService;public class RegisterDao implements RegisterService { private JdbcUtils utils=null ; public RegisterDao() {
utils = new JdbcUtils();
} /*
* 完成了对用户注册的dao的编写
*
* @see
* com.product.dbutil.register.service.RegisterService#registerUser(java
* .util.List)
*/
public boolean registerUser(List<Object> params) {
// TODO Auto-generated method stub
boolean flag = false;
utils.getConnection();
String sql = "insert into userinfo(username,pswd,realname) values(?,?,?)";
try {
// 先获得链接
flag = utils.updateByPreparedStatement(sql, params);//第31行
//System.out.println("-flag-->>" + flag);
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
} finally {
// 关闭数据库的链接
utils.releaseConn();
}
return flag;
}
}
RegisterService:
package com.product.dbutil.register.service;import java.util.List;public interface RegisterService { //完成用户的注册功能
public boolean registerUser(List<Object> params);
}
package com.product.dbutil.jdbc;import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;import org.apache.commons.lang.ObjectUtils.Null;public class JdbcUtils { // 表示定义数据库的用户名
private final String USERNAME = "root";
// 定义数据库的密码
private final String PASSWORD = "1234";
// 定义数据库的驱动信息
private final String DRIVER = "com.mysql.jdbc.Driver";
// 定义访问数据库的地址
private final String URL = "jdbc:mysql://localhost:3306/mydb";
// 定义数据库的链接
private Connection connection;
// 定义sql语句的执行对象
private PreparedStatement pstmt;
// 定义查询返回的结果集合
private ResultSet resultSet;
// 实现批处理操作的功能
private Statement stmt; public JdbcUtils() {
try {
Class.forName(DRIVER);
System.out.println("注册驱动成功!!");
} catch (Exception e) {
// TODO: handle exception
}
} // 定义获得数据库的链接
public Connection getConnection() {
try {
connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch (Exception e) {
e.printStackTrace();
}
return connection;
} public boolean deleteByBatch(String[] sql) throws SQLException{
boolean flag = false;
stmt = connection.createStatement();
if(sql!=null){
for(int i=0;i<sql.length;i++){
stmt.addBatch(sql[i]);
}
}
int[] count = stmt.executeBatch();
if(count!=null){
flag = true;
}
return flag;
}
/**
* 完成对数据库的表的添加删除和修改的操作
*
* @param sql
* @param params
* @return
* @throws SQLException
*/
public boolean updateByPreparedStatement(String sql, List<Object> params)
throws SQLException {
boolean flag = false;//第80行
pstmt = connection.prepareStatement(sql);
int index = 1;
int result =-1;
if (params != null && !params.isEmpty()) {
for (int i = 0; i < params.size(); i++) {
pstmt.setObject(index++, params.get(i));
}
}
result = pstmt.executeUpdate();
flag = result > 0 ? true : false;
return flag;
} /**
* 查询返回单条记录
*
* @param sql
* @param params
* @return
* @throws SQLException
*/
public Map<String, Object> findSimpleResult(String sql, List<Object> params)
throws SQLException {
Map<String, Object> map = new HashMap<String, Object>();
int index = 1;
pstmt = connection.prepareStatement(sql);
if (params != null && !params.isEmpty()) {
for (int i = 0; i < params.size(); i++) {
pstmt.setObject(index++, params.get(i));
}
}
resultSet = pstmt.executeQuery();// 返回查询结果
ResultSetMetaData metaData = resultSet.getMetaData();
int col_len = metaData.getColumnCount();// 获得列的名称
while (resultSet.next()) {
for (int i = 0; i < col_len; i++) {
String cols_name = metaData.getColumnName(i + 1);
Object cols_value = resultSet.getObject(cols_name);
if (cols_value == null) {
cols_value = "";
}
map.put(cols_name, cols_value);
}
}
return map;
} /**
* 查询返回多行记录
*
* @param sql
* @param params
* @return
* @throws SQLException
*/
public List<Map<String, Object>> findMoreResult(String sql,
List<Object> params) throws SQLException {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
int index = 1;
pstmt = connection.prepareStatement(sql);
if (params != null && !params.isEmpty()) {
for (int i = 0; i < params.size(); i++) {
pstmt.setObject(index++, params.get(i));
}
}
resultSet = pstmt.executeQuery();
ResultSetMetaData metaData = resultSet.getMetaData();
int cols_len = metaData.getColumnCount();
while (resultSet.next()) {
Map<String, Object> map = new HashMap<String, Object>();
for (int i = 0; i < cols_len; i++) {
String cols_name = metaData.getColumnName(i + 1);
Object cols_value = resultSet.getObject(cols_name);
if (cols_value == null) {
cols_value = "";
}
map.put(cols_name, cols_value);
}
list.add(map);
}
return list;
} // jdbc的封装可以用反射机制来封装:
public <T> T findSimpleRefResult(String sql, List<Object> params,
Class<T> cls) throws Exception {
T resultObject = null;
int index = 1;
pstmt = connection.prepareStatement(sql);
if (params != null && !params.isEmpty()) {
for (int i = 0; i < params.size(); i++) {
pstmt.setObject(index++, params.get(i));
}
}
resultSet = pstmt.executeQuery();
ResultSetMetaData metaData = resultSet.getMetaData();
int cols_len = metaData.getColumnCount();
while (resultSet.next()) {
// 通过反射机制创建实例
resultObject = cls.newInstance();
for (int i = 0; i < cols_len; i++) {
String cols_name = metaData.getColumnName(i + 1);
Object cols_value = resultSet.getObject(cols_name);
if (cols_value == null) {
cols_value = "";
}
Field field = cls.getDeclaredField(cols_name);
field.setAccessible(true);// 打开javabean的访问private权限
field.set(resultObject, cols_value);
}
}
return resultObject;
} /**
* 通过反射机制访问数据库
*
* @param <T>
* @param sql
* @param params
* @param cls
* @return
* @throws Exception
*/
public <T> List<T> findMoreRefResult(String sql, List<Object> params,
Class<T> cls) throws Exception {
List<T> list = new ArrayList<T>();
int index = 1;
pstmt = connection.prepareStatement(sql);
if (params != null && !params.isEmpty()) {
for (int i = 0; i < params.size(); i++) {
pstmt.setObject(index++, params.get(i));
}
}
resultSet = pstmt.executeQuery();
ResultSetMetaData metaData = resultSet.getMetaData();
int cols_len = metaData.getColumnCount();
while (resultSet.next()) {
T resultObject = cls.newInstance();
for (int i = 0; i < cols_len; i++) {
String cols_name = metaData.getColumnName(i + 1);
Object cols_value = resultSet.getObject(cols_name);
if (cols_value == null) {
cols_value = "";
}
Field field = cls.getDeclaredField(cols_name);
field.setAccessible(true);
field.set(resultObject, cols_value);
}
list.add(resultObject);
}
return list;
} public void releaseConn() {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
at com.product.dbutil.jdbc.JdbcUtils.updateByPreparedStatement(JdbcUtils.java:80)
at com.product.dbutil.register.dao.RegisterDao.registerUser(RegisterDao.java:31)
at com.product.dbutil.register.action.RegisterAction.doPost(RegisterAction.java:85)
at com.product.dbutil.register.action.RegisterAction.doGet(RegisterAction.java:53)
楼主 把你的 RegisterAction.java 第85行 53行 RegisterDao.java 31行的代码贴出来看看啊
connection这个类的对象你在哪初始化的,看看是不是为null
pstmt = connection.prepareStatement(sql);
貌似没给utils赋值啊,utils应该是空的吧
flag = utils.updateByPreparedStatement(sql, params);//第31行
我估计是你的utils 为空 ,你可以打个断点看看
utils = new JdbcUtils();
}
将这段先隐藏然后在
public boolean registerUser(List<Object> params)
在这个方法里面第一行加入;你试一试看行不 具体为什么我也说不好。等待大神解释
utils = new JdbcUtils();