Oracle,Mysql,Db2 Connection方法
oracle数据库连接方法:
package com.abin.db.connection;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.Statement;public final class DBClassConnection {//Oracle 的数据库连接参数private static String driver="oracle.jdbc.driver.OracleDriver";private static String url="jdbc:oracle:thin:@localhost:1521:orcl";private static String username="zhang";private static String password="zhang";public DBClassConnection ()throws ClassNotFoundException{}//Oracle连接public static Connection getOracle(){Connection conn=null;try{if(null == conn || conn.isClosed()){Class.forName(driver).newInstance();conn=DriverManager.getConnection(url,username,password);}}catch(Exception e){e.printStackTrace();throw new RuntimeException(e);}return conn;}//oracle插入,修改,删除语句(PreparedStatement)executeUpdatepublic static int psexecuteUpdate(String sql){int count=0;Connection conn=getOracle();try{PreparedStatement ps=null;if(null==ps||ps.isClosed()){ps=conn.prepareStatement(sql);count=ps.executeUpdate();}}catch(Exception e){throw new RuntimeException(e);}return count;}//oracle插入,修改,删除语句(PreparedStatement)executepublic static boolean psexecute(String sql){boolean flag=false;try{Connection conn=getOracle();PreparedStatement ps=null;if(null==ps||ps.isClosed()){ps=conn.prepareStatement(sql);flag=ps.execute();}}catch(Exception e){throw new RuntimeException(e);}return flag;}//oracle插入,修改,删除语句(PreparedStatement)executepublic static boolean stexecute(String sql){boolean flag=false;try{Connection conn=getOracle();Statement stmt=null;if(null==stmt||stmt.isClosed()){stmt=conn.createStatement();flag=stmt.execute(sql);}}catch(Exception e){throw new RuntimeException(e);}return flag;}//oracle插入,修改,删除语句(Statement)public static int stexecuteUpdate(String sql){int count=0;Connection conn=getOracle();try{Statement stmt=null;if(null==stmt||stmt.isClosed()){stmt=conn.createStatement();stmt.executeUpdate(sql);}}catch(Exception e){throw new RuntimeException(e);}return count;}//oracle 的 Statement结果集 ResultSetpublic static ResultSet stexecuteQuery(String sql){ResultSet rs=null;try{Connection conn=getOracle();if(null==rs||rs.isClosed()){Statement stmt=conn.createStatement();rs=stmt.executeQuery(sql);}}catch(Exception e){throw new RuntimeException(e);}return rs;}//oracle 的 PreparedStatement结果集 ResultSetpublic static ResultSet psexecuteQuery(){ResultSet rs=null;PreparedStatement ps=null;try{if(null==rs||rs.isClosed()){rs=ps.executeQuery();}}catch(Exception e){throw new RuntimeException(e);}return rs;}//关闭连接PreparedStatementpublic static void close(Connection conn,PreparedStatement ps,ResultSet rs){try{if(null!=conn){conn.close();}}catch(Exception e){throw new RuntimeException(e);}try{if(null!=ps){ps.close();}}catch(Exception e){throw new RuntimeException(e);}try{if(null!=rs){rs.close();}}catch(Exception e){throw new RuntimeException(e);}}//关闭连接Statementpublic static void close(Connection conn,Statement stmt,ResultSet rs){try{if(null!=conn){conn.close();}}catch(Exception e){throw new RuntimeException(e);}try{if(null!=stmt){stmt.close();}}catch(Exception e){throw new RuntimeException(e);}try{if(null!=rs){rs.close();}}catch(Exception e){throw new RuntimeException(e);}}}
package com.abin.db.connection;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.Statement;public final class DBClassMysql {//Mysql 数据库连接参数private static String driver="com.mysql.jdbc.Driver";private static String url="jdbc:mysql://localhost:3306/admin";private static String user="root";private static String password="root";public DBClassMysql ()throws ClassNotFoundException{}//Mysql连接public static Connection getMysql(){Connection conn=null;try{if(null==conn||conn.isClosed()){Class.forName(driver).newInstance();conn=DriverManager.getConnection(url,user,password);}}catch(Exception e){e.printStackTrace();throw new RuntimeException(e);}return conn;}//oracle插入,修改,删除语句(PreparedStatement)executeUpdatepublic static int psexecuteUpdate(String sql){int count=0;Connection conn=getMysql();try{PreparedStatement ps=null;if(null==ps||ps.isClosed()){ps=conn.prepareStatement(sql);count=ps.executeUpdate();}}catch(Exception e){throw new RuntimeException(e);}return count;}//oracle插入,修改,删除语句(PreparedStatement)executepublic static boolean psexecute(String sql){boolean flag=false;try{Connection conn=getMysql();PreparedStatement ps=null;if(null==ps||ps.isClosed()){ps=conn.prepareStatement(sql);flag=ps.execute();}}catch(Exception e){throw new RuntimeException(e);}return flag;}//oracle插入,修改,删除语句(PreparedStatement)executepublic static boolean stexecute(String sql){boolean flag=false;try{Connection conn=getMysql();Statement stmt=null;if(null==stmt||stmt.isClosed()){stmt=conn.createStatement();flag=stmt.execute(sql);}}catch(Exception e){throw new RuntimeException(e);}return flag;}//oracle插入,修改,删除语句(Statement)public static int stexecuteUpdate(String sql){int count=0;Connection conn=getMysql();try{Statement stmt=null;if(null==stmt||stmt.isClosed()){stmt=conn.createStatement();stmt.executeUpdate(sql);}}catch(Exception e){throw new RuntimeException(e);}return count;}//oracle 的 Statement结果集 ResultSetpublic static ResultSet stexecuteQuery(String sql){ResultSet rs=null;try{Connection conn=getMysql();if(null==rs||rs.isClosed()){Statement stmt=conn.createStatement();rs=stmt.executeQuery(sql);}}catch(Exception e){throw new RuntimeException(e);}return rs;}//oracle 的 PreparedStatement结果集 ResultSetpublic static ResultSet psexecuteQuery(){ResultSet rs=null;PreparedStatement ps=null;try{if(null==rs||rs.isClosed()){rs=ps.executeQuery();}}catch(Exception e){throw new RuntimeException(e);}return rs;}public static void close(Connection conn,PreparedStatement ps,ResultSet rs){try{if(null!=conn){conn.close();}}catch(Exception e){throw new RuntimeException(e);}try{if(null!=ps){ps.close();}}catch(Exception e){throw new RuntimeException(e);}try{if(null!=rs){rs.close();}}catch(Exception e){throw new RuntimeException(e);}}//关闭连接Statementpublic static void close(Connection conn,Statement stmt,ResultSet rs){try{if(null!=conn){conn.close();}}catch(Exception e){throw new RuntimeException(e);}try{if(null!=stmt){stmt.close();}}catch(Exception e){throw new RuntimeException(e);}try{if(null!=rs){rs.close();}}catch(Exception e){throw new RuntimeException(e);}}}
package com.abin.db.connection;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.Statement;public final class DBClassDbtwo {//Oracle 的数据库连接参数private static String driver="com.ibm.db2.jcc.DB2Driver";private static String url="jdbc:db2://localhost:50000/you";private static String username="acer";private static String password="abin";public DBClassDbtwo ()throws ClassNotFoundException{}//Oracle连接public static Connection getDbtwo(){Connection conn=null;try{if(null == conn || conn.isClosed()){Class.forName(driver).newInstance();conn=DriverManager.getConnection(url,username,password);}}catch(Exception e){e.printStackTrace();throw new RuntimeException(e);}return conn;}//oracle插入,修改,删除语句(PreparedStatement)executeUpdatepublic static int psexecuteUpdate(String sql){int count=0;Connection conn=getDbtwo();try{PreparedStatement ps=null;if(null==ps||ps.isClosed()){ps=conn.prepareStatement(sql);count=ps.executeUpdate();}}catch(Exception e){throw new RuntimeException(e);}return count;}//oracle插入,修改,删除语句(PreparedStatement)executepublic static boolean psexecute(String sql){boolean flag=false;try{Connection conn=getDbtwo();PreparedStatement ps=null;if(null==ps||ps.isClosed()){ps=conn.prepareStatement(sql);flag=ps.execute();}}catch(Exception e){throw new RuntimeException(e);}return flag;}//oracle插入,修改,删除语句(PreparedStatement)executepublic static boolean stexecute(String sql){boolean flag=false;try{Connection conn=getDbtwo();Statement stmt=null;if(null==stmt||stmt.isClosed()){stmt=conn.createStatement();flag=stmt.execute(sql);}}catch(Exception e){throw new RuntimeException(e);}return flag;}//oracle插入,修改,删除语句(Statement)public static int stexecuteUpdate(String sql){int count=0;Connection conn=getDbtwo();try{Statement stmt=null;if(null==stmt||stmt.isClosed()){stmt=conn.createStatement();stmt.executeUpdate(sql);}}catch(Exception e){throw new RuntimeException(e);}return count;}//oracle 的 Statement结果集 ResultSetpublic static ResultSet stexecuteQuery(String sql){ResultSet rs=null;try{Connection conn=getDbtwo();if(null==rs||rs.isClosed()){Statement stmt=conn.createStatement();rs=stmt.executeQuery(sql);}}catch(Exception e){throw new RuntimeException(e);}return rs;}//oracle 的 PreparedStatement结果集 ResultSetpublic static ResultSet psexecuteQuery(){ResultSet rs=null;PreparedStatement ps=null;try{if(null==rs||rs.isClosed()){rs=ps.executeQuery();}}catch(Exception e){throw new RuntimeException(e);}return rs;}public static void close(Connection conn,PreparedStatement ps,ResultSet rs){try{if(null!=conn){conn.close();}}catch(Exception e){throw new RuntimeException(e);}try{if(null!=ps){ps.close();}}catch(Exception e){throw new RuntimeException(e);}try{if(null!=rs){rs.close();}}catch(Exception e){throw new RuntimeException(e);}}//关闭连接Statementpublic static void close(Connection conn,Statement stmt,ResultSet rs){try{if(null!=conn){conn.close();}}catch(Exception e){throw new RuntimeException(e);}try{if(null!=stmt){stmt.close();}}catch(Exception e){throw new RuntimeException(e);}try{if(null!=rs){rs.close();}}catch(Exception e){throw new RuntimeException(e);}}}