[转]java 连接 各种数据库的函数
来源:互联网 发布:php 判断语句多个条件 编辑:程序博客网 时间:2024/05/01 13:23
- Java code
- /* * Db.javaCreated on 2007年8月20日, 上午 8:37*/import java.io.*;import java.sql.*;import java.util.Properties;public class Db { private String driver; private String url; private String user; private String password; private Connection conn; private Statement stm; private ResultSet rs; public Db(){ this("DBConf.properties"); } public Db(String conf) { loadProperties(conf); setConn(); } public Connection getConn(){ return this.conn; } //handle the properties file to get the informations for connection private void loadProperties(String conf){ Properties props = new Properties(); try { props.load(new FileInputStream(conf)); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } this.driver = props.getProperty("driver"); this.url = props.getProperty("url"); this.user = props.getProperty("user"); this.password = props.getProperty("password"); } //implement the Connection private void setConn(){ try { Class.forName(driver); this.conn = DriverManager.getConnection(url,user,password); } catch(ClassNotFoundException classnotfoundexception) { classnotfoundexception.printStackTrace(); System.err.println("db: " + classnotfoundexception.getMessage()); } catch(SQLException sqlexception) { System.err.println("db.getconn(): " + sqlexception.getMessage()); } } public void doInsert(String sql) { try { Statement statement = conn.createStatement(); int i = stm.executeUpdate(sql); } catch(SQLException sqlexception) { System.err.println("db.executeInset:" + sqlexception.getMessage()); } } public void doDelete(String sql) { try { stm = conn.createStatement(); int i = stm.executeUpdate(sql); } catch(SQLException sqlexception) { System.err.println("db.executeDelete:" + sqlexception.getMessage()); } } public void doUpdate(String sql) { try { stm = conn.createStatement(); int i = stm.executeUpdate(sql); } catch(SQLException sqlexception) { System.err.println("db.executeUpdate:" + sqlexception.getMessage()); } } public ResultSet doSelect(String sql) { try { stm = conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY); rs = stm.executeQuery(sql); } catch(SQLException sqlexception) { System.err.println("db.executeQuery: " + sqlexception.getMessage()); } return rs; } public static void main(String[] args){ try{ Db db = new Db(); Connection conn = db.getConn(); if(conn != null && !conn.isClosed()) { System.out.println("連結成功"); ResultSet rs = db.doSelect("select * from content"); while(rs.next()){ System.out.println(rs.getString(1)+":"+rs.getString(2)+":"+rs.getString(3)); } rs.close(); conn.close(); } }catch(SQLException e) { e.printStackTrace(); } } }
DBConf.properties:
driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@tdt151:1521:train
user=XX
password=XX
- [转]java 连接 各种数据库的函数
- java 连接 各种数据库的函数
- Java连接各种数据库的实例 (转)
- Java各种数据库的连接
- 连接各种数据库的连接字符串(java)
- JAVA连接各种数据库
- java连接各种数据库
- JAVA连接各种数据库
- JAVA 连接各种数据库
- Java连接各种数据库
- java连接各种数据库
- Java连接各种数据库
- java连接各种数据库
- Java连接各种数据库
- Java连接各种数据库
- Java连接各种数据库
- Java连接各种数据库
- Java连接各种数据库
- 第7集 构造函数中抛出的异常
- redhat上的ftp配置实例【转】
- 第8集析构函数中抛出的异常
- Timestamp Format Codes
- ASP.NET杜绝文件上传漏洞的代码
- [转]java 连接 各种数据库的函数
- vmware 下的redhat linux 9.0 的host only上网配置 【转】
- redhat linux9 如何从文本模式启动改为图形模式启动?
- C++中string和string.h的作用和区别
- LINUX必学的60个命令
- 三生石
- RedHat9-vsftp配置大全
- google栅格map技术实现原理和用户体验方式
- chinaitlab上看到的一篇文章————揪出"程序员"身上的"六宗罪"