• Java使用JDBC连接数据库


    不同数据库需要去官网上面下载对应的驱动jar ,例如MySQL https://dev.mysql.com/downloads/connector/j/

      properties文件

     1 #MySQL
     2 mysqlDriver:com.mysql.jdbc.Driver
     3 mysqlUrl:jdbc:mysql://localhost:3306/test
     4 mysqlUser:root
     5 mysqlPassword:root
     6 
     7 #Oracle
     8 oracleDriver:oracle.jdbc.driver.OracleDriver
     9 oracleUrl:jdbc:oracle:thin@localhost:1521:orcl
    10 oracleUser:username
    11 oraclePassword:password
     1 package com.CommonUtil;
     2 
     3 /**
     4  * JDBC工具类
     5  * @author Andrew
     6  */
     7 
     8 import java.io.IOException;
     9 import java.sql.Connection;
    10 import java.sql.DriverManager;
    11 import java.sql.ResultSet;
    12 import java.sql.SQLException;
    13 import java.sql.Statement;
    14 import java.util.Properties;
    15 
    16 public class JDBCUtil {
    17     //可以帮助读取和处理资源文件中的信息
    18     private static Properties pros = null;        
    19     //静态代码块,加载config.properties文件
    20     static {
    21         pros = new Properties();
    22         try {
    23             pros.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("./com/config/config.properties"));
    24         } catch (IOException e) {
    25             e.printStackTrace();
    26         }
    27     }
    28     
    29     /**
    30      * 建立MySQL数据库连接
    31      * @return    java.sql.Connection连接对象
    32      * @throws ClassNotFoundException
    33      * @throws SQLException
    34      */
    35     public static Connection getMySQLConn() throws ClassNotFoundException, SQLException {
    36         Class.forName(pros.getProperty("mysqlDriver"));
    37         return DriverManager.getConnection(pros.getProperty("mysqlUrl"), pros.getProperty("mysqlUser"), 
    38                 pros.getProperty("mysqlPassword"));
    39     }
    40 
    41     /**
    42      * 建立Oracle数据库连接
    43      * @return    java.sql.Connection连接对象
    44      * @throws ClassNotFoundException
    45      * @throws SQLException
    46      */
    47     public static Connection getOracleConn() throws ClassNotFoundException, SQLException {
    48         Class.forName(pros.getProperty("oracleDriver"));
    49         return DriverManager.getConnection(pros.getProperty("oracleUrl"), pros.getProperty("oracleUser"), 
    50                 pros.getProperty("oraclePassword"));
    51     }
    52     
    53     /**
    54      * 关闭所有的数据库连接
    55      * @param rs    ResultSet对象
    56      * @param stmt    Statement对象
    57      * @param conn    Connection对象
    58      * @throws SQLException
    59      */
    60     public void closeAll(ResultSet rs, Statement stmt, Connection conn) throws SQLException {
    61         if (rs != null)            
    62             rs.close();    
    63         if (stmt != null)         
    64             stmt.close();    
    65         if (conn != null)         
    66             conn.close();    
    67     }
    68 
    69     //关闭ResultSet结果集
    70     public void close(ResultSet rs) throws SQLException {
    71         if (rs != null) {
    72             rs.close();
    73         }
    74     }
    75 
    76     //关闭Statement
    77     public void close(Statement stmt) throws SQLException {
    78         if (stmt != null) {
    79             stmt.close();
    80         }
    81     }
    82     
    83     //关闭数据库Connection连接对象
    84     public void close(Connection conn) throws SQLException {
    85             if (conn != null) {
    86                 conn.close();
    87             }
    88     }
    89 }
    兴趣是最好的老师,知识改变格局,转载请注明出处!
  • 相关阅读:
    阻止a链接跳转的点击事件
    appium python版api
    Appium—python_ 安卓手机划屏幕操作
    appium-unittest框架中的断言
    Appium 服务关键字
    python mysql入库的时候字符转义
    python实现两个字典合并
    解决linux登录后总是时间过会就断开(解决ssh登录后闲置时间过长而断开连接)
    linux安装好redis,如何在window端访问?
    linux上安装redis
  • 原文地址:https://www.cnblogs.com/andrew209/p/9004969.html
Copyright © 2020-2023  润新知