package Utils; import java.io.FileInputStream; import java.io.IOException; import java.sql.*; import java.util.Properties; public class Utils { private static String user; private static String pwd; private static String driver; private static String url; /* *注意配置文件的位置和名称,同时注意配置文件key名同步 * */ static { try { Properties properties = new Properties(); properties.load(new FileInputStream("src\\mysql.properties")); user = properties.getProperty("user"); pwd = properties.getProperty("password"); driver = properties.getProperty("driver"); url = properties.getProperty("url"); } catch (IOException e) { throw new RuntimeException(e); } }
/* *msyql数据库的连接方法 * */ public static Connection connection(){ try { Class.forName(driver); return DriverManager.getConnection(url,user,pwd); } catch (ClassNotFoundException | SQLException e) { throw new RuntimeException(e); } }
/*1.Statement是PreparedStatement的父接口 * 2.这个方法是关闭数据库连接的方法 * */ public static void close(ResultSet set,Statement statement,Connection connection){ try { if(set!=null){ set.close(); } if (statement!=null){ statement.close(); } if(connection!=null){ connection.close(); } } catch (SQLException e) { throw new RuntimeException(e); } } }