zoukankan      html  css  js  c++  java
  • 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 }
    兴趣是最好的老师,知识改变格局,转载请注明出处!
  • 相关阅读:
    《ElasticSearch6.x实战教程》之准备工作、基本术语
    《ElasticSearch6.x实战教程》正式推出
    【好书推荐】《剑指Offer》之硬技能(编程题12~16)
    synchronized凭什么锁得住?
    synchronized到底锁住的是谁?
    Java面试宝典(2020版)
    50道Redis面试题及答案整理,史上最全!
    MySQL面试题及答案整理,史上最全!
    Github 上优秀的 Java 项目推荐
    100道MySQL常见面试题总结
  • 原文地址:https://www.cnblogs.com/andrew209/p/9004969.html
Copyright © 2011-2022 走看看