mysql.properties文件
oracle.driver=oracle.jdbc.driver.OracleDriver oracle.url=jdbc:oracle:thin:@localhost:1521:xe oracle.username=数据库名称 oracle.password=密码
oracle连接数据库代码
package com.zy.utils; import java.io.IOException; import java.sql.CallableStatement; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Properties; public class DBUtil { private static String driver = "" ; private static String url = "" ; private static String username = "" ; private static String password = "" ; static { Properties pp = new Properties(); try { pp.load(DBUtil.class.getClassLoader().getResourceAsStream("mysql.properties")); driver = pp.getProperty("oracle.driver"); url = pp.getProperty("oracle.url"); username = pp.getProperty("oracle.username"); password = pp.getProperty("oracle.password"); Class.forName(driver); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static Connection getConnection (){ Connection conn = null ; try { conn = DriverManager.getConnection(url, username, password); } catch (SQLException e) { e.printStackTrace(); } return conn ; } public static void close(ResultSet rs , CallableStatement cs , Connection conn){ try { if(rs!=null /*&& !rs.isClosed()*/){ rs.close(); } if(cs!=null/* && !ps.isClosed()*/){ cs.close(); } if(conn!=null /*&& !conn.isClosed()*/){ conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } public static void main(String[] args) { System.out.println(DBUtil.getConnection()); } }
调用该数据库方法
package com.zy.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import com.zy.utils.DBUtil; public class QueryAll { Connection conn=null; PreparedStatement ps=null; ResultSet rs=null; ArrayList<String> arrayList=new ArrayList<String>(); public void getConn(){ conn= DBUtil.getConnection(); } public void callProc(){ String sql="select * from t_user"; try { ps=conn.prepareStatement(sql); rs=ps.executeQuery(); while(rs.next()){ arrayList.add(rs.getString("password")); } System.out.println(arrayList); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { rs.close(); ps.close(); conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static void main(String[] args) { QueryAll queryAll=new QueryAll(); queryAll.getConn(); queryAll.callProc(); } }