import java.io.IOException; import java.io.InputStream; import java.sql.*; import java.util.Properties; public class JDBCUtil { static private String driver=null; static private String url=null; static private String username=null; static private String password=null; static InputStream ins; static { try{ Properties props=new Properties(); //这里是你配置的地址 ins=JDBCUtil.class.getResourceAsStream("/database.properties"); props.load(ins); driver=props.getProperty("driver"); url=props.getProperty("url"); username=props.getProperty("username"); password=props.getProperty("password"); Class.forName(driver); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { try { ins.close(); } catch (IOException e) { e.printStackTrace(); } } } //这里是JDBC读取的配置工具 public static Connection getConn() throws SQLException{ return DriverManager.getConnection(url,username,password); } private JDBCUtil() { } //这里是异常的的工具 public static void close(ResultSet rs, Statement statement, Connection connection){ try { if(statement!=null){ statement.close(); } } catch (SQLException e) { e.printStackTrace(); } finally { try { if (connection!=null) { connection.close(); } } catch (SQLException e) { e.printStackTrace(); }finally { try { if (rs != null) { rs.close(); } } catch (SQLException e) { e.printStackTrace(); } } } } public static void close( Statement statement, Connection connection){ try { if(statement!=null){ statement.close(); } } catch (SQLException e) { e.printStackTrace(); } finally { try { if (connection!=null) { connection.close(); } } catch (SQLException e) { e.printStackTrace(); } } } } //------------------------------------------------------------------------------------
这里是database.properties配置文件的内容
driver=com.mysql.cj.jdbc.Driver url=jdbc:mysql://localhost:3306/ username=root password=123456