自己动手做试卷管理系统-用户管理部分
开发环境:java5,myeclipse,tomcat。为了从基础理解javaweb,采用jsp+servlet,没用任何框架。
源文件有四个包:
com.yijia_ctgu.bean
com.yijia_ctgu.DB
com.yijia_ctgu.exception
com.yijia_ctgu.servlet
bean包含一个类:user.java。它从形式上是bean,但到底是不是严格意义的bean,我自己也不确定。我只是怎样方便开发就怎样写。部分源文件如下:
package com.yijia_ctgu.bean;
import java.sql.SQLException;
import java.util.List;
import com.yijia_ctgu.DB.DBExcute;
import com.yijia_ctgu.exception.NotQueryException;
public class User {
String username;
String password;
String ip;
String mail;
int authorize;
DBExcute dbExcute=new DBExcute();
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword()throws SQLException,NotQueryException {
String sql="select password from user where username='"+username+"' ";
try{
String password=dbExcute.queryString(sql);
return password;
}catch(NotQueryException ex){
throw ex;
}catch(SQLException ex){
throw ex;
}
}
public void setPassword(String password)throws SQLException {
String sql="update user set password='"+password+"' where username='"+username+"'";
try {
dbExcute.update(sql);
} catch (SQLException ex) {
throw ex;
}
}
public int getAuthorize()throws SQLException,NotQueryException {
String sql="select authorize from user where username='"+username+"' ";
try{
String str=dbExcute.queryString(sql);
int authorize=Integer.parseInt(str);
return authorize;
}catch(NotQueryException ex){
throw ex;
}catch(SQLException ex){
throw ex;
}
}
public void setAuthorize(int authorize)throws SQLException {
String sql="update user set authorize='"+authorize+"' where username='"+username+"'";
try {
dbExcute.update(sql);
} catch (SQLException ex) {
throw ex;
}
}
public String getIp()throws SQLException,NotQueryException {
String sql="select ip from user where username='"+username+"' ";
try{
String str=dbExcute.queryString(sql);
return str;
}catch(NotQueryException ex){
throw ex;
}catch(SQLException ex){
throw ex;
}
}
public void setIp(String ip)throws SQLException {
String sql="update user set ip='"+ip+"' where username='"+username+"'";
try {
dbExcute.update(sql);
} catch (SQLException ex) {
throw ex;
}
}
public String getMail()throws SQLException,NotQueryException {
String sql="select ip from user where username='"+username+"' ";
try{
String str=dbExcute.queryString(sql);
return str;
}catch(NotQueryException ex){
throw ex;
}catch(SQLException ex){
throw ex;
}
}
public void setMail(String mail)throws SQLException {
String sql="update user set mail='"+mail+"' where username='"+username+"'";
try {
dbExcute.update(sql);
} catch (SQLException ex) {
throw ex;
}
}
public DBExcute getDbExcute() {
return dbExcute;
}
public void setDbExcute(DBExcute dbExcute) {
this.dbExcute = dbExcute;
}
}
看到这里,你觉得它算不算做bean呢?
DB包里的四个文件,DBExcute是原创的,其它的(数据库连接部分)都是借用的。不知道我的DBExcute的重用性怎么样?代码如下:
package com.yijia_ctgu.DB;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Vector;
import org.apache.commons.beanutils.RowSetDynaClass;
import com.yijia_ctgu.DB.DBConnect;
import com.yijia_ctgu.exception.NotQueryException;
public class DBExcute{
Connection conn;
String condition;
Statement stm;
ResultSet rs;
public DBExcute(){}
public void initize(){
try{
if(conn==null){
conn=DBConnect.getConnection();
}
if(stm==null){
stm=conn.createStatement();
}
}
catch(SQLException ex){
System.out.println("初始化错误");
}
}
public void close(){
try{
if(rs!=null) rs.close();
if(stm!=null)stm.close();
if(conn!=null)conn.close();
}catch(SQLException ex){
System.out.println("关闭出错");
}
}
public String queryString(int i,String sql)throws SQLException,NotQueryException{
initize();
try{
String str=null;
rs=stm.executeQuery(sql);
//System.out.println("1");
if(rs.next()){
str=rs.getString(i);
return str;
}
else {
NotQueryException cex=new NotQueryException();
throw cex;
}
}catch(SQLException ex){
System.out.println("异常抛出 :queryString of Service");
throw ex;
}catch(NotQueryException cex){
System.out.println("notqurey异常 :queryString of Service");
throw cex;
}
}
public String queryString(String sql) throws SQLException,NotQueryException{
return queryString(1,sql);
}
public List queryList(String sql)throws SQLException{
initize();
try{
rs=stm.executeQuery(sql);
RowSetDynaClass rsdc = new RowSetDynaClass(rs);
List list = rsdc.getRows();
return list;
}catch(SQLException ex){
System.out.println("异常抛出 :queryList of Service");
throw ex;
}
}
public boolean update(String sql)throws SQLException{
initize();
try{
stm.executeUpdate(sql);
return true;
}catch(SQLException ex){
System.out.println("异常抛出 :update of Service");
throw ex;
}
}
public static void main(String[] args){
DBExcute dbExcute=new DBExcute();
try{
String str=dbExcute.queryString(1,"select authorize from user where username='root' ");
System.out.println(str);
dbExcute.queryList("select authorize from user where username='root' ");
}catch(Exception ex){
System.out.println("test failer");
}
}
}
exception包中是自定义的异常,起到了一点小作用。
servlet中就不多说了,全是自己写的,没什么特别的地方。看它的时候注意session变量userList.
jsp页面:为了全面一点,修改和添加 是用链接实现的,搜索和删除 是用表单完成的。还真看到问题了。用超链接 链接servlet来实现修改的时候总是看不出效果,原来链接过去的都是到doGet()方法,我想表单传过去的默认的也是doGet()吧,这一点我没查资料,不知道是不是这样的?jsp页面中同样注意userList。
现在只实现了用户管理部分,如果有时间的话会继续其它部分,那位同志有兴趣的可以来完善它,本文附件附有源代码。最后声明:仅供学习,仅此而已。
欢迎多提宝贵意见!
附件说明
开发工具:java5,myeclipse,tomcat。
使用方法:把papermanage文件夹copy到..\Apache Software Foundation\Tomcat 6.0\webapps文件夹下
登陆时用 用户名:root(超级管理员),密码:root 登陆才能看到 用户管理的链接
附件: http://www.blogjava.net/Files/yijia/papermanage.rar
没找到上传附件的地方,就先上传文件上去,找复制链接到这里了。