菜鸟请教一个jsp数据库简单问题

xiaoluo 2001-08-15 09:56:47
1。如何把Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:test","sa","");
Statement statement=con.createStatement() ;这些连接数据库的东西写个简单的javabean?

2。java.sql里的resultset,如果执行完一条sql语句,还要执行一个,是不是要重建一个resultset?要不要重建connection和Statement等对象
...全文
69 点赞 收藏 2
写回复
2 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
lci21 2001-08-15
1.给你一个对数据库增删改的bean:里面包含又和数据库的连接。

QueryBean.java:

/*
*writed by lci21
*/
import java.sql.*;
public class QueryBean
{
public String query_statement;
public String param[]; //查询条件,或者是新的记录
public ResultSet result=null;
public Connection conn;

public void setParam(String[] param)
{
this.param=param;
}
public void setQuerystatement(String query_statement)
{
this.query_statement=query_statement;
}
public void setConnection(String driverName,String jdbcURL,String username,String passwd) throws Exception
{
Connection conn1;
Class.forName(driverName);
conn1=DriverManager.getConnection(jdbcURL,username,passwd);
conn1.setAutoCommit(false);
this.conn = conn1;
}

public ResultSet getResult()
{
try
{
PreparedStatement select_stm=conn.prepareStatement(query_statement,java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);
if (param!=null)
for(int i=0;i<param.length;i++)
select_stm.setString(i+1,param[i]);
result=select_stm.executeQuery();
}catch(Exception e){System.out.println(e);}
return result;
}


public void insertRecord() throws SQLException,java.io.UnsupportedEncodingException
{
try
{
PreparedStatement insert_stm=conn.prepareStatement(query_statement);
if (param!=null)
for(int j=0;j<param.length;j++)
insert_stm.setString(j+1,param[j]);
insert_stm.executeUpdate();
insert_stm.close();
conn.commit();
}
catch(Exception e)
{
System.out.println(e);
conn.rollback();
}
}


public void updateRecord() throws SQLException,java.io.UnsupportedEncodingException
{
try
{
PreparedStatement update_stm=conn.prepareStatement(query_statement);
if (param!=null)
for (int k=0;k<param.length;k++)
update_stm.setString(k+1,param[k]);
update_stm.executeUpdate();
update_stm.close();
conn.commit();
}
catch(Exception e)
{
System.out.println(e);
conn.rollback();
}
}

}

2.如果你用完一个,以后就不再用了,不需要重建,只需要重新赋就行
回复
lci21 2001-08-15
1:
public Connection conn;

public void setConnection(String driverName,String jdbcURL,String username,String passwd) throws Exception
{
Connection conn1;
Class.forName(driverName);
conn1=DriverManager.getConnection(jdbcURL,username,passwd);
conn1.setAutoCommit(false);
this.conn = conn1;
}

回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2001-08-15 09:56
社区公告
暂无公告