數(shù)據(jù)庫相關(guān)(關(guān)于數(shù)據(jù)庫連接的方法應(yīng)該定義為靜態(tài)方法):
加載驅(qū)動(dòng):
static {
// 加載驅(qū)動(dòng)
try {
Class.forName("com.mysql.cj.jdbc.Driver"); //MySQL8更換了新的驅(qū)動(dòng)包名
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
獲取驅(qū)動(dòng)連接:
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參數(shù)
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
關(guān)閉數(shù)據(jù)庫連接:
public static void closeall(ResultSet rs, PreparedStatement ps,
Connection conn) {
try {
//關(guān)閉結(jié)果集
if (rs != null) {
rs.close();
}
//關(guān)閉預(yù)處理語句
if (ps != null) {
ps.close();
}
//關(guān)閉數(shù)據(jù)庫連接
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
執(zhí)行有副作用的SQL語句:
//定義語句
public static int insert(ESHOP_USER user) {
String sql = "insert into eshop_user values(?,?,?,?,DATE_FORMAT(?,'%Y-%m-%d'),?,?,?,?,?)"; //?為占位符 DATE_FORMAT函數(shù)可以格式化時(shí)間字符串為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);
}
//執(zhí)行語句
//不直接執(zhí)行SQL語句來防止SQL注入
public static int exec(String sql, Object[] params) {
int count = 0;//計(jì)數(shù)影響的條目
Connection conn = Basedao.getConn(); //獲取SQL連接
PreparedStatement ps = null; //定義SQL預(yù)處理語句
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類語句不存在結(jié)果集,故傳遞null
}
return count; //返回受影響的條目數(shù)量
}
對(duì)預(yù)處理語句和結(jié)果集的常用操作函數(shù):
ps.setObject(index,object)//將對(duì)象插入預(yù)處理語句的占位符中,下標(biāo)是sql語句中占位符的序號(hào),從1開始
ps.setString(index,String)
rs = ps.executeQuery(); //處理預(yù)處理語句,并保存結(jié)果集
rs.getString("statement")//從結(jié)果集中讀取相應(yīng)標(biāo)簽的字符串類型數(shù)據(jù)
SQL常用語句的寫法:
"select * from eshop_user limit 4"
//從第1行數(shù)據(jù)開始檢索,檢索4條。檢索的id為1-4(如果id從1開始)
"select * from eshop_user limit 2,4"
//從第2行數(shù)據(jù)開始檢索,檢索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函數(shù)格式化字符串,并取別名birthday,注意別名和函數(shù)不能有空格
servlet相關(guān):
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
//在向數(shù)據(jù)庫寫入字段時(shí),應(yīng)該在servlet內(nèi)加上這兩句確保編碼正確
String statement = request.getParameter("Statemant") //通過標(biāo)簽獲取前端傳遞過來的值
response.sendRedirect("url") //重定向頁面
PrintWriter out = response.getWriter();
out.write(""); //創(chuàng)建輸出流并打印文本格式的數(shù)據(jù),通常用來打印js代碼
request.setAttribute("user", user); //在請(qǐng)求域添加參數(shù),便于轉(zhuǎn)發(fā)
request.getRequestDispatcher("url").forward(request, response); //轉(zhuǎn)發(fā),可以保留剛剛添加的參數(shù)
el表達(dá)式:
//可以在jsp前端使用java代碼,獲取后端傳遞過來的信息
${statement}
jsp雜項(xiàng)
<%@ include file="xxx"%>
//引入其他jsp頁面
<%@ taglib prefix="c" uri="http://java./jsp/jstl/core"%>
//使用taglib插件
//導(dǎo)入項(xiàng)目的jar包直接拖入WEB-INF的lib目錄下即可使用
Get請(qǐng)求傳入的中文傳入數(shù)據(jù)庫模糊查詢無效的問題:
servlet中使用get請(qǐng)求獲取了中文字符串的值,但在數(shù)據(jù)庫中使用like模糊查詢時(shí)找不到結(jié)果,英文則可以
在客戶端直接用sql查詢結(jié)果正常,則是在服務(wù)端出現(xiàn)了問題
根據(jù)百度到的信息,在連接字符串中加入了setUnicode=true&characterEncoding=UTF-8 ,未果
項(xiàng)目的編碼設(shè)置和數(shù)據(jù)庫中的編碼設(shè)置都是utf-8
最終確認(rèn)是tomcat的問題,在tomcat的conf文件夾下的conf中找到server.xml文件
添加URIEncoding參數(shù)
<Connector port=“8080” protocol=“HTTP/1.1”
connectionTimeout=“20000”
redirectPort=“8443” URIEncoding=“UTF-8”/>
|