zoukankan      html  css  js  c++  java
  • JDBCTool

    新建 *.properties属性文件,内容如下:

    driver=com.mysql.jdbc.Driver

    url=jdbc:mysql://localhost:3306/dbName

    username=root

    password=root

    public class DbTool {
    private static String db_driver=null;
    private static String db_url=null;
    private static String db_user=null;
    private static String db_password=null;
    public static Connection getConnection() throws ClassNotFoundException, IOException{
    Connection conn=null;
    InputStream in=DbTool.class.getClassLoader().getResourceAsStream("jdbc.properties");
    Properties properties=new Properties();
    properties.load(in);
    db_driver=properties.getProperty("driver");
    db_url=properties.getProperty("url");
    db_user=properties.getProperty("user");
    db_password=properties.getProperty("password");
    try {
    Class.forName(db_driver);
    conn=DriverManager.getConnection(db_url,db_user,db_password);
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    // TODO Auto-generated catch block
    // TODO Auto-generated catch block
    return conn;
    }

    public static void close(ResultSet rs){
    if(rs!=null){
    try {
    rs.close();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
    public static void close(PreparedStatement prst){
    if(prst!=null){
    try {
    prst.close();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
    public static void close(Connection conn){
    if(conn!=null){
    try {
    conn.close();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
    public static void close(PreparedStatement prst,Connection conn){
    close(prst);
    close(conn);
    }
    public static void close(ResultSet rs,PreparedStatement prst,Connection conn){

    close(rs);
    close(prst);
    close(conn);



    }

    }

  • 相关阅读:
    CSUST 8.4 早训
    CSUST 8.5 早训
    hdu1542 Atlantis 线段树--扫描线求面积并
    hdu1540 Tunnel Warfare 线段树/树状数组
    hdu1535 Invitation Cards 最短路
    hdu1358 Period KMP
    SQL Server 向数据库中创建表并添加数据
    初次实践数据库--SQL Server2016
    hdu1301 Jungle Roads 最小生成树
    hdu1281 棋盘游戏 二分图最大匹配
  • 原文地址:https://www.cnblogs.com/cn-chy-com/p/7467488.html
Copyright © 2011-2022 走看看