資料庫相關(關于資料庫連接的方法應該定義為靜態方法):
加載驅動:
static {
// 加載驅動
try {
Class.forName("com.mysql.cj.jdbc.Driver"); //MySQL8更換了新的驅動包名
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
獲取驅動連接:
public static Connection getConn() {
Connection conn = null;
try {
conn = DriverManager
.getConnection(
"jdbc:mysql://localhost:3306/eshop?setUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC",
"root", "123456");
//MySQL8需要添加useSSL和serverTimezone引數
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
關閉資料庫連接:
public static void closeall(ResultSet rs, PreparedStatement ps,
Connection conn) {
try {
//關閉結果集
if (rs != null) {
rs.close();
}
//關閉預處理陳述句
if (ps != null) {
ps.close();
}
//關閉資料庫連接
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
執行有副作用的SQL陳述句:
//定義陳述句
public static int insert(ESHOP_USER user) {
String sql = "insert into eshop_user values(?,?,?,?,DATE_FORMAT(?,'%Y-%m-%d'),?,?,?,?,?)"; //?為占位符 DATE_FORMAT函式可以格式化時間字串為datetime格式
Object[] params = { user.getUSER_ID(), user.getUSER_NAME(),
user.getUSER_PASSWORD(), user.getUSER_SEX(),
user.getUSER_BIRTHDAY(), user.getUSER_IDENITY_CODE(),
user.getUSER_EMAIL(), user.getUSER_MOBILE(),
user.getUSER_ADDRESS(), user.getUSER_STATUS() };
return exec(sql, params);
}
//執行陳述句
//不直接執行SQL陳述句來防止SQL注入
public static int exec(String sql, Object[] params) {
int count = 0;//計數影響的條目
Connection conn = Basedao.getConn(); //獲取SQL連接
PreparedStatement ps = null; //定義SQL預處理陳述句
try {
ps = conn.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
ps.setObject(i + 1, params[i]);
}
count = ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
closeall(null, ps, conn); //CU類陳述句不存在結果集,故傳遞null
}
return count; //回傳受影響的條目數量
}
對預處理陳述句和結果集的常用操作函式:
ps.setObject(index,object)//將物件插入預處理陳述句的占位符中,下標是sql陳述句中占位符的序號,從1開始
ps.setString(index,String)
rs = ps.executeQuery(); //處理預處理陳述句,并保存結果集
rs.getString("statement")//從結果集中讀取相應標簽的字串型別資料
SQL常用陳述句的寫法:
"select * from eshop_user limit 4"
//從第1行資料開始檢索,檢索4條,檢索的id為1-4(如果id從1開始)
"select * from eshop_user limit 2,4"
//從第2行資料開始檢索,檢索4條,檢索的id為2-5(如果id從1開始)
"select m.*, DATE_FORMAT(m.user_birthday,'%Y-%m-%d')birthday from ESHOP_USER m where USER_ID = ?";
//將表名的別名取為m,再使用DATE_FORMAT函式格式化字串,并取別名birthday,注意別名和函式不能有空格
servlet相關:
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
//在向資料庫寫入欄位時,應該在servlet內加上這兩句確保編碼正確
String statement = request.getParameter("Statemant") //通過標簽獲取前端傳遞過來的值
response.sendRedirect("url") //重定向頁面
PrintWriter out = response.getWriter();
out.write(""); //創建輸出流并列印文本格式的資料,通常用來列印js代碼
request.setAttribute("user", user); //在請求域添加引數,便于轉發
request.getRequestDispatcher("url").forward(request, response); //轉發,可以保留剛剛添加的引數
el運算式:
//可以在jsp前端使用java代碼,獲取后端傳遞過來的資訊
${statement}
jsp雜項
<%@ include file="xxx"%>
//引入其他jsp頁面
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
//使用taglib插件
//匯入專案的jar包直接拖入WEB-INF的lib目錄下即可使用
Get請求傳入的中文傳入資料庫模糊查詢無效的問題:
servlet中使用get請求獲取了中文字串的值,但在資料庫中使用like模糊查詢時找不到結果,英文則可以
在客戶端直接用sql查詢結果正常,則是在服務端出現了問題
根據百度到的資訊,在連接字串中加入了setUnicode=true&characterEncoding=UTF-8,未果
專案的編碼設定和資料庫中的編碼設定都是utf-8
最終確認是tomcat的問題,在tomcat的conf檔案夾下的conf中找到server.xml檔案
添加URIEncoding引數
<Connector port=“8080” protocol=“HTTP/1.1”
connectionTimeout=“20000”
redirectPort=“8443” URIEncoding=“UTF-8”/>
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/240749.html
標籤:Java
