注册
登录
论坛
搜索
社区银行
帮助
导航
私人消息 (0)
公共消息 (0)
系统消息 (0)
好友消息 (0)
帖子消息 (0)
黑色海岸线论坛
»
网络安全
» [原创]学习javabean的小程序,欢迎批评
返回列表
发帖
wxf
该用户已被删除
楼主
跳转到
»
倒序看帖
打印
字体大小:
t
T
wxf
发表于 2006-8-11 13:23
|
显示全部帖子
[原创]学习javabean的小程序,欢迎批评
小弟近日学习java ,然后自己试着写了个小javabean,纯当学习,其中定有许多不正确之处,请大家知道的指出啊,谢谢拉
DbOprt.java
这是数据库操作的bean,用的是sqlserver2000
package dbop;
import java.io.*;
import java.util.*;
import java.sql.*;
public class DbOprt{
/////////////////////////////////////////////////////////////////////////////
//To use a different database please modify here first...
String dbDriverPath = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String dbUrl = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=";
String dbName = "sdppcpeople";
/////////////////////////////////////////////////////////////////////////////
//Basic friendly properties...
String user = null;
String password = null;
Connection conForQuery = null;
Statement stmtForQuery = null;
ResultSet rsForQuery = null;
/////////////////////////////////////////////////////////////////////////////
//Constrator...
public DbOprt()
{
try {
Class.forName(dbDriverPath);
}catch(Exception e){
System.err.println(e.getMessage());
}
}
/////////////////////////////////////////////////////////////////////////////
//Base public set* method...
public void setUser(String user)
{
this.user = user;
}
public void setPassword(String password)
{
this.password = password;
}
/////////////////////////////////////////////////////////////////////////////
//Base public get* method...
public String getUser()
{
return this.user;
}
public String getPassword()
{
return this.password;
}
public Connection getConForQuery()
{
return this.conForQuery;
}
public Statement getStmtForQuery()
{
return this.stmtForQuery;
}
public ResultSet getRsForQuery()
{
return this.rsForQuery;
}
/////////////////////////////////////////////////////////////////////////////
//Extra method depends the base public set* & get* method...
public Boolean closeResultSet()
{
Boolean closeFlag = true;
try{
if(getRsForQuery() != null)
this.rsForQuery.close();
}catch(Exception e)
{
System.err.println(e.getMessage());
closeFlag = false;
}
return closeFlag;
}
public Boolean closeStatement()
{
Boolean closeFlag = true;
try{
if(getStmtForQuery() != null)
this.stmtForQuery.close();
}catch(Exception e)
{
System.err.println(e.getMessage());
closeFlag = false;
}
return closeFlag;
}
public Boolean closeConnection()
{
Boolean closeFlag = true;
try{
if(getConForQuery() != null)
this.conForQuery.close();
}catch(Exception e)
{
System.err.println(e.getMessage());
closeFlag = false;
}
return closeFlag;
}
public Boolean getLogin()
{
Boolean loginFlag = true;
Connection con = null;
try{
con = DriverManager.getConnection(dbUrl, getUser(), getPassword());
}catch(Exception e)
{
System.err.println(e.getMessage());
loginFlag = false;
}finally{
try {
if(con != null)
con.close();
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
return loginFlag;
}
/////////////////////////////////////////////////////////////////////////////
//Warning:
//After call thismethod you must call the close*() method release them...
public ResultSet excuteQuery(String tablename)
{
Boolean queryFlag = true;
String sqlToQuery = "SELECT * FROM " + tablename;
try{
conForQuery = DriverManager.getConnection(dbUrl, getUser(), getPassword());
stmtForQuery = conForQuery.createStatement();
rsForQuery = stmtForQuery.executeQuery(sqlToQuery);
}catch(Exception e)
{
System.err.println(e.getMessage());
queryFlag = false;
}finally
{
if (!queryFlag)
{
try{
if (rsForQuery != null)
rsForQuery.close();
if (stmtForQuery != null)
stmtForQuery.close();
if (conForQuery != null)
conForQuery.close();
}catch(Exception e)
{
System.err.println(e.getMessage());
}
}
}
return rsForQuery;
}
public Boolean excuteUpdate(String sqlToUpdate)
{
Boolean updateFlag = true;
Connection con = null;
Statement stmt = null;
try{
con = java.sql.DriverManager.getConnection(dbUrl, getUser(), getPassword());
stmt = con.createStatement();
stmt.executeUpdate(sqlToUpdate);
}catch(Exception e)
{
System.err.println(e.getMessage());
updateFlag = false;
}finally
{
try
{
if(stmt != null)
stmt.close();
if (con != null)
con.close();
}catch(Exception e)
{
System.err.println(e.getMessage());
}
}
return updateFlag;
}
public int getDbColumn(String tablename)
{
int columnNum = 0;
String sql = "SELECT * FROM " + tablename;
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
ResultSetMetaData rsmd = null;
try
{
Class.forName(this.dbDriverPath);
con = DriverManager.getConnection(dbUrl, getUser(), getPassword());
stmt = con.createStatement();
rs = stmt.executeQuery(sql);
rsmd = rs.getMetaData();
columnNum = rsmd.getColumnCount();
}catch(Exception e)
{
System.err.println(e.getMessage());
columnNum = 0;
}finally
{
try{
if(rs != null)
rs.close();
if(stmt != null)
stmt.close();
if(con != null)
con.close();
}catch(Exception e)
{
System.err.println(e.getMessage());
}
}
return columnNum;
}
/////////////////////////////////////////////////////////////////////////////
//Now,this is the end...
}
复制代码
收藏
分享
wxf
该用户已被删除
沙发
wxf
发表于 2006-8-11 13:25
|
显示全部帖子
[原创]学习javabean的小程序,欢迎批评
PeopleInfo.java
这是一个用来生成查询语句的bean.
package people;
public class PeopleInfo{
String peoplename = null;
String peoplesex = null;
String peoplebirthday = null;
String peoplejiguan = null;
String peopledepartment = null;
String tablename = null;
String pubkey = null;
///////////////////////////////////////////////////////////////////////////
//Basic public set* method...
public void setPeoplename(String name)
{
this.peoplename = name;
}
public void setPeoplesex(String sex)
{
this.peoplesex = sex;
}
public void setPeoplebirthday(String birthday)
{
this.peoplebirthday = birthday;
}
public void setPeoplejiguan(String jiguan)
{
this.peoplejiguan = jiguan;
}
public void setPeopledepartment(String department)
{
this.peopledepartment = department;
}
public void setTablename(String tablename)
{
this.tablename = tablename;
}
public void setPubkey(String pubkey)
{
this.pubkey = pubkey;
}
///////////////////////////////////////////////////////////////////////////
//Basic public get* method...
public String getPeoplename()
{
return this.peoplename;
}
public String getPeoplesex()
{
return this.peoplesex;
}
public String getPeoplebirthday()
{
return this.peoplebirthday;
}
public String getPeoplejiguan()
{
return this.peoplejiguan;
}
public String getPeopledepartment()
{
return this.peopledepartment;
}
public String getTablename()
{
return this.tablename;
}
public String getPubkey()
{
return this.pubkey;
}
public String getAddSql()
{
String sql = null;
sql = "INSERT INTO " + getTablename() + " VALUES (";
sql += ("';" + getPeoplename() + "';, ';" + getPeoplesex() + "';, ';" + getPeoplebirthday());
sql += ("';, ';" + getPeoplejiguan() + "';, ';" + getPeopledepartment() + "';)");
return sql;
}
public String getDelSql()
{
String sql = null;
sql = "DELETE " + getTablename() + " WHERE " + getPubkey() + "=';" + getPeoplename() + "';";
return sql;
}
public String getModSql()
{
String sql = null;
sql = "UPDATE " + getTablename() + " SET ";
sql += ("peoplesex=';" + getPeoplesex() + "';, " + "peoplebirthday=';" +
getPeoplebirthday() + "';, " + "peoplejiguan=';" + getPeoplejiguan() +
"';, " + "peopledepartment=';" + getPeopledepartment() +"'; WHERE " +
getPubkey() + "=';" + getPeoplename() + "';");
return sql;
}
///////////////////////////////////////////////////////////////////////////
//Now,it';s the end..
}
复制代码
TOP
wxf
该用户已被删除
板凳
wxf
发表于 2006-8-11 13:27
|
显示全部帖子
[原创]学习javabean的小程序,欢迎批评
[程序流程] user(client) | ________>在session中生成javabean,并保存用户名和密码 | | login.htm --post--> login.jsp(验证用户) ^ | |-------------------| 关闭session| error关闭session | 离开| |right LEA BUTTON| | |<------------>operate.jsp(操作及查询页面) | ^ | | | | 返回 | 返回 | |返回 | | | | /|\ | | / | \ | | / | \ | | / | \ | | / | \ | add.jsp del.jsp mod.jsp _________________________|____________|___________|___________________________________ USE BEAN PeopleInfo & DbOp ______________________________________________________________________________________ PeopleInfo.class DbOprt.class [数据库] databasename: sdppcpeople (在javabean DbOprt.class中定义) tablename: peopleinfo (在jsp中定义,并由jsp传递至javabean) properties: 名称 数据类型 大小 附加 peoplename varchar 20 PK peoplesex varchar 30 peoplebirthday carchar 30 peoplejiguan varchar 20 peopledepartment varchar 10 如上,jsp代码就不发了啊,第一次写bean,望各位给点意见。
TOP
返回列表
回复
发帖
使用交流
网络安全
网络技术
娱乐休闲
灌水乐园
文学天地
美图欣赏
网站办公
站务处理
[收藏此主题]
[关注此主题的新回复]
[通过 QQ、MSN 分享给朋友]