zoukankan      html  css  js  c++  java
  • 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);
            }
    
        }
    }
  • 相关阅读:
    Exception in thread "main" java.lang.UnsatisfiedLinkError: xxx()V
    Python 数组比较
    远程桌面CredSSP 加密数据库修正
    (转)解决ssh登录慢的问题(一定要在远端主机上修改啊)
    让linux开机自动执行一条需要管理员的密码的命令
    ssh批量登录并执行命令(python实现)
    批量重命名文件——python实现
    配置python的eclipse开发环境
    Linux常用压缩解压命令
    强大的zsh配置文件
  • 原文地址:https://www.cnblogs.com/wanglichaoya/p/10276274.html
Copyright © 2011-2022 走看看