码迷,mamicode.com
首页 > 数据库 > 详细

数据库连接方式比较

时间:2016-11-15 23:12:32      阅读:335      评论:0      收藏:0      [点我收藏+]

标签:ret   use   try   username   数据库   soft   cal   generated   void   

 

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

 

数据库连接方式比较

标签:ret   use   try   username   数据库   soft   cal   generated   void   

原文地址:http://www.cnblogs.com/haimishasha/p/6067541.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!