首页 > 代码库 > 数据库连接方式比较

数据库连接方式比较

 

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;
            }
        }
    }
}

 

数据库连接方式比较