求详细的解释jsp操作mysql 实现 增加 修改 删除功能 希望有详细的代码和注释帮解释一些 小弟初学感激不尽
2个回答
2011-11-21
展开全部
//连接数据库
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class ConnDB {
public Connection conn = null;
public Statement stmt = null;
public ResultSet rs = null;
private static String propFileName = "/com/connDB.properties"; //指定资源文件保存的位置
private static Properties prop = new Properties();
private static String dbClassName ="com.mysql.jdbc.Driver";
private static String dbUrl =
"jdbc:mysql://127.0.0.1:3306/db_mydb?user=root&password=111&useUnicode=true";
public ConnDB(){
try {
InputStream in=getClass().getResourceAsStream(propFileName);
prop.load(in); //通过输入流对象加载Properties文件
dbClassName = prop.getProperty("DB_CLASS_NAME"); //获取数据库驱动
dbUrl = prop.getProperty("DB_URL",
"jdbc:mysql://127.0.0.1:3306/db_librarySys?user=root&password=111&useUnicode=true");
}
catch (Exception e) {
e.printStackTrace(); //输出异常信息
}
}
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName(dbClassName).newInstance();
conn = DriverManager.getConnection(dbUrl);
}
catch (Exception ee) {
ee.printStackTrace();
}
if (conn == null) {
System.err.println(
"警告: DbConnectionManager.getConnection() 获得数据库链接失败.\r\n\r\n链接类型:" +
dbClassName + "\r\n链接位置:" + dbUrl);
}
return conn;
}
/*
* 功能:执行查询语句
*/
public ResultSet executeQuery(String sql) {
try {
conn = getConnection();
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
} catch (SQLException ex) {
System.err.println(ex.getMessage());
}
return rs;
}
/*
* 功能:执行更新操作
*/
public int executeUpdate(String sql) {
int result = 0;
try {
conn = getConnection(); //调用getConnection()方法构造Connection对象的一个实例conn
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
result = stmt.executeUpdate(sql); //执行更新操作
} catch (SQLException ex) {
result = 0;
}
return result;
}
/*
* 功能:关闭数据库的连接
*/
public void close() {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e) {
e.printStackTrace(System.err);
}
}
}
//一部分的操作
import org.apache.struts.action.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.actionForm.ManagerForm;
import com.dao.ManagerDAO;
import javax.servlet.http.HttpSession;
public class Manager extends Action {
private ManagerDAO managerDAO = null;
public Manager() {
this.managerDAO = new ManagerDAO();
}
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
String action = request.getParameter("action");
System.out.println("获取的查询字符串:" + action);
if (action == null || "".equals(action)) {
return mapping.findForward("error");
} else if ("login".equals(action)) {
return managerLogin(mapping, form, request, response);
} else if ("managerAdd".equals(action)) {
return managerAdd(mapping, form, request, response);
} else if ("managerQuery".equals(action)) {
return managerQuery(mapping, form, request, response);
} else if ("managerModifyQuery".equals(action)) {
return managerModifyQuery(mapping, form, request, response);
} else if ("managerModify".equals(action)) {
return managerModify(mapping, form, request, response);
} else if ("managerDel".equals(action)) {
return managerDel(mapping, form, request, response);
} else if ("querypwd".equals(action)) {
return pwdQuery(mapping, form, request, response);
} else if ("modifypwd".equals(action)) {
return modifypwd(mapping, form, request, response);
}
request.setAttribute("error", "操作失败!");
return mapping.findForward("error");
}
// 管理员身份验证
public ActionForward managerLogin(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
ManagerForm managerForm = (ManagerForm) form;
managerForm.setName(managerForm.getName());
managerForm.setPwd(managerForm.getPwd());
int ret = managerDAO.checkManager(managerForm);
System.out.print("验证结果ret的值:" + ret);
if (ret == 1) {
HttpSession session = request.getSession();
session.setAttribute("manager", managerForm.getName());
return mapping.findForward("managerLoginok");
} else {
request.setAttribute("error", "您输入的管理员名称或密码错误!");
return mapping.findForward("error");
}
}
// 查询管理员信息
private ActionForward managerQuery(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
String str = null;
request.setAttribute("managerQuery", managerDAO.query(str));
return mapping.findForward("managerQuery");
}
// 添加管理员信息
private ActionForward managerAdd(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
ManagerForm managerForm = (ManagerForm) form;
managerForm.setName(managerForm.getName()); // 获取设置管理员名称
managerForm.setPwd(managerForm.getPwd()); // 获取并设置密码
int ret = managerDAO.insert(managerForm); // 调用添加管理员信息
if (ret == 1) {
return mapping.findForward("managerAdd"); // 转到管理员信息添加成功页面
} else if (ret == 2) {
request.setAttribute("error", "该管理员信息已经添加!"); // 将错误信息保存到error参数中
return mapping.findForward("error"); // 转到错误提示页面
} else {
request.setAttribute("error", "添加管理员信息失败!"); // 将错误信息保存到error参数中
return mapping.findForward("error"); // 转到错误提示页面
}
}
// 查询修改管理员信息
private ActionForward managerModifyQuery(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) {
ManagerForm managerForm = (ManagerForm) form;
managerForm.setId(Integer.valueOf(request.getParameter("id")));//获取并设置管理ID号
System.out.print("查询到的id:" + request.getParameter("id"));
request.setAttribute("managerQueryif", managerDAO
.query_update(managerForm));
return mapping.findForward("managerQueryModify"); //转到权限设置成功页面
}
// 修改密码时查询
private ActionForward pwdQuery(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
ManagerForm managerForm = (ManagerForm) form;
HttpSession session = request.getSession();
String manager = (String) session.getAttribute("manager");
managerForm.setName(manager);
System.out.print("查询到的manager:" + manager);
request.setAttribute("pwdQueryif", managerDAO.query_pwd(managerForm));
return mapping.findForward("pwdQueryModify");
}
// 修改管理员信息
private ActionForward managerModify(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
ManagerForm managerForm = (ManagerForm) form;
managerForm.setId(managerForm.getId()); //获取并设置管理员ID号
managerForm.setName(managerForm.getName()); //获取并设置管理员名称
managerForm.setPwd(managerForm.getPwd()); //获取并设置管理员密码
managerForm.setSysset(managerForm.getSysset()); //获取并设置系统设置权限
managerForm.setReaderset(managerForm.getReaderset()); //获取并设置读者管理权限
managerForm.setBookset(managerForm.getBookset()); //获取并设置图书管理权限
managerForm.setBorrowback(managerForm.getBorrowback()); //获取并设置图书借还权限
managerForm.setSysquery(managerForm.getSysquery()); //获取并设置系统查询权限
int ret = managerDAO.update(managerForm); //调用设置管理员权限的方法
if (ret == 0) {
request.setAttribute("error", "设置管理员权限失败!"); //保存错误提示信息到error参数中
return mapping.findForward("error"); //转到错误提示页面
} else {
return mapping.findForward("managerModify"); //转到权限设置成功页面
}
}
// 删除管理员信息
private ActionForward managerDel(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
ManagerForm managerForm = (ManagerForm) form;
managerForm.setId(Integer.valueOf(request.getParameter("id"))); //获取并设置管理员ID号
int ret = managerDAO.delete(managerForm); //调用删除信息的方法delete()
if (ret == 0) {
request.setAttribute("error", "删除管理员信息失败!"); //保存错误提示信息到error参数中
return mapping.findForward("error"); //转到错误提示页面
} else {
return mapping.findForward("managerDel"); //转到删除管理员信息成功页面
}
}
// 修改管理员密码
private ActionForward modifypwd(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
ManagerForm managerForm = (ManagerForm) form;
managerForm.setName(managerForm.getName());
managerForm.setPwd(managerForm.getPwd());
int ret = managerDAO.updatePwd(managerForm);
if (ret == 0) {
request.setAttribute("error", "更改口令失败!");
return mapping.findForward("error");
} else {
return mapping.findForward("pwdModify");
}
}
}
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class ConnDB {
public Connection conn = null;
public Statement stmt = null;
public ResultSet rs = null;
private static String propFileName = "/com/connDB.properties"; //指定资源文件保存的位置
private static Properties prop = new Properties();
private static String dbClassName ="com.mysql.jdbc.Driver";
private static String dbUrl =
"jdbc:mysql://127.0.0.1:3306/db_mydb?user=root&password=111&useUnicode=true";
public ConnDB(){
try {
InputStream in=getClass().getResourceAsStream(propFileName);
prop.load(in); //通过输入流对象加载Properties文件
dbClassName = prop.getProperty("DB_CLASS_NAME"); //获取数据库驱动
dbUrl = prop.getProperty("DB_URL",
"jdbc:mysql://127.0.0.1:3306/db_librarySys?user=root&password=111&useUnicode=true");
}
catch (Exception e) {
e.printStackTrace(); //输出异常信息
}
}
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName(dbClassName).newInstance();
conn = DriverManager.getConnection(dbUrl);
}
catch (Exception ee) {
ee.printStackTrace();
}
if (conn == null) {
System.err.println(
"警告: DbConnectionManager.getConnection() 获得数据库链接失败.\r\n\r\n链接类型:" +
dbClassName + "\r\n链接位置:" + dbUrl);
}
return conn;
}
/*
* 功能:执行查询语句
*/
public ResultSet executeQuery(String sql) {
try {
conn = getConnection();
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
} catch (SQLException ex) {
System.err.println(ex.getMessage());
}
return rs;
}
/*
* 功能:执行更新操作
*/
public int executeUpdate(String sql) {
int result = 0;
try {
conn = getConnection(); //调用getConnection()方法构造Connection对象的一个实例conn
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
result = stmt.executeUpdate(sql); //执行更新操作
} catch (SQLException ex) {
result = 0;
}
return result;
}
/*
* 功能:关闭数据库的连接
*/
public void close() {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e) {
e.printStackTrace(System.err);
}
}
}
//一部分的操作
import org.apache.struts.action.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.actionForm.ManagerForm;
import com.dao.ManagerDAO;
import javax.servlet.http.HttpSession;
public class Manager extends Action {
private ManagerDAO managerDAO = null;
public Manager() {
this.managerDAO = new ManagerDAO();
}
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
String action = request.getParameter("action");
System.out.println("获取的查询字符串:" + action);
if (action == null || "".equals(action)) {
return mapping.findForward("error");
} else if ("login".equals(action)) {
return managerLogin(mapping, form, request, response);
} else if ("managerAdd".equals(action)) {
return managerAdd(mapping, form, request, response);
} else if ("managerQuery".equals(action)) {
return managerQuery(mapping, form, request, response);
} else if ("managerModifyQuery".equals(action)) {
return managerModifyQuery(mapping, form, request, response);
} else if ("managerModify".equals(action)) {
return managerModify(mapping, form, request, response);
} else if ("managerDel".equals(action)) {
return managerDel(mapping, form, request, response);
} else if ("querypwd".equals(action)) {
return pwdQuery(mapping, form, request, response);
} else if ("modifypwd".equals(action)) {
return modifypwd(mapping, form, request, response);
}
request.setAttribute("error", "操作失败!");
return mapping.findForward("error");
}
// 管理员身份验证
public ActionForward managerLogin(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
ManagerForm managerForm = (ManagerForm) form;
managerForm.setName(managerForm.getName());
managerForm.setPwd(managerForm.getPwd());
int ret = managerDAO.checkManager(managerForm);
System.out.print("验证结果ret的值:" + ret);
if (ret == 1) {
HttpSession session = request.getSession();
session.setAttribute("manager", managerForm.getName());
return mapping.findForward("managerLoginok");
} else {
request.setAttribute("error", "您输入的管理员名称或密码错误!");
return mapping.findForward("error");
}
}
// 查询管理员信息
private ActionForward managerQuery(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
String str = null;
request.setAttribute("managerQuery", managerDAO.query(str));
return mapping.findForward("managerQuery");
}
// 添加管理员信息
private ActionForward managerAdd(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
ManagerForm managerForm = (ManagerForm) form;
managerForm.setName(managerForm.getName()); // 获取设置管理员名称
managerForm.setPwd(managerForm.getPwd()); // 获取并设置密码
int ret = managerDAO.insert(managerForm); // 调用添加管理员信息
if (ret == 1) {
return mapping.findForward("managerAdd"); // 转到管理员信息添加成功页面
} else if (ret == 2) {
request.setAttribute("error", "该管理员信息已经添加!"); // 将错误信息保存到error参数中
return mapping.findForward("error"); // 转到错误提示页面
} else {
request.setAttribute("error", "添加管理员信息失败!"); // 将错误信息保存到error参数中
return mapping.findForward("error"); // 转到错误提示页面
}
}
// 查询修改管理员信息
private ActionForward managerModifyQuery(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) {
ManagerForm managerForm = (ManagerForm) form;
managerForm.setId(Integer.valueOf(request.getParameter("id")));//获取并设置管理ID号
System.out.print("查询到的id:" + request.getParameter("id"));
request.setAttribute("managerQueryif", managerDAO
.query_update(managerForm));
return mapping.findForward("managerQueryModify"); //转到权限设置成功页面
}
// 修改密码时查询
private ActionForward pwdQuery(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
ManagerForm managerForm = (ManagerForm) form;
HttpSession session = request.getSession();
String manager = (String) session.getAttribute("manager");
managerForm.setName(manager);
System.out.print("查询到的manager:" + manager);
request.setAttribute("pwdQueryif", managerDAO.query_pwd(managerForm));
return mapping.findForward("pwdQueryModify");
}
// 修改管理员信息
private ActionForward managerModify(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
ManagerForm managerForm = (ManagerForm) form;
managerForm.setId(managerForm.getId()); //获取并设置管理员ID号
managerForm.setName(managerForm.getName()); //获取并设置管理员名称
managerForm.setPwd(managerForm.getPwd()); //获取并设置管理员密码
managerForm.setSysset(managerForm.getSysset()); //获取并设置系统设置权限
managerForm.setReaderset(managerForm.getReaderset()); //获取并设置读者管理权限
managerForm.setBookset(managerForm.getBookset()); //获取并设置图书管理权限
managerForm.setBorrowback(managerForm.getBorrowback()); //获取并设置图书借还权限
managerForm.setSysquery(managerForm.getSysquery()); //获取并设置系统查询权限
int ret = managerDAO.update(managerForm); //调用设置管理员权限的方法
if (ret == 0) {
request.setAttribute("error", "设置管理员权限失败!"); //保存错误提示信息到error参数中
return mapping.findForward("error"); //转到错误提示页面
} else {
return mapping.findForward("managerModify"); //转到权限设置成功页面
}
}
// 删除管理员信息
private ActionForward managerDel(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
ManagerForm managerForm = (ManagerForm) form;
managerForm.setId(Integer.valueOf(request.getParameter("id"))); //获取并设置管理员ID号
int ret = managerDAO.delete(managerForm); //调用删除信息的方法delete()
if (ret == 0) {
request.setAttribute("error", "删除管理员信息失败!"); //保存错误提示信息到error参数中
return mapping.findForward("error"); //转到错误提示页面
} else {
return mapping.findForward("managerDel"); //转到删除管理员信息成功页面
}
}
// 修改管理员密码
private ActionForward modifypwd(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
ManagerForm managerForm = (ManagerForm) form;
managerForm.setName(managerForm.getName());
managerForm.setPwd(managerForm.getPwd());
int ret = managerDAO.updatePwd(managerForm);
if (ret == 0) {
request.setAttribute("error", "更改口令失败!");
return mapping.findForward("error");
} else {
return mapping.findForward("pwdModify");
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询