时间:2021-07-01 10:21:17 帮助过:2人阅读
public void test1() throws Exception{ InputStream in = new FileInputStream("F:\\workspace\\android_web05\\src\\jdbc.properties"); Properties p = new Properties(); p.load(in); System.out.println(p.get("driver")); System.out.println(p.get("url")); System.out.println(p.get("username")); System.out.println(p.get("password"));}//由于路径在服务器端会时时变化,所以使用类的加载器动态获取路径public void test2() throws Exception{ //类加载器:加载的是类路径的代码(.class字节码文件所在的目录) String path = ReadFileDemo.class.getClassLoader().getResource("jdbc.properties").getPath(); System.out.println(path); InputStream in = new FileInputStream(path); Properties p = new Properties(); p.load(in); System.out.println(p.get("driver")); System.out.println(p.get("url")); System.out.println(p.get("username")); System.out.println(p.get("password"));} //这种方式和第2中方式相似,不过是直接通过类的加载器获得文件的输入流。public void test3() throws Exception{ //类加载器:加载的是类路径的代码(.class字节码文件所在的目录) InputStream in = ReadFileDemo.class.getClassLoader().getResourceAsStream("jdbc.properties"); Properties p = new Properties(); p.load(in); System.out.println(p.get("driver")); System.out.println(p.get("url")); System.out.println(p.get("username")); System.out.println(p.get("password"));} //通过创建ResourceBundle对象来读取配置文件,ResourceBundle底层也是封装了类的加载器专门读取配置文件使用,开发者一般使用这种方法,最为简单。public void test4() throws Exception{ ResourceBundle rb = ResourceBundle.getBundle("jdbc");//这里直接写文件名,不用加扩展名 System.out.println(rb.getString("driver")); System.out.println(rb.getString("username")); System.out.println(rb.getString("password")); System.out.println(rb.getString("url")); }注意:需要判断连接是否为空,同时在finally里,把上一个try块中的对象置为空(null),已经本次try块中的对象关闭。
public static void close(Connection conn,Statement stmt,ResultSet rs){try {if(rs!=null){rs.close();}} catch (SQLException e) {e.printStackTrace();} finally{rs = null;try {if(stmt!=null){stmt.close();}} catch (SQLException e) {e.printStackTrace();} finally{stmt = null;try {if(conn!=null){conn.close();}} catch (SQLException e) {e.printStackTrace();} finally{conn = null;}}}}
import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.ResourceBundle;public class JDBCUtils {private static String driver = null;private static String url = null;private static String username = null;private static String password = null;static{ResourceBundle rb = ResourceBundle.getBundle("jdbc");driver = rb.getString("driver");url = rb.getString("url");username = rb.getString("username");password = rb.getString("password");}static{try {Class.forName(driver);} catch (ClassNotFoundException e) {e.printStackTrace();}}public static Connection getConnection() throws Exception{Connection conn = DriverManager.getConnection(url, username, password);return conn;}public static void close(Connection conn,Statement stmt,ResultSet rs){try {if(rs!=null){rs.close();}} catch (SQLException e) {e.printStackTrace();} finally{rs = null;try {if(stmt!=null){stmt.close();}} catch (SQLException e) {e.printStackTrace();} finally{stmt = null;try {if(conn!=null){conn.close();}} catch (SQLException e) {e.printStackTrace();} finally{conn = null;}}}}}
JDBC(数据库的使用优化)
标签: