模板一:
package js; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class JdbcUtil { private static String url="jdbc:mysql://localhost:3306/day01"; private static String user="root"; private static String password="root"; static{ try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); throw new RuntimeException(); } } /* * 获取连接 */ public static Connection getConnection(){ try { Connection conn= DriverManager.getConnection(url, user, password); return conn; } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(e); } } public static void close(Connection conn,Statement smt,ResultSet rs){ if(rs!=null){ try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); throw new RuntimeException(e); } } if(smt!=null){ try { smt.close(); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(e); } } if(conn!=null){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(e); } } } }
在配置文件中读取信息:
db.properties:
url=jdbc:mysql://localhost:3306/day01 user=root password=root driverClass=com.mysql.jdbc.Driver
模板二:
package js; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; public class JdbcUtil { private static String url=null; private static String user=null; private static String password=null; private static String driverClass=null; static{ try { /* * 使用类路径的读取方式: * / :斜杠标示classpath的根目录 * 在java项目下:classpath的根目录从bin开始 * 在web 项目下:classpath的根目录从WEB-INF/classes目录开始 */ //读取db.properties文件 Properties props=new Properties(); InputStream in= JdbcUtil.class.getResourceAsStream("/db.properties"); props.load(in); url=(String) props.get("url"); user=props.getProperty("user"); password=props.getProperty("password"); driverClass=props.getProperty("driverClass"); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { Class.forName(driverClass); } catch (ClassNotFoundException e) { e.printStackTrace(); throw new RuntimeException(); } } /* * 获取连接 */ public static Connection getConnection(){ try { Connection conn= DriverManager.getConnection(url, user, password); return conn; } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(e); } } /* * 关闭连接 */ public static void close(Connection conn,Statement smt,ResultSet rs){ if(rs!=null){ try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); throw new RuntimeException(e); } } if(smt!=null){ try { smt.close(); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(e); } } if(conn!=null){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(e); } } } }