JDBC 连接Oracle 数据库,JDBC 连接Mysql 数据库

Posted 消失的那两年

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JDBC 连接Oracle 数据库,JDBC 连接Mysql 数据库相关的知识,希望对你有一定的参考价值。

首先是JDBC 连接Oracle  数据库

   

package com.util;
import com.pojo.UserInfo;

import java.sql.*;


public class DBConnection {

    private static Connection conn;

    private static String userName = "scott";
    private static String passWord = "tiger";
    private static String url;

    static {
        url = "jdbc:oracle:thin:@localhost:1521:orcl";
    }

    public static Connection getConnection() {

        try {

            Class.forName("oracle.jdbc.OracleDriver");
            conn = DriverManager.getConnection(url, userName, passWord);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }

    //关闭资源的方法
    public static void closeResource(ResultSet rs, PreparedStatement pstmt, Connection conn) {

        try {
            if (null != rs) {
                rs.close();
            }
            if (null != pstmt) {
                pstmt.close();
            }
            if (null != conn) {
                conn.close();
            }

        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("关闭资源出现问题--");
        }

    }

    public static void main(String[] args) {

        ResultSet resultSet = null;
        PreparedStatement preparedStatement = null;
        Connection conn;
        DBConnection db = new DBConnection();
        conn = db.getConnection();
        String sql = "select * from userInfo ";
        try {
            preparedStatement = conn.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                UserInfo u = new UserInfo();
                u.setUsername(resultSet.getString("username"));
                System.out.println("用户信息: 用户名为:"+ u.getUsername());
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeResource(resultSet,preparedStatement,conn);
        }

    }
}

 

然后是JDBC 连接mysql 数据库 模板

package com.util;
import com.pojo.UserInfo;

import java.sql.*;


public class DBConnectionMySql {

    private static Connection conn;

    private static String userName = "root";
    private static String passWord = "tiger";
    private static String url;
    private static String driver;
    static {
         url = "jdbc:mysql://localhost:3306/wlc";
         driver = "com.mysql.jdbc.Driver";
    }

    public static Connection getConnection() {

        try {

            Class.forName(driver);
            conn = DriverManager.getConnection(url, userName, passWord);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }

    //关闭资源的方法
    public static void closeResource(ResultSet rs, PreparedStatement pstmt, Connection conn) {

        try {
            if (null != rs) {
                rs.close();
            }
            if (null != pstmt) {
                pstmt.close();
            }
            if (null != conn) {
                conn.close();
            }

        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("关闭资源出现问题--");
        }

    }

    public static void main(String[] args) {

        ResultSet resultSet = null;
        PreparedStatement preparedStatement = null;
        Connection conn;
        DBConnectionMySql db = new DBConnectionMySql();
        conn = db.getConnection();
        String sql = "select * from userInfo ";
        try {
            preparedStatement = conn.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                UserInfo u = new UserInfo();
                u.setUsername(resultSet.getString("username"));
                System.out.println("用户信息: 用户名为:"+ u.getUsername());
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeResource(resultSet,preparedStatement,conn);
        }

    }
}

 

以上是关于JDBC 连接Oracle 数据库,JDBC 连接Mysql 数据库的主要内容,如果未能解决你的问题,请参考以下文章

jdbc 连接oracle失败!请教原因

JDBC 连接Oracle 数据库,JDBC 连接Mysql 数据库

java使用jdbc连接oracle数据库怎么关闭连接

jdbc连接oracle数据库字符串

JDBC连接Oracle

JDBC使用JDBC连接Oracle数据库(JAVA反射机制)