标题:
[原创]学习javabean的小程序,欢迎批评
[打印本页]
作者:
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
时间:
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..
}
复制代码
作者:
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,望各位给点意见。
作者:
风灵风之子
时间:
2006-8-11 18:46
标题:
[原创]学习javabean的小程序,欢迎批评
如果LZ把数据库的信息写在配置文件里
再使用O/RM的话 程序可能会更简洁和清晰些
不过LZ第一次写javabean,程序写得已经不错了
欢迎光临 黑色海岸线论坛 (http://bbs.thysea.com/)
Powered by Discuz! 7.2