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(); } } }
  • 相关阅读:
    进程和线程
    进程通信、同步与调度
    文件和文件系统
    【nexys3】【verilog】小设计——拆弹游戏
    Qt4开发环境搭建(Qt4.8.7+mingw4.8.2+Qt Creator4.2.0)
    GPL和LGPL
    【rpi】使用putty远程连接rpi(ssh)
    mysql 命令 小结
    安装mysql zip 安装包 Navicat连接
    python虚拟环境 virtualenv工具
  • 原文地址:https://www.cnblogs.com/lijins/p/10122063.html
Copyright © 2011-2022 走看看