mysql和oracle的连接
来源:互联网 发布:守望者 知乎 编辑:程序博客网 时间:2024/06/16 18:56
package com.atgaozhen.jdbc;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class JDBCTool {
private InputStream resultSet;
public void CloseFunction2(Connection
connection,Statement statement
) throws SQLException{
try {
if(statement!=null)
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(connection!=null)
connection.close();
}
}
//4.執行update
public void updatesql(String sql) throws IOException,
SQLException, ClassNotFoundException{
Connection connection=null;
Statement statement=null;
try {
connection=getConnection();
statement=(Statement) connection.createStatement();
statement.executeUpdate(sql);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
CloseFunction2(connection,statement);
}
}
//3.執行sql
public void executeselect(String sql) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException, IOException{
Connection connection=null;
Statement statement=null;
ResultSet resultSet=null;
try {
connection=getConnection();
statement=connection.createStatement();
resultSet=statement.executeQuery(sql);
if(resultSet.next()){
int id=resultSet.getInt(1);
String username=resultSet.getString(2);
String password=resultSet.getString(3);
System.out.println("id:"+id);
System.out.println("username:"+username);
System.out.println("password:"+password);
}
//关闭数据库资源
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
CloseFunction(
connection,statement,
resultSet);
}
}
//2.关闭资源的方法
public void CloseFunction(Connection
connection,Statement statement,
ResultSet resultSet) throws SQLException{
try {
if(resultSet!=null)
resultSet.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(statement!=null)
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(connection!=null)
connection.close();
}
}
}
//1.获取connection的方法
public Connection getConnection() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException, IOException {
String url=null;
String user=null;
String password=null;
String name=null;
//1.读取类路径下的jdbc.properties文件
InputStream in=
getClass().getClassLoader().getResourceAsStream("jdbc.properties");
Properties properties=new Properties();
properties.load(in);
name=properties.getProperty("name");
url=properties.getProperty("url");
user=properties.getProperty("user");
password=properties.getProperty("password");
Driver driver=(Driver) Class.forName(name).newInstance();
Properties info=new Properties();
info.put("user",user);
info.put("password",password);
Connection connection=driver.connect(url, info);
return connection;
//System.out.println("通用Driver connection: "+connection);
}
//可变参数
public void update(String sql, Object ... argu) throws SQLException {
Connection connection=null;
PreparedStatement preparedStatement=null;
try {
connection=new JDBCTool().getConnection();
preparedStatement=connection.prepareStatement(sql);
for(int i=0;i<argu.length;++i){
preparedStatement.setObject(i+1, argu[i]);
}
System.out.println(sql);
preparedStatement.executeUpdate();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
new JDBCTool().CloseFunction(connection, preparedStatement, null);
}
}
}
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class JDBCTool {
private InputStream resultSet;
public void CloseFunction2(Connection
connection,Statement statement
) throws SQLException{
try {
if(statement!=null)
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(connection!=null)
connection.close();
}
}
//4.執行update
public void updatesql(String sql) throws IOException,
SQLException, ClassNotFoundException{
Connection connection=null;
Statement statement=null;
try {
connection=getConnection();
statement=(Statement) connection.createStatement();
statement.executeUpdate(sql);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
CloseFunction2(connection,statement);
}
}
//3.執行sql
public void executeselect(String sql) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException, IOException{
Connection connection=null;
Statement statement=null;
ResultSet resultSet=null;
try {
connection=getConnection();
statement=connection.createStatement();
resultSet=statement.executeQuery(sql);
if(resultSet.next()){
int id=resultSet.getInt(1);
String username=resultSet.getString(2);
String password=resultSet.getString(3);
System.out.println("id:"+id);
System.out.println("username:"+username);
System.out.println("password:"+password);
}
//关闭数据库资源
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
CloseFunction(
connection,statement,
resultSet);
}
}
//2.关闭资源的方法
public void CloseFunction(Connection
connection,Statement statement,
ResultSet resultSet) throws SQLException{
try {
if(resultSet!=null)
resultSet.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(statement!=null)
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(connection!=null)
connection.close();
}
}
}
//1.获取connection的方法
public Connection getConnection() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException, IOException {
String url=null;
String user=null;
String password=null;
String name=null;
//1.读取类路径下的jdbc.properties文件
InputStream in=
getClass().getClassLoader().getResourceAsStream("jdbc.properties");
Properties properties=new Properties();
properties.load(in);
name=properties.getProperty("name");
url=properties.getProperty("url");
user=properties.getProperty("user");
password=properties.getProperty("password");
Driver driver=(Driver) Class.forName(name).newInstance();
Properties info=new Properties();
info.put("user",user);
info.put("password",password);
Connection connection=driver.connect(url, info);
return connection;
//System.out.println("通用Driver connection: "+connection);
}
//可变参数
public void update(String sql, Object ... argu) throws SQLException {
Connection connection=null;
PreparedStatement preparedStatement=null;
try {
connection=new JDBCTool().getConnection();
preparedStatement=connection.prepareStatement(sql);
for(int i=0;i<argu.length;++i){
preparedStatement.setObject(i+1, argu[i]);
}
System.out.println(sql);
preparedStatement.executeUpdate();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
new JDBCTool().CloseFunction(connection, preparedStatement, null);
}
}
}
0 0
- mysql和oracle的连接
- 连接mysql和oracle数据库的配置
- oracle和Mysql连接数据库
- SAS连接Oracle和Mysql
- oracle 和 mysql 连接字符串
- jdbc连接mysql和oracle的驱动程序名称
- QT连接Oracle和Mysql的详细步骤,已成功~!
- Spring连接MySQL、Oracle和SQL Server的数据库连接属性
- mysql以及oracle数据库的驱动和连接
- jdbc与oracle和mysql连接工具类的写法
- SH 连接Oracle和MySQL数据库的不同配置
- JDBC-连接oracle和MySql数据库的基类
- JDBC连接Mysql和Oracle的源码格式
- JDBC连接MySQL和Oracle数据库的规范写法
- jdbc连接mysql,oracle的URL和DriverClass
- mysql和oracle对于内连接和左右连接的使用(一)
- JDBC连接Oracle和Mysql数据库示例
- jdbc连接方式(Oracle和mySql)
- Java中的一些常见类System、Runtime、Date与Carendar
- Servlet学习1
- ACM1008:Elevator
- [python]学习笔记4-关于列表去重练习
- NYOJ_119_士兵杀敌(三)(RMQ-ST)
- mysql和oracle的连接
- Java内部类的作用
- Android 与 JDK 下查找包下的类
- hdoj 4123 Bob’s Race 【树的直径 + RMQ】 【二分查询会超时】
- offsetLeft与style.left的区别
- 欢迎使用CSDN-markdown编辑器
- 【前端学习笔记】Javascript中的正则表达式-实例
- 博客第一言
- ZOJ 1671 --Walking Ant【BFS】