java web複習 day13,14(數據庫sql語句和jdbc)

    來到了一個新環境工作,感覺還不錯,每天還有時間學習,這兩天主要學習了jdbc和sql語句,由於這方面知識沒什麼特別的,我主要說一下mysql8.0的新的操作和一些注意事項:

    1.//Mon Apr 04 15:43:00 CST 2016 WARN: Establishing SSL connection without server‘s identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn‘t set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to ‘false‘. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
//這是警告不是錯誤,以後使用是不影響的。大概的意思就是說建立ssl連接,但是服務器沒有身份認證,這種方式不推薦使用。

加上?useUnicode=true&characterEncoding=utf-8&useSSL=false可以解決

    2.//解決java.sql.SQLException: The server time zone value ‘XXXXXX’ is unrecognized or represents more than one time zone.

數據庫和系統的時區差異所造成的在jdbc鏈接的url後加上serverTimezone=GMT   就OK

    3. 最後數據庫url就長這樣:String url = "jdbc:mysql://localhost:3306/day14?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT";

    4.爲了防止sql注入,我們通常使用prepareStatement,進行預編譯。

    5.在操作數據庫時常常會有異常拋出,於是我們應自定義一個異常類:

public class DaoException extends RuntimeException {

	public DaoException() {
		// TODO Auto-generated constructor stub
	}

	public DaoException(String message) {
		super(message);
		// TODO Auto-generated constructor stub
	}

	public DaoException(Throwable cause) {
		super(cause);
		// TODO Auto-generated constructor stub
	}

	public DaoException(String message, Throwable cause) {
		super(message, cause);
		// TODO Auto-generated constructor stub
	}

}

6.爲了降低各層耦合性,通常從全局變量中獲取網頁多選項。

7.爲了避免重複代碼,通常編寫jdbcUtils來獲取鏈接:

package cn.itcast.utils;

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 JdbcUtils {
	
	private static String url = null;
	private static String username = null;
	private static String password = null;
	//爲了只加載一次驅動,放在靜態代碼塊中編寫,
	static{
		try{
			InputStream in = JdbcUtils.class.getClassLoader().getResourceAsStream("db.properties");
			Properties prop = new Properties();
			prop.load(in);
                        //從資源文件中獲取鏈接什麼數據庫,以及賬號密碼
                      String driver = prop.getProperty("driver");
		      url = prop.getProperty("url");
		      username = prop.getProperty("username");
		      password = prop.getProperty("password");
			
			Class.forName(driver);
			
		}catch (Exception e) {
			throw new ExceptionInInitializerError(e);
		}
	}

	public static Connection getConnection() throws SQLException{
		
		return DriverManager.getConnection(url, username, password);
	}
	//釋放鏈接
	public static void release(Connection conn,Statement st,ResultSet rs){
		
		
		if(rs!=null){
			try{
				rs.close();
			}catch (Exception e) {
				e.printStackTrace();
			}
			rs = null;

		}
		if(st!=null){
			try{
				st.close();
			}catch (Exception e) {
				e.printStackTrace();
			}
			
		}
		
		if(conn!=null){
			try{
				conn.close();
			}catch (Exception e) {
				e.printStackTrace();
			}
			
		}
	}
	
}

8.在提交多選項(比如說愛好之類的)通常只提交連接之後的一個長字符串,單獨就不用提交以提高系統性能,具體做法就是,每一個多選項不設置名稱。


複習本章內容主要就是做好這一次的案例,準備花兩個小時做一下,上班溜號不知道要不要緊,whatever。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章