開發(fā)環(huán)境:java5,myeclipse,tomcat。為了從基礎(chǔ)理解javaweb,采用jsp+servlet,沒用任何框架。
源文件有四個(gè)包:
com.yijia_ctgu.bean
com.yijia_ctgu.DB
com.yijia_ctgu.exception
com.yijia_ctgu.servlet
bean包含一個(gè)類:user.java。它從形式上是bean,但到底是不是嚴(yán)格意義的bean,我自己也不確定。我只是怎樣方便開發(fā)就怎樣寫。部分源文件如下:
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包里的四個(gè)文件,DBExcute是原創(chuàng)的,其它的(數(shù)據(jù)庫連接部分)都是借用的。不知道我的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("初始化錯(cuò)誤");
}
}
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("關(guān)閉出錯(cuò)");
}
}
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包中是自定義的異常,起到了一點(diǎn)小作用。
servlet中就不多說了,全是自己寫的,沒什么特別的地方。看它的時(shí)候注意session變量userList.
jsp頁面:為了全面一點(diǎn),修改和添加 是用鏈接實(shí)現(xiàn)的,搜索和刪除 是用表單完成的。還真看到問題了。用超鏈接 鏈接servlet來實(shí)現(xiàn)修改的時(shí)候總是看不出效果,原來鏈接過去的都是到doGet()方法,我想表單傳過去的默認(rèn)的也是doGet()吧,這一點(diǎn)我沒查資料,不知道是不是這樣的?jsp頁面中同樣注意userList。
現(xiàn)在只實(shí)現(xiàn)了用戶管理部分,如果有時(shí)間的話會繼續(xù)其它部分,那位同志有興趣的可以來完善它,本文附件附有源代碼。最后聲明:僅供學(xué)習(xí),僅此而已。
歡迎多提寶貴意見!
附件說明
開發(fā)工具:java5,myeclipse,tomcat。
使用方法:把papermanage文件夾copy到..\Apache Software Foundation\Tomcat 6.0\webapps文件夾下
登陸時(shí)用 用戶名:root(超級管理員),密碼:root 登陸才能看到 用戶管理的鏈接
附件: http://m.tkk7.com/Files/yijia/papermanage.rar
沒找到上傳附件的地方,就先上傳文件上去,找復(fù)制鏈接到這里了。
posted on 2008-06-13 09:42
開機(jī) 閱讀(1043)
評論(7) 編輯 收藏 所屬分類:
jsp+javabean 、
javaweb