Java课程设计:基于tomcat+jsp+sqlserver的javaweb计算机配件报价系统
一、项目介绍
前台功能模块:系统首页、我的信息、留言板、用户登陆、公告以及日历模块
后台功能模块:修改个人信息、管理员管理、注册用户管理、类别信息管理、类别信息添加、散件信息管理、散件信息添加、公告信息管理、公告信息添加
二、项目技术栈
tomcat+jsp+sqlserver
三、核心代码
登录功能
public class loginService
{
public String login(String userName,String userPw,int userType)
{
System.out.println("userType"+userType);
try
{
Thread.sleep(700);
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
String result="no";
if(userType==0)//系统管理员登陆
{
String sql="select * from t_admin where userName=? and userPw=?";
Object[] params={userName,userPw};
DB mydb=new DB();
mydb.doPstm(sql, params);
try
{
ResultSet rs=mydb.getRs();
boolean mark=(rs==null||!rs.next()?false:true);
if(mark==false)
{
result="no";
}
else
{
result="yes";
TAdmin admin=new TAdmin();
admin.setUserId(rs.getInt("userId"));
admin.setUserName(rs.getString("userName"));
admin.setUserPw(rs.getString("userPw"));
WebContext ctx = WebContextFactory.get();
HttpSession session=ctx.getSession();
session.setAttribute("userType", 0);
session.setAttribute("admin", admin);
}
rs.close();
}
catch (SQLException e)
{
System.out.println("登录失败!");
e.printStackTrace();
}
finally
{
mydb.closed();
}
}
if(userType==1)
{
String sql="select * from t_user where del='no' and loginname=? and loginpw=?";
Object[] params={userName,userPw};
DB mydb=new DB();
mydb.doPstm(sql, params);
try
{
ResultSet rs=mydb.getRs();
boolean mark=(rs==null||!rs.next()?false:true);
if(mark==false)
{
result="no";
}
else
{
result="yes";
Tuser user=new Tuser();
user.setId(rs.getString("id"));
user.setLoginname(rs.getString("loginname"));
user.setLoginpw(rs.getString("loginpw"));
user.setLoginpw(rs.getString("loginpw"));
user.setName(rs.getString("name"));
user.setSex(rs.getString("sex"));
user.setAge(rs.getString("age"));
user.setAddress(rs.getString("address"));
user.setTel(rs.getString("tel"));
user.setEmail(rs.getString("email"));
user.setQq(rs.getString("qq"));
WebContext ctx = WebContextFactory.get();
HttpSession session=ctx.getSession();
session.setAttribute("userType", 1);
session.setAttribute("user", user);
}
rs.close();
}
catch (SQLException e)
{
System.out.println("登录失败!");
e.printStackTrace();
}
finally
{
mydb.closed();
}
}
if(userType==2)
{
}
return result;
}
public String adminPwEdit(String userPwNew)
{
System.out.println("DDDD");
try
{
Thread.sleep(700);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
WebContext ctx = WebContextFactory.get();
HttpSession session=ctx.getSession();
TAdmin admin=(TAdmin)session.getAttribute("admin");
String sql="update t_admin set userPw=? where userId=?";
Object[] params={userPwNew,admin.getUserId()};
DB mydb=new DB();
mydb.doPstm(sql, params);
return "yes";
}
public List catelogAll()
{
try
{
Thread.sleep(700);
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
List catelogList=new ArrayList();
String sql="select * from t_catelog where del='no'";
Object[] params={};
DB mydb=new DB();
try
{
mydb.doPstm(sql, params);
ResultSet rs=mydb.getRs();
while(rs.next())
{
Tcatelog catelog=new Tcatelog();
catelog.setId(rs.getInt("id"));
catelog.setName(rs.getString("name"));
catelog.setJieshao(rs.getString("jieshao"));
catelogList.add(catelog);
}
rs.close();
}
catch(Exception e)
{
e.printStackTrace();
}
mydb.closed();
System.out.println(catelogList.size()+"^^");
return catelogList;
}
}
登录servlet层
public class admin_servlet extends HttpServlet
{
public void service(HttpServletRequest req,HttpServletResponse res)throws ServletException, IOException
{
String type=req.getParameter("type");
if(type.endsWith("adminMana"))
{
adminMana(req, res);
}
if(type.endsWith("adminAdd"))
{
adminAdd(req, res);
}
if(type.endsWith("adminDel"))
{
adminDel(req, res);
}
}
public void adminMana(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException
{
List adminList=new ArrayList();
String sql="select * from t_admin";
Object[] params={};
DB mydb=new DB();
try
{
mydb.doPstm(sql, params);
ResultSet rs=mydb.getRs();
while(rs.next())
{
TAdmin admin=new TAdmin();
admin.setUserId(rs.getInt("userId"));
admin.setUserName(rs.getString("userName"));
admin.setUserPw(rs.getString("userPw"));
adminList.add(admin);
}
rs.close();
}
catch(Exception e)
{
e.printStackTrace();
}
mydb.closed();
req.setAttribute("adminList", adminList);
req.getRequestDispatcher("admin/admin/adminMana.jsp").forward(req, res);
}
public void adminAdd(HttpServletRequest req,HttpServletResponse res)
{
String userName=req.getParameter("userName");
String userPw=req.getParameter("userPw");
String sql="insert into t_admin values(?,?)";
Object[] params={userName,userPw};
DB mydb=new DB();
mydb.doPstm(sql, params);
mydb.closed();
req.setAttribute("message", "操作成功");
req.setAttribute("path", "admin?type=adminMana");
String targetURL = "/common/success.jsp";
dispatch(targetURL, req, res);
}
public void adminDel(HttpServletRequest req,HttpServletResponse res)
{
System.out.println(req.getParameter("userId")+"**");
String sql="delete from t_admin where userId="+Integer.parseInt(req.getParameter("userId"));
Object[] params={};
DB mydb=new DB();
mydb.doPstm(sql, params);
mydb.closed();
req.setAttribute("message", "操作成功");
req.setAttribute("path", "admin?type=adminMana");
String targetURL = "/common/success.jsp";
dispatch(targetURL, req, res);
}
public void dispatch(String targetURI,HttpServletRequest request,HttpServletResponse response)
{
RequestDispatcher dispatch = getServletContext().getRequestDispatcher(targetURI);
try
{
dispatch.forward(request, response);
return;
}
catch (ServletException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public void init(ServletConfig config) throws ServletException
{
super.init(config);
}
public void destroy()
{
}
}
数据库功能
public class DB {
private Connection con;
private PreparedStatement pstm;
private String user = "sa";
private String password = "sa";
private String className = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private String url = "jdbc:sqlserver://localhost:1433;databaseName=db_jsjsj";
public DB()
{
try
{
Class.forName(className);
} catch (ClassNotFoundException e)
{
System.out.println("加载数据库驱动失败!");
e.printStackTrace();
}
}
/** 创建数据库连接 */
public Connection getCon()
{
try
{
con = DriverManager.getConnection(url, user, password);
} catch (SQLException e)
{
System.out.println("创建数据库连接失败!");
con = null;
e.printStackTrace();
}
return con;
}
public void doPstm(String sql, Object[] params)
{
if (sql != null && !sql.equals(""))
{
if (params == null)
params = new Object[0];
getCon();
if (con != null)
{
try
{
System.out.println(sql);
pstm = con.prepareStatement(sql,
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
for (int i = 0; i < params.length; i++)
{
pstm.setObject(i + 1, params[i]);
}
pstm.execute();
} catch (SQLException e)
{
System.out.println("doPstm()方法出错!");
e.printStackTrace();
}
}
}
}
public ResultSet getRs() throws SQLException
{
return pstm.getResultSet();
}
public int getCount() throws SQLException
{
return pstm.getUpdateCount();
}
public void closed()
{
try
{
if (pstm != null)
pstm.close();
} catch (SQLException e)
{
System.out.println("关闭pstm对象失败!");
e.printStackTrace();
}
try
{
if (con != null)
{
con.close();
}
} catch (SQLException e)
{
System.out.println("关闭con对象失败!");
e.printStackTrace();
}
}
}
四、项目展示
主页
登录
系统管理员端
五、源码
因为页面与源码太多了,所以页面与源码只展示了一部分,完整源码已经打包了,点击下面蓝色链接获取!
原文地址:https://blog.csdn.net/2401_84040513/article/details/144693420
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!