Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » 技术文章库  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 连接数据库用的JavaBean
icecloud



发贴: 0
积分: 0
于 2003-01-03 03:47 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
package works;
//我的包

import java.sql.*;

public class JdbcBean {
private String sDBDriver="sun.jdbc.odbc.JdbcOdbcDriver";
private String connStr="jdbc:odbc:Mydata";
private Connection conn=null;
private Statement stmt=null;
private String username="";
private String password="";
private ResultSet rs=null;

public JdbcBean() throws SQLException {
try {
//注册数据库驱动程序为Oracle驱动

Class.forName(sDBDriver);
conn = DriverManager.getConnection(connStr,username,password);
}
catch(java.lang.ClassNotFoundException e) {
System.err.println("jdbcBean(): " + e.getMessage());
}
}

//executeQuery方法用于进行记录的查询操作

//入口参数为sql语句,返回ResultSet对象
public ResultSet executeQuery(String sql)
{
try {
//建立数据库连接

stmt = conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);
//执行数据库查询操作
rs = stmt.executeQuery(sql);
}
catch (SQLException ex) {
System.err.println("db.executeQuery: " + sql + "" + ex.getMessage());
}
finally{
return rs;
}
}

//executeUpdate方法用于进行add或者update记录的操作

//入口参数为sql语句,成功返回true,否则为false
public boolean executeUpdate(String sql) {
boolean bupdate = false;
rs = null;
try {
//建立数据库连接,其它参数说明同上面的一样
stmt = conn.createStatement();
int rowCount = stmt.executeUpdate(sql);
//如果不成功,bupdate就会返回0
if (rowCount != 0)
bupdate = true;
}
catch (SQLException ex) {
//打印出错信息
System.err.println("db.executeUpdate: " + ex.getMessage());
}
finally {
return bupdate;
}
}

protected void finalize(){

}

}



作者 Re:连接数据库用的JavaBean [Re:icecloud]
00000000



发贴: 0
积分: 0
于 2003-01-13 15:33 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
surport


作者 Re:连接数据库用的JavaBean [Re:icecloud]
floater

Java Jedi

总版主


发贴: 3233
积分: 421
于 2003-01-13 22:46 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
In
public boolean executeUpdate(String sql)

returning the rowCount is better since it's a very useful info for the users of this bean.



"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
- Martin Fowler, Refactoring - Improving the Design of Existing Code
作者 Re:连接数据库用的JavaBean [Re:icecloud]
flytiger





发贴: 79
积分: 21
于 2003-02-13 01:27 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
Why don't close the statement, resultset and connection?



flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923