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(); } } }
  • 相关阅读:
    hadoopfs: 未找到命令...
    WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
    centos 7 安装音乐播放器(亲测可用)(转载)
    Linux 脚本编写基础
    Zip加密
    Qt嵌入cef3关闭窗口时崩溃的问题
    C++11多线程基础
    C++11多线程(std::atomic)
    C++11多线程(thread_local)
    VS 新建RelWithDebInfo模式
  • 原文地址:https://www.cnblogs.com/lijins/p/10122063.html
Copyright © 2011-2022 走看看