java通过jdbc连接oracle和mysql
相关链接:
OJDBC所有版本下载:https://download.csdn.net/download/qq_31049375/10688712
OJDBC版本说明:https://blog.csdn.net/qq_31049375/article/details/82853074
MySql_JDBC下载:https://download.csdn.net/download/qq_31049375/10688798
package com.yhh.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class BaseDao {
/*
* OJDBC的URL有两种常用格式
*
* 1.通过SID连接ORACLE
* SID查询方式: select value from v$parameter where name=’instance_name’
* url="jdbc:oracle:thin:@host:port:SID";
* port默认是1521
*
* 2.通过数据库名连接ORACLE
* 数据库名查询方式:select value from v$parameter where name=’service_names’
* url="jdbc:oracle:thin:@//host:port/service_name";
* port默认是1521
*
* MySql的URL格式
* url="jdbc:mysql://host:port/db_name";
* port 默认是 3306
*/
private static String url="url";
/*
* oracle的驱动器
* String jdbc_driver="oracle.jdbc.driver.OracleDriver"
*
* mysql的驱动器
* String jdbc_driver="com.mysql.jdbc.Driver";
*/
private static String jdbc_driver="driver";
private static String username="username";
private static String password="password";
public static Connection conn=null;
public static Statement stmt=null;
public static ResultSet rs=null;
public static Connection openConnection() {
try {
Class.forName(jdbc_driver);
conn = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void closeConnection() {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}