zoukankan      html  css  js  c++  java
  • jdbc-------JDBCUtil类 工具类

    jdbcutil 主要处理的是 连接数据库, 和关闭各个流

    1, 数据库连接的配置信息: mysql.properties (在工程的目录下)个人配置

    url=jdbc:mysql://localhost:3306/test
    driver=com.mysql.jdbc.Driver
    username=root
    password=123

    2, 获取连接

    读取配置信息,加载驱动。连接。(这个在后面的例子常用到)

    
    

    package com.ljs.util;

    import java.io.File;
    import java.io.FileInputStream;

    
    

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.Properties;


    public
    class JDBCUtil { private static String url; private static String user; private static String password; private static String driver; static{ try { Properties properties = new Properties(); FileInputStream fis = new FileInputStream(new File("mysql.properties")); properties.load(fis); url = properties.getProperty("url"); user = properties.getProperty("username"); password = properties.getProperty("password"); driver = properties.getProperty("driver"); Class.forName(driver); } catch (Exception e) { e.getMessage(); } } public static Connection getConn() throws Exception{ Connection connection = DriverManager.getConnection(url, user, password); return connection; } public static void close(ResultSet resultSet, PreparedStatement preparedStatement, Connection connection){ try { if (resultSet != null) { resultSet.close(); } if(preparedStatement != null ){ preparedStatement.close(); } if(connection != null ){ connection.close(); } } catch (SQLException e) { throw new RuntimeException(); } } }
  • 相关阅读:
    Spring Security和Swagger2集成报错
    [转] SpringBoot返回json 数据以及数据封装
    ElasticSearch问题总结
    Linux基础命令
    Jenkins总结3-shell脚本
    Jenkins总结2-部署maven项目
    SpringCloud启动异常Stopping service [Tomcat]
    索引使用场景
    flask app.config
    python验证企业统一信用码
  • 原文地址:https://www.cnblogs.com/lijins/p/10122063.html
Copyright © 2011-2022 走看看