想请人帮我写访问数据库的javabean。

紫罗兰Cherry 2004-01-27 11:35:22
熟悉jsp+javabean的高手可以和我联系。
我现在在做个项目,时间有点紧张,
在调试jsp,javabean中遇到很多问题,
希望各位帮忙!
...全文
36 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
紫罗兰Cherry 2004-04-06
  • 打赏
  • 举报
回复
谢谢各位,给分。
zlhlj2000 2004-01-27
  • 打赏
  • 举报
回复
import java.sql.*;
public class DBCon {
Connection con=null;
Statement smt=null;
ResultSet rs=null;
public DBCon(){
try{
Class.forName(database engine);
}catch(Exception e){ }
}
public ResultSet executeQuery(String sql){
rs=null;
try{
con=DriverManager.getConnection("connection","user","password");
smt=con.createStatement();
rs=smt.executeQuery(sql);
}catch(SQLException ex){
System.out.println("aq.executeQuery:"+ex.getMessage());
}
return rs;
}
}
紫罗兰Cherry 2004-01-27
  • 打赏
  • 举报
回复
我的意思:
我把数据库表的结构给你们,
帮我写所有的javabean,当然要保证全部正常使用。
表共有20多个,就是要求把数据库的操作放在bean中。当然如果可以写jsp就更好了。
整体就是个网站。

可以谈谈价钱。:)
因为我刚开始使用jsp,很多问题觉得怪,时间有点紧张,多谢各位了!
fantasyCoder 2004-01-27
  • 打赏
  • 举报
回复
package connectionpool;

/**
* @author Administrator
*
* 更改所生成类型注释的模板为
* 窗口 > 首选项 > Java > 代码生成 > 代码和注释
*/

import java.sql.*;

public class SqlStatement {

private Connection conn = null;
private Statement stmt = null;
private ResultSet rs = null;

public SqlStatement() {
}

public void setConnection(Connection conn) {
this.conn = conn;
}
/**执行插入*/
public void executeInsert(String sql) {
try {
stmt = conn.createStatement();
stmt.executeUpdate(sql);
} catch (SQLException e) {
System.out.println("执行插入有错误" + e.getMessage());
}
}
/**执行更新*/
public void executeUpdate(String sql) {
try {
stmt = conn.createStatement();
stmt.executeUpdate(sql);
} catch (SQLException e) {
System.out.println("执行更新有错误" + e.getMessage());
}
}
/**执行删除*/
public void executeDelete(String sql) {
try {
stmt = conn.createStatement();
stmt.executeUpdate(sql);
} catch (SQLException e) {
System.out.println("执行删除有错误" + e.getMessage());
}
}
/**执行查询*/
public ResultSet executeQuery(String sql) {
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
} catch (SQLException e) {
System.out.println("执行查询有错误" + e.getMessage());
}
return rs;
}

}
fantasyCoder 2004-01-27
  • 打赏
  • 举报
回复
package connectionpool;

/**
* @author jcreater
*
* 更改所生成类型注释的模板为
* 窗口 > 首选项 > Java > 代码生成 > 代码和注释
*/

import java.sql.*;
import java.util.*;
import connectionpool.ConnectionFactory;

public class ConnectionPool {

/**布尔数组用于判断连接是否使用*/
private boolean inUse[];
/**初始化时为10个连接*/
private int poolSize = 10;

private ArrayList connections;

public ConnectionPool() {
connections = new ArrayList();
inUse = new boolean[poolSize];
try {
setupConnectionPool();
} catch (SQLException e) {
System.out.println("初始化连接池错误" + e.getMessage());
}
}

/**初始化连接*/
public void setupConnectionPool() throws SQLException {
for (int i = 0; i < poolSize; i++) {
Connection conn = ConnectionFactory.getConnection();
connections.add(conn);
inUse[i] = false;
}
}

/**从连接池里取出一连接*/
public Connection getConnection() {
Connection conn = null;
for (int i = 0; i < connections.size(); i++) {
if (inUse[i] == false) {
conn = (Connection) connections.get(i);
inUse[i] = true;
break;
}
}
return conn;
}

/**把一个连接释放到连接池中*/
public void freeConnection(int index) {
inUse[index] = false;
}

public static void main(String[] args) {
Connection c[] = new Connection[10];
ConnectionPool pool = new ConnectionPool();
for (int i = 0; i < 10; i++) {
c[i] = pool.getConnection();
}
for (int i = 0; i < c.length; i++) {
System.out.println("连接:" + c[i]);
}
}

}
fantasyCoder 2004-01-27
  • 打赏
  • 举报
回复
一个较好的解决方案:
使用Facade模式

package connectionpool;

/**
* @author jcreater
*
* 更改所生成类型注释的模板为
* 窗口 > 首选项 > Java > 代码生成 > 代码和注释
*/

import java.sql.*;

public class ConnectionFactory {

public static ConnectionFactory ref=new ConnectionFactory();
public ConnectionFactory(){
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
}catch(ClassNotFoundException e){
System.out.println("驱动加载错误");
}
}

public static Connection getConnection()throws SQLException{
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=myBBS";
String user="sa";
String password="2361183";
return DriverManager.getConnection(url,user,password);
}
}

23,404

社区成员

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

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