数据库连接方式比较

 

Oracle连接方式

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBConnection {
    public DBConnection(){
        driver="oracle.jdbc.driver.OracleDriver";
        url="jdbc:oracle:thin:@localhost:1521:myoracle";
        dbusername="system";
        dbpassword="admin";
        this.getConection();
    }
    
    private String driver;
    private String url;
    private String dbusername;
    private String dbpassword;
    private Connection con;
    private Statement sta;
    private ResultSet rs;
    
    /*
     * 得到Connection对象。
     */
    public Connection getConection(){
        try {
            Class.forName(driver);
            con=DriverManager.getConnection(url,dbusername,dbpassword);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch(SQLException e){
            e.printStackTrace();
        }
        return con;
    }
    
    /*
     * 得到Statement对象
     */
    public Statement getStatement(){
        try {
            sta=con.createStatement();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return sta;
    }
}

SQL SERVER

package com.wy.tools;

import java.sql.*;

/**
 * 
 * @author Administrator
 */
public class JDBConnection {
    private final String dbDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
    private final String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_register";
    private final String userName = "sa";
    private final String password = "";
    private ResultSet rs = null;
    private Statement stmt = null;
    private Connection con = null;

    public JDBConnection() {
        try {
            Class.forName(dbDriver).newInstance();
        } catch (Exception ex) {
            System.out.println("数据库加载失败");
        }
    }

    private boolean creatConnection() {
        try {
            con = DriverManager.getConnection(url, userName, password);
            con.setAutoCommit(true);
            return true;
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            return false;
        }
        
    }

    public boolean executeUpdate(String sql) {
        if (null == con) {
            creatConnection();
        }
        try {
            stmt = con.createStatement();
            stmt.executeUpdate(sql);
            return true;
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            return false;
        }
    }

    public ResultSet executeQuery(String sql) {
        try {
            if (null == con) {
                creatConnection();
            }
            stmt = con.createStatement();
            try {
                rs = stmt.executeQuery(sql);
                return rs;
            } catch (SQLException e) {
                System.out.println(e.getMessage());
                return null;
            }
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            return null;
        }    
    }

    public void closeConnection() {
        if (null != rs) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (null != stmt) {
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (null != con) {
            try {
                con.close();
            } catch (SQLException e) {
            } finally {
                con = null;
            }
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值