| 发表于:2007-10-11 20:24:06 楼主 |
connectdb package bbs; import java.lang.*; import java.sql.*; public class connectdb { string url="jdbc:odbc:bbs"; connection dbconn; string errmes; public connectdb() { errmes=""; dbconn=null; } public connection getconn() { try{ class.forname("sun.jdbc.odbc.jdbcodbcdriver"); dbconn=drivermanager.getconnection("jdbc:odbc:bbs","sa",""); }catch(exception e){ system.out.print(e); } return dbconn; } public string geterrmes() { return errmes; } } package bbs; import java.lang.*; import java.sql.*; public class EXECutedb extends connectdb { private connection dbconn; private statement stmt; private resultset rs; private string errmes; public EXECutedb() { dbconn=super.getconn(); stmt=null; rs=null; this.errmes=super.geterrmes(); } public boolean exesql(string strsql) { boolean issuc=false; try { stmt=dbconn.createstatement(); stmt.EXECuteupdate(strsql); stmt.close(); issuc=true; } catch(exception e) { this.errmes=this.errmes+" <br> "+e.tostring(); } return issuc; } public resultset exequery(string strsql) { try { stmt=dbconn.createstatement(); rs=stmt.EXECutequery(strsql); } catch(exception e) { this.errmes=this.errmes+" <br> "+e.tostring(); rs=null; } return rs; } public string geterrmes() { return errmes; } } package bbs; import java.sql.*; public class user extends EXECutedb{ private long userid; private string username; private string userpassword; private string email; private string sex; private string nickname; private string createtime; private string role; private string strsql; public user() { super(); this.userid=0; this.username=""; this.userpassword=""; this.sex=""; this.nickname=""; this.role="0"; this.email=""; java.util.date nowtime=new java.util.date(); this.createtime=nowtime.tostring(); this.strsql=""; } public void setusername(string name) { this.username=name; } public void setuserpassword(string password) { this.userpassword=password; } public void setsex(string sex) { this.sex=sex; } public void setnickname(string nickname) { this.nickname=nickname; } public void setemail(string email) { this.email=email; } public boolean add() { this.strsql="insert into users (username,userpassword,email,sex,nickname,createtime) "; this.strsql=" values('"+username+"','"+userpassword+"','"+email+"','"+sex+"','"+nickname+"')"; boolean isadd=super.exesql(this.strsql); return isadd; } public boolean modify_info() { this.strsql="update users set"; this.strsql=this.strsql+"email="+"'"+this.email+"',"; this.strsql=this.strsql+"sex="+"'"+this.sex+"',"; this.strsql=this.strsql+"nickname="+"'"+this.nickname+"',"; this.strsql=this.strsql+"create="+"'"+this.createtime+"',"; this.strsql=this.strsql+"where userid='"+this.userid+"'"; boolean isupdate=super.exesql(this.strsql); return isupdate; } public boolean modify_userpassword() { this.strsql="update users set"; this.strsql=this.strsql+"userpassword="+"'"+this.userpassword+"'"; this.strsql=this.strsql+"where userid='"+this.userid+"'"; boolean isupdatekey=super.exesql(this.strsql); return isupdatekey; } public boolean init() { this.strsql="select * from 'users' where userid="; this.strsql=this.strsql+"'"+this.userid+"'"; try { resultset rs=super.exequery(this.strsql); if(rs.next()) { this.userid=rs.getlong("userid"); this.username=rs.getstring("username"); this.userpassword=rs.getstring("userpassword"); this.sex=rs.getstring("sex"); this.nickname=rs.getstring("nickname"); this.email=rs.getstring("email"); this.createtime=rs.getstring("createtime"); return true; } else { return false; } }catch(exception e) { system.out.println(e.tostring()); return false; } } public boolean valid() { this.strsql="select userid,username,role from 'users'"; this.strsql=this.strsql+" where username='"+this.username+"'"; this.strsql=this.strsql+" and userpassword='"+this.userpassword+"'"; system.out.println(this.strsql); try { resultset rs=super.exequery(this.strsql); if(rs.next()) { this.userid=rs.getlong("userid"); this.username=rs.getstring("username"); this.role=rs.getstring("role"); return true; } else { return false; } }catch(exception ex) { return false; } } public boolean exist() { this.strsql="select * from 'users' "; this.strsql=this.strsql+" where username='"+this.username+"'"; try { resultset rs=super.exequery(this.strsql); if(rs.next()) { return true; } else { return false; } }catch(exception exe) { system.out.println(exe.tostring()); return false; } } } 在调用add方法时候出错... |
|
|
|
|