mysql和oracle的连接

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);
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值