- package com.lh.backupserver;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.ResultSetMetaData;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- import java.util.List;
- import com.lh.Server.ImpServlet;
- public class DataDAO
- {
- private static Connection conn = null;
- private static Statement stmt = null;
- private static ResultSet rs = null;
- //根据不同的数据库来获取不同数据库的链接
- public static Connection getConnection(String DBName)
- {
- try
- {
- if(DBName.equalsIgnoreCase("a"))
- {
- // Class.forName(ImpServlet.DRIVERSA);
- // conn = DriverManager.getConnection(ImpServlet.URL_1, ImpServlet.USER_1, ImpServlet.PASSWORD_1);
- Class.forName("oracle.jdbc.driver.OracleDriver");
- conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ORCL", "airdrome", "lz0921air");
- }
- else if(DBName.equalsIgnoreCase("b"))
- {
- Class.forName(ImpServlet.DRIVERSB);
- conn = DriverManager.getConnection(ImpServlet.URL_2, ImpServlet.USER_2, ImpServlet.PASSWORD_2);
- }
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- catch (ClassNotFoundException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return conn;
- }
- //关闭连接
- public static void getClose()
- {
- try
- {
- if(rs != null)
- rs.close();
- if(stmt != null)
- stmt.close();
- if(conn != null)
- conn.close();
- }
- catch (Exception e)
- {
- // TODO: handle exception
- throw new RuntimeException("数据库关闭异常");
- }
- }
- //更新记录
- public static int executeUpdate(String sql, String DBName)
- {
- int count = 0;
- try
- {
- stmt = DataDAO.getConnection(DBName).createStatement();
- count = stmt.executeUpdate(sql);
- DataDAO.getClose();
- return count;
- }
- catch (Exception e)
- {
- // TODO: handle exception
- e.printStackTrace();
- }
- return 0;
- }
- //获取结果集
- public static ResultSet executeSelect(String sql, String DBName)
- {
- ResultSet rs = null;
- try
- {
- stmt = getConnection(DBName).createStatement();
- rs = stmt.executeQuery(sql);
- }
- catch (Exception e)
- {
- // TODO: handle exception
- e.printStackTrace();
- }
- return rs;
- }
- //获取结果集列数
- public static int getColumnCount(String sql, String DBName)
- {
- int count = 0;
- ResultSetMetaData rsmd = null;
- try
- {
- rsmd = executeSelect(sql, DBName).getMetaData();
- count = rsmd.getColumnCount();
- }
- catch (Exception e)
- {
- // TODO: handle exception
- e.printStackTrace();
- }
- return count;
- }
- //查询Oracle中某用户下所有表
- public static<T> List<T> getAllTable(String DBName, String userName)
- {
- List<T> tables = null;
- String sql = "select table_name from all_tables where owner='"+ userName + "'";
- try
- {
- tables = new ArrayList<T>();
- rs = getConnection(DBName).createStatement().executeQuery(sql);
- while(rs.next())
- {
- tables.add((T)rs.getString(1));
- }
- getClose();
- }
- catch (Exception e)
- {
- // TODO: handle exception
- e.printStackTrace();
- throw new RuntimeException("查询用户下所有的表时出错!");
- }
- return tables;
- }
- public static void main(String[] args)
- {
- List list = getAllTable("a", "LIUHUAN");
- for(int i = 0; i < list.size(); i++)
- {
- System.out.println(list.get(i));
- }
- int count = getColumnCount("select * from scott.emp", "a");
- System.out.println(count);
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/171220138092.html
来源: http://www.codesnippet.cn/detail/171220138092.html