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(); } } }