續上(1)了
1.撰寫公共基礎類
(1)撰寫資料庫組態檔db.properties,只要連接的是MySQL資料庫driver是固定的,url中的127.0.0.1是本地IP,也可以用localhost替換,3306是MySQL默認埠號,smbms是我們前面創建的資料庫,問號后面是設定utf8編碼,解決亂碼問題的
1 driver=com.mysql.jdbc.Driver 2 url=jdbc:mysql://127.0.0.1:3306/smbms?useUnicode=true&&characterEncoding=utf-8 3 username=root 4 password=123456
(2)撰寫資料庫的公共類,包括資料庫連接資訊的獲取,資料庫連接,公共的查詢方法,公共的增刪改方法
1 package com.xiaoma.dao;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.sql.*;
6 import java.util.Properties;
7
8 //用來操作資料庫的基類(公共類)
9 public class BaseDao {
10 private static String url;
11 private static String username;
12 private static String password;
13
14 //靜態代碼塊,類啟動的時候就會加載
15 static {
16 //通過類名.class就獲取了該類的反射物件,就可以通過類加載器去讀取資源,然后通過getResourceAsStream將一個資源變成流
17 //說白了就是讓這個類可以讀取到db.properties這個檔案
18 InputStream is = BaseDao.class.getClassLoader().getResourceAsStream("db.properties");
19
20 //有了這個流后,就可以通過properti物件將檔案里的具體內容搞出來
21 Properties properties=new Properties();
22 try {
23 properties.load(is);
24 } catch (IOException e) {
25 e.printStackTrace();
26 }
27
28 //使用實體化后的properties物件獲取內容
29 url=properties.getProperty("url");
30 username=properties.getProperty("username");
31 password=properties.getProperty("password");
32 }
33
34 //獲取資料庫連接
35 public static Connection getConnection() throws Exception {
36 Connection connection=null;
37 //注冊驅動(因為我的環境版本問題,沒法用那個forname,這里就用這一行代碼去替代,同樣的效果)
38 com.mysql.jdbc.Driver driver=new com.mysql.jdbc.Driver();
39 //獲取資料庫連接物件
40 connection= DriverManager.getConnection(url,username,password);
41 //將連接物件回傳
42 return connection;
43 }
44
45 //撰寫查詢公共方法
46 //因為我們不曉得sql有多少個引數,是啥型別嘞,所以弄一個Object型別的陣列來暫存這個引數,然后通過回圈去遍歷
47 public static ResultSet execute(Connection connection,String sql,Object[] params,ResultSet resultSet,PreparedStatement preparedStatement) throws Exception {
48 //通過preparedStatement預編譯一個sql陳述句
49 connection.prepareStatement(sql);
50
51 //遍歷引數
52 for (int i = 1; i < params.length; i++) {
53 preparedStatement.setObject(i+1,params[i]);
54 }
55
56 //執行sql
57 resultSet = preparedStatement.executeQuery();
58
59 //回傳資料
60 return resultSet;
61 }
62
63 //撰寫增刪改公共方法(與查詢類似)
64 public static int execute(Connection connection,String sql,Object[] params,PreparedStatement preparedStatement) throws Exception {
65 //通過preparedStatement預編譯一個sql陳述句
66 connection.prepareStatement(sql);
67
68 //遍歷引數
69 for (int i = 1; i < params.length; i++) {
70 preparedStatement.setObject(i+1,params[i]);
71 }
72
73 //執行sql
74 int updataRows = preparedStatement.executeUpdate();
75
76 //回傳資料
77 return updataRows;
78 }
79
80 //釋放資源
81 public static boolean closeResourses(Connection connection,ResultSet resultSet,PreparedStatement preparedStatement){
82 boolean flag=true;
83 if (connection != null) {
84 try{
85 connection.close();
86 //如果關閉完成后還有資源存在,就讓GC回收
87 resultSet=null;
88 }catch (Exception e){
89 e.printStackTrace();
90 //如果關閉出現了錯誤,就讓flag=false
91 flag=false;
92 }
93 }
94 if (resultSet != null) {
95 try{
96 resultSet.close();
97 //如果關閉完成后還有資源存在,就讓GC回收
98 resultSet=null;
99 }catch (Exception e){
100 e.printStackTrace();
101 //如果關閉出現了錯誤,就讓flag=false
102 flag=false;
103 }
104 }
105 if (preparedStatement != null) {
106 try{
107 preparedStatement.close();
108 //如果關閉完成后還有資源存在,就讓GC回收
109 resultSet=null;
110 }catch (Exception e){
111 e.printStackTrace();
112 //如果關閉出現了錯誤,就讓flag=false
113 flag=false;
114 }
115 }
116 return flag;
117 }
118 }
2.撰寫字符編碼過濾器
1 package com.xiaoma.filter;
2
3 import javax.servlet.*;
4 import java.io.IOException;
5
6 public class CharacterEncodingFilter implements Filter {
7 @Override
8 public void init(FilterConfig filterConfig) throws ServletException {
9
10 }
11
12 @Override
13 public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
14 servletRequest.setCharacterEncoding("utf-8");
15 servletResponse.setCharacterEncoding("utf-8");
16
17 filterChain.doFilter(servletRequest,servletResponse);
18 }
19
20 @Override
21 public void destroy() {
22
23 }
24 }
3.在web.xml檔案內注冊過濾器
1 <!--注冊字符編碼過濾器--> 2 <filter> 3 <filter-name>CharacterEncodingFilter</filter-name> 4 <filter-class>com.xiaoma.filter.CharacterEncodingFilter</filter-class> 5 </filter> 6 <filter-mapping> 7 <filter-name>CharacterEncodingFilter</filter-name> 8 <url-pattern>/*</url-pattern> 9 </filter-mapping>
4.將靜態資源匯入到專案的webapp目錄下,與WEB-INF目錄同級別,至此準備作業完畢
靜態資源鏈接
鏈接:https://pan.baidu.com/s/1IDz2EPuBlmYD7lsk5G1yWA
提取碼:9999
第一階段專案結構目錄

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/319571.html
標籤:Java
下一篇:Spring框架入門
