这个bean写的对吗?

javawriter 2002-12-26 01:24:45
请问大家所有的功能都写在一个bean里对吗?
...
import java.sql.*;
public class LeaderAction {
private int id;
private String leader_name;
private String leader_level;
private String leader_position;
private String leader_intro;
private String department;
private String working;
private String tel;
private String photo;
private static String dbDriver;//数据库驱动
private static String dbURL;//数据库路径
private Connection conn;//建立数据库连接对象
public static ResultSet rs;//结果集对象

public LeaderAction() {
id = 0;
leader_name = "";
leader_level = "";
leader_position = "";
leader_intro = "";
department = "";
working = "";
tel = "";
photo = "";
dbDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
dbURL = "jdbc:odbc:gov";
conn = null;
rs = null;
try{
Class.forName(dbDriver);
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}

public void setLeader(String leader_name1,String leader_level1,String leader_position1,String leader_intro1,String department1,String working1,String tel1,String photo1){
this.leader_name = leader_name1;
this.leader_level = leader_level1;
this.leader_position = leader_position1;
this.leader_intro = leader_intro1;
this.department = department1;
this.working = working1;
this.tel = tel1;
this.photo = photo1;
}

/*
*添加
*/

public String getAddLeader(String leader_name,String leader_level,String leader_position,String leader_intro,String department,String working,String tel,String photo){
String sql = null;
int max_id=-1;
//得到最大值
try{
conn = DriverManager.getConnection(dbURL,"jsp_user","jsp");
Statement stmt = conn.createStatement();
sql = "select max(id) from leader ";
rs = stmt.executeQuery(sql);
rs.next();
max_id = rs.getInt(1);
stmt.close();
conn.close();
}
catch(SQLException e){
e.printStackTrace();
}



/**将参数传入并返回结果
*
*/

id = max_id+1;
int success = 0;
String message = "";

try{
conn = DriverManager.getConnection(dbURL,"jsp_user","jsp");
Statement stmt = conn.createStatement();
sql = "insert into leader (id,leader_name,leader_level,leader_position,leader_intro,department,working,tel,photo) values("+id+",'"+leader_name+"','"+leader_level+"','"+leader_position+"','"+leader_intro+"','"+department+"','"+working+"','"+tel+"','"+photo+"')";
success = stmt.executeUpdate(sql);
stmt.close();
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
if(success == 1){
message = "添加纪录成功!";
}
else{
message = "添加纪录失败!";
}
return message;
}


/*
*显示数据库中全部记录
*/
public void getAllLeader(){
String sql = null;
try{
conn = DriverManager.getConnection(dbURL,"jsp_user","jsp");
Statement stmt = conn.createStatement();
sql = "SELECT * FROM leader";
rs = stmt.executeQuery(sql);

}catch(SQLException e){
e.printStackTrace();
}
}


}
...全文
33 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaofenguser 2002-12-27
  • 打赏
  • 举报
回复
没什么特别的意义和作用,只是我喜欢把数据封装起来,传送数据的时候可以作为一个整体,比较方便些,如果都是单个的话,在几个页面或类中间一传就容易传错或传丢.
blackfiles 2002-12-27
  • 打赏
  • 举报
回复
To xiaofenguser(风雨):
请教你的这个数据模型public class KeyModel
有什么意义和作用么??谢谢
xiaofenguser 2002-12-27
  • 打赏
  • 举报
回复
sql语句和操作数据库的放在一起.
javawriter 2002-12-27
  • 打赏
  • 举报
回复
sql语句写在外面吗?
xiaofenguser 2002-12-26
  • 打赏
  • 举报
回复
这是我的:
//连接数据库
package com.data;
import java.sql.*;
import java.util.*;
public class DBConnect
{
private Connection con=null;
private ResultSet rs=null;
private Statement stmt=null;
//private String strDRV="sun.jdbc.odbc.JdbcOdbcDriver";
private String strDRV="sun.jdbc.odbc.JdbcOdbcDriver";
private String strURL="jdbc:odbc:mysite";

public DBConnect(){
try{
Class.forName(strDRV);
con=DriverManager.getConnection(strURL,"",""); }catch(Exception e){
System.out.print("Error is:"+e);
}
}
public Connection getConn()
{
return con;
}
public ResultSet executeQuery(String sql)
{
try{
stmt=con.createStatement();
rs=stmt.executeQuery(sql);
}catch(Exception e){
System.out.print("Error in query:"+e);
}
return rs;
}
public int executeUpdate(String sql)
{
int flag=0;
try{
stmt=con.createStatement();
flag=stmt.executeUpdate(sql);
}catch(Exception e){
}
return flag;
}
public void destroy()
{
try{
stmt.close();
}catch(Exception e){
}
try{
con.close();
}catch(Exception e){
}
}
}
//操作数据库
import java.sql.*;
public class KeyDB
{
private Connection con=null;
private static final String sql_insert="insert into test(id,content) values(?,?)";
private static final String sql_query="select * from test";
private static final String sql_queryid="select * from test where id=?";
private String id=null;
private String content=null;
private KeyModel km=null;
private String go=null;
public String toString()
{
return go;
}
public int executeInsert(Connection con,KeyModel km)
{
this.con=con;
this.km=km;
setKm();
return executeInsert();
}
public int executeInsert()
{
int i=0;
try{
byte bs[]=new xiao().encrypt(content);
PreparedStatement pstm=con.prepareStatement(sql_insert);
pstm.setString(1,id);
pstm.setBytes(2,bs);
i=pstm.executeUpdate();
go=new xiao().uncrypt(bs);
}catch(Exception e)
{
i=-1;
}
return i;
}
public KeyModel executeQueryId(Connection con,String id)
{
this.con=con;
this.id=id;
return executeQueryId();
}
public KeyModel executeQueryId()
{
KeyModel kml=new KeyModel();
try{
PreparedStatement pstm=con.prepareStatement(sql_queryid);
pstm.setString(1,id);
ResultSet rs=pstm.executeQuery();
if(rs.next())
{
kml.setId(id);
byte bl[]=rs.getBytes("content");
String str=new xiao().uncrypt(bl);
kml.setContent(str);
}
}catch(Exception e)
{
System.out.print("err in KeyModel executeQueryId()!");
}
return kml;
}
public KeyVector executeQuery(Connection con)
{
this.con=con;
return executeQuery();
}
public KeyVector executeQuery()
{
KeyVector kv=new KeyVector();
try{
PreparedStatement pstm=con.prepareStatement(sql_query);
ResultSet rs=pstm.executeQuery();
while(rs.next())
{
byte bl[]=rs.getBytes("content");
id=rs.getString("id");
String str=new xiao().uncrypt(bl);
KeyModel km=new KeyModel(id,str);
kv.add(km);
}
}catch(SQLException e)
{
System.out.println("err in KeyDB executeQuery()!");
}
return kv;
}
private void setKm()
{
this.id=km.getId();
this.content=km.getContent();
}
}
//数据模型
public class KeyModel
{
private String id=null;
private String content=null;
public KeyModel(){};
public KeyModel(String id,String content)
{
this.id=id;
this.content=content;
}
public void setId(String id)
{
this.id=id;
}
public void setContent(String content)
{
this.content=content;
}
public String getId()
{
return id;
}
public String getContent()
{
return content;
}
}
//多个数据的
import java.util.Vector;
public class KeyVector
{
Vector vt=null;
public KeyVector()
{
vt=new Vector();
}
public void add(KeyModel km)
{
vt.add(km);
}
public KeyModel get(int index)
{
return (KeyModel)vt.get(index);
}
public int size()
{
return vt.size();
}
}
仅供参考!
javawriter 2002-12-26
  • 打赏
  • 举报
回复
有没有一个现成例子的
wenqiangyang 2002-12-26
  • 打赏
  • 举报
回复
不是很好,首先数据库最好作为一个单独的bean,在以后应用中可以设为session变量,其他bean可以接受它传入的参数,这样数据库的压力也不是特别大。变量全是private太绝对了,如果要好的习惯的话,可以加一个error的显示的变量public
tzzxj96 2002-12-26
  • 打赏
  • 举报
回复
这样当然不好,你把连接搞进来。
如果换个环境,你所有的bean都得改,累得不行。
tzzxj96 2002-12-26
  • 打赏
  • 举报
回复
这样当然不好,你把连接搞进来。
如果换个环境,你所有的bean都得改,累得不行。
Martin2002 2002-12-26
  • 打赏
  • 举报
回复
不对。
我统一whiteshen的说法
yujinping 2002-12-26
  • 打赏
  • 举报
回复
应该分开!
1)数据库管理工具包
2)商务逻辑处理包
3)公共工具包
这样一来后期维护和扩展就好多了
whiteshen 2002-12-26
  • 打赏
  • 举报
回复
个人认为这样做不好
这样对今后的扩展和维护带来很多不便
希偌 2002-12-26
  • 打赏
  • 举报
回复
太长,懒得看,说说什么问题吧
xiaofenguser 2002-12-26
  • 打赏
  • 举报
回复
有点不好,最好把连接数据库和操作数据库的分开来写.

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧