• Java-JDBC.mysql 工具类 读取本地文件配置


    引用 mysql-connector-jav 的jar 

    配置文件为  database.propertties .  格式如下

    1 driverClass=com.mysql.jdbc.Driver
    2 url=jdbc:mysql://localhost/mydatabase?useUnicode=true&characterEncoding=GBK
    3 username=root
    4 password=***

    以下是代码

     1 package JDBCDemo;
     2 
     3 import java.io.FileInputStream;
     4 import java.io.FileNotFoundException;
     5 import java.io.IOException;
     6 import java.io.InputStream;
     7 import java.sql.DriverManager;
     8 import java.sql.PreparedStatement;
     9 import java.sql.ResultSet;
    10 import java.sql.SQLException;
    11 import java.util.InputMismatchException;
    12 import java.util.Properties;
    13 
    14 import com.mysql.jdbc.Connection;
    15 import com.mysql.jdbc.Statement;
    16 
    17 public class JDBCUtils {
    18     private void JDBCUtils() {};
    19     private  static String sqlurl = ""; 
    20     private  static String sqluser = "";
    21     private  static String sqlpwd = "";
    22     private  static String classforName = "";
    23     private static  Connection con = null;
    24     
    25     static {
    26         try {
    27             initConfig();
    28             Class.forName(classforName);
    29             con = (Connection) DriverManager.getConnection(sqlurl, sqluser, sqlpwd);
    30         } catch (ClassNotFoundException e) {
    31             System.out.println("数据驱动失败");
    32             e.printStackTrace();
    33         } catch (SQLException e) {
    34             
    35             System.out.println("数据连接失败");
    36             e.printStackTrace();
    37         } 
    38     }
    39     
    40     private static void initConfig() {
    41         InputStream inf = JDBCUtils.class.getClassLoader().getResourceAsStream("database.propertties");
    42         Properties properties = new Properties();
    43         try {
    44             properties.load(inf);
    45             classforName = properties.getProperty("driverClass");
    46             sqlurl = properties.getProperty("url");
    47             sqluser = properties.getProperty("username");
    48             sqlpwd = properties.getProperty("password");
    49         } catch (IOException e) {
    50             System.out.println("读取配置文件失败");
    51         }
    52     }
    53     
    54     public static Connection getConnection() {
    55         return con;
    56     }
    57     public static void close(Connection con,Statement preparedStatement,ResultSet rs)   {
    58         if(con !=null) {
    59             try {
    60                 con.close();
    61             } catch (SQLException e) {
    62                 //e.printStackTrace();
    63             }
    64             
    65         }
    66         if (preparedStatement!=null) {
    67             try {
    68                 preparedStatement.close();
    69             } catch (Exception e) {
    70                 //e.printStackTrace();
    71             }
    72         }
    73         if (rs!=null) {
    74             try {
    75                 rs.close();
    76             } catch (Exception e) {
    77                 //e.printStackTrace();
    78             }
    79         }
    80     }
    81 }
  • 相关阅读:
    矢量瓦片切割工具,注意不是切图工具哦
    openlayers模仿google地图--地图版权随鹰眼关闭打开而改变位置
    centos建立本地yum源shell脚本
    python通用序列操作
    awk手册
    linux启动级别简单说明
    win8程序开机自启动管理
    linux系统监控shell脚本
    shell脚本实现源码lamp自动化安装
    python实现冒泡排序
  • 原文地址:https://www.cnblogs.com/wwcherish/p/8037395.html
Copyright © 2020-2023  润新知