Topic: 连接数据库用的JavaBean |
Print this page |
1.连接数据库用的JavaBean | Copy to clipboard |
Posted by: icecloud Posted on: 2003-01-03 03:47 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(){ } } |
2.Re:连接数据库用的JavaBean [Re: icecloud] | Copy to clipboard |
Posted by: 00000000 Posted on: 2003-01-13 15:33 surport |
3.Re:连接数据库用的JavaBean [Re: icecloud] | Copy to clipboard |
Posted by: floater Posted on: 2003-01-13 22:46 In public boolean executeUpdate(String sql) returning the rowCount is better since it's a very useful info for the users of this bean. |
4.Re:连接数据库用的JavaBean [Re: icecloud] | Copy to clipboard |
Posted by: flytiger Posted on: 2003-02-13 01:27 Why don't close the statement, resultset and connection? |
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 |