日期:2014-05-16 浏览次数:20485 次
package com.db; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ResourceBundle; /** * jdbc读取配置文件连接 * @author 赵永恩 * */ public class DataBase { ResourceBundle bundle = ResourceBundle.getBundle("DBConfig");//DBConfig.properties配置文件的名称 String driver = bundle.getString("driver"); String url = bundle.getString("url"); String user = bundle.getString("user"); String password = bundle.getString("password"); Connection c = null; Statement conn = null; ResultSet rs = null; public DataBase() { System.out.println(driver); System.out.println(url); System.out.println(user); System.out.println(password); try { Class.forName(driver).newInstance(); c = DriverManager.getConnection(url, user, password); // 创建滚动结果集 // 需要把创建stme对象后多加两个参数ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE conn = c.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); } catch (Exception e) { e.printStackTrace(); } } /** * 更新 * * @param sql * @return */ public boolean executeUpdate(String sql) { try { conn.executeUpdate(sql); return true; } catch (SQLException e) { e.printStackTrace(); return false; } } /** * 查询 * * @param sql * @return */ public ResultSet executeQuery(String sql) { rs = null; try { rs = conn.executeQuery(sql); } catch (SQLException e) { e.printStackTrace(); } return rs; } /** * 关闭数据库 */ public void close() { try { conn.close(); c.close(); } catch (Exception e) { e.printStackTrace(); } } /** * 测试方法 * * @param args * @author 赵永恩 2011-3-10 */ public static void main(String[] args) { DataBase db = new DataBase(); ResultSet rs = db.executeQuery("select * from zykuser"); try { while (rs.next()) { System.out.println(rs.getString("userid")); System.out.println("ddd"); } } catch (Exception e) { e.printStackTrace(); } finally { db.close(); } //增加 //String sql="insert into zykuser(userid) VALUES('1')"; //db.executeUpdate(sql); //删除 //String sql1="delete from zykuser where userid='343712951' "; //db.executeUpdate(sql); } }
driver=oracle.jdbc.driver.OracleDriver url=jdbc:oracle:thin:@127.0.0.1:1521:orcl user=system password=password