之前的代碼中,以下代碼很多時候並不是固定的:
1 private static String url = "jdbc:mysql://localhost:3306/day1029?useUnicode=true&characterEncoding=GB2312"; 2 // 數據庫的用戶名和密碼 3 private static String user = "root"; 4 private static String password = "root"; 5 Class.forName("com.mysql.jdbc.Driver");
這時候,我們可以在該類的包的src目錄下創建一個bd.properties文件,用來存儲這些信息:
url=jdbc:mysql://localhost:3306/day17 user=root password=root driverClass=com.mysql.jdbc.Driver
如果是java項目讀取該文件的這些信息的話,可以這樣讀取:
FileInputStream in = new FileInputStream("./src/db.properties");
但是在Web項目中不能這樣讀取,因為:
. 代表java命令運行的目錄
* 在java項目下,. java命令的運行目錄從項目的根目錄開始
* 在web項目下, . java命令的而運行目錄從tomcat/bin目錄開始
* 所以不能使用點.
那么,我們該如何讀取,才能讓java和Web項目都能讀取呢?
這時候就用到了類路徑讀取
InputStream in = JdbcUtil.class.getResourceAsStream("/db.properties");
原因:
使用類路徑的讀取方式
* / : 斜杠表示classpath的根目錄
* 在java項目下,classpath的根目錄從bin目錄開始
* 在web項目下,classpath的根目錄從WEB-INF/classes目錄開始
java項目中只要更改: 1)第一個代碼塊,把它們的值都改為null
2)增加類路徑讀取即可。
下面是Web項目的代碼:
1)jdbc工具類:
1 /** 2 * jdbc工具類 3 * @author APPle 4 * 5 */ 6 public class JdbcUtil { 7 private static String url = null; 8 private static String user = null; 9 private static String password = null; 10 private static String driverClass = null; 11 12 /** 13 * 靜態代碼塊中(只加載一次) 14 */ 15 static{ 16 try { 17 //讀取db.properties文件 18 Properties props = new Properties(); 19 /** 20 * . 代表java命令運行的目錄 21 * 在java項目下,. java命令的運行目錄從項目的根目錄開始 22 * 在web項目下, . java命令的而運行目錄從tomcat/bin目錄開始 23 * 所以不能使用點. 24 */ 25 //FileInputStream in = new FileInputStream("./src/db.properties"); 26 27 /** 28 * 使用類路徑的讀取方式 29 * / : 斜杠表示classpath的根目錄 30 * 在java項目下,classpath的根目錄從bin目錄開始 31 * 在web項目下,classpath的根目錄從WEB-INF/classes目錄開始 32 */ 33 InputStream in = JdbcUtil.class.getResourceAsStream("/db.properties"); 34 35 //加載文件 36 props.load(in); 37 //讀取信息 38 url = props.getProperty("url"); 39 user = props.getProperty("user"); 40 password = props.getProperty("password"); 41 driverClass = props.getProperty("driverClass"); 42 43 44 //注冊驅動程序 45 Class.forName(driverClass); 46 } catch (Exception e) { 47 e.printStackTrace(); 48 System.out.println("驅程程序注冊出錯"); 49 } 50 } 51 52 /** 53 * 抽取獲取連接對象的方法 54 */ 55 public static Connection getConnection(){ 56 try { 57 Connection conn = DriverManager.getConnection(url, user, password); 58 return conn; 59 } catch (SQLException e) { 60 e.printStackTrace(); 61 throw new RuntimeException(e); 62 } 63 } 64 65 66 /** 67 * 釋放資源的方法 68 */ 69 public static void close(Connection conn,Statement stmt){ 70 if(stmt!=null){ 71 try { 72 stmt.close(); 73 } catch (SQLException e) { 74 e.printStackTrace(); 75 throw new RuntimeException(e); 76 } 77 } 78 if(conn!=null){ 79 try { 80 conn.close(); 81 } catch (SQLException e) { 82 e.printStackTrace(); 83 throw new RuntimeException(e); 84 } 85 } 86 } 87 88 public static void close(Connection conn,Statement stmt,ResultSet rs){ 89 if(rs!=null) 90 try { 91 rs.close(); 92 } catch (SQLException e1) { 93 e1.printStackTrace(); 94 throw new RuntimeException(e1); 95 } 96 if(stmt!=null){ 97 try { 98 stmt.close(); 99 } catch (SQLException e) { 100 e.printStackTrace(); 101 throw new RuntimeException(e); 102 } 103 } 104 if(conn!=null){ 105 try { 106 conn.close(); 107 } catch (SQLException e) { 108 e.printStackTrace(); 109 throw new RuntimeException(e); 110 } 111 } 112 } 113 }
然后是web登陸連接sql的類:
1 public class TestPath extends HttpServlet { 2 3 public void doGet(HttpServletRequest request, HttpServletResponse response) 4 throws ServletException, IOException { 5 //連接數據庫 6 //獲取連接 7 Connection conn = JdbcUtil.getConnection(); 8 System.out.println(conn); 9 } 10 11 public void doPost(HttpServletRequest request, HttpServletResponse response) 12 throws ServletException, IOException { 13 doGet(request, response); 14 } 15 16 }