Oracle,Mysql,Db2 Connection方法

本文提供了Oracle、MySQL和DB2三种常用数据库的Java连接方法,包括连接参数设置、基本操作如增删改查等,并展示了如何处理异常及关闭资源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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)executeUpdate
public 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)execute
public 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)execute
public 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结果集 ResultSet
public 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结果集 ResultSet
public 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;
}

//关闭连接PreparedStatement
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);
}
}
//关闭连接Statement
public 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);
}
}





}








Mysql数据库连接方法:


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)executeUpdate
public 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)execute
public 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)execute
public 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结果集 ResultSet
public 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结果集 ResultSet
public 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);
}
}
//关闭连接Statement
public 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);
}
}



}





DB2方法:



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)executeUpdate
public 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)execute
public 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)execute
public 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结果集 ResultSet
public 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结果集 ResultSet
public 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);
}
}

//关闭连接Statement
public 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);
}
}




}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值