當前位置:首頁 » 編程語言 » javasql轉義

javasql轉義

發布時間: 2023-12-29 18:24:15

java轉義字元postgresql資料庫

使用預處理.估計%應該用轉義字元..用預處理安全防止sql注入..具體方法如下:

//conn = DBUtil.getInstance().getConnection();這個是獲取conn的一個方法
//DBUtil.getInstance().close(pstmt, conn);這個是我關閉pstmt conn的方法
//關鍵就是使用PreparedStatement 這個預處理。要導入包import java.sql.PreparedStatement;
public void addStudent(BufferedReader br){
String name = null;
String sex = null;
String age = null;
String id = creatId();
Connection conn =null;
PreparedStatement pstmt = null;
System.out.println("請輸入學生相應的信息:");
try {
//從鍵盤輸入學生名稱
System.out.println("請您輸入學生姓名 :");
name = br.readLine();
//從鍵盤輸入性別
System.out.println("請您輸入學生性別 :");
sex = br.readLine();
//從鍵盤輸入年齡
System.out.println("請您輸入學生年齡 :");
age = br.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// System.out.println(name + sex + age);
// insertStudent(name,sex,age);
conn = DBUtil.getInstance().getConnection();

//通過輸出判斷conn是否正確

// String sql = "insert into t_student(id,name,sex,age) values(111,"+"'"+name+"','"+sex+"',"+age+")";
String sql = "insert into t_student(id,name,sex,age) values(?,?,?,?)";
try {
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, id);
pstmt.setString(2, name);
pstmt.setString(3, sex);
pstmt.setString(4, age);
pstmt.execute();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

DBUtil.getInstance().close(pstmt, conn);

System.out.println("記錄插入成功");

}

如果還有疑問聯系我:qq 6 0 3 051135

熱點內容
win7svn伺服器搭建 發布:2025-09-17 21:01:03 瀏覽:902
python寫shell腳本 發布:2025-09-17 20:50:22 瀏覽:803
數字存儲卡有用嗎 發布:2025-09-17 20:31:00 瀏覽:492
編程有用么 發布:2025-09-17 20:22:01 瀏覽:161
ftp怎麼發文件到伺服器 發布:2025-09-17 20:12:14 瀏覽:145
怎麼設置筆記本的密碼怎麼設置密碼 發布:2025-09-17 20:12:12 瀏覽:318
foxmail上傳附件失敗 發布:2025-09-17 20:03:54 瀏覽:361
128伺服器是什麼意思 發布:2025-09-17 19:49:54 瀏覽:611
yum安裝phpfpm 發布:2025-09-17 19:48:49 瀏覽:574
斗羅大陸我的世界伺服器游戲 發布:2025-09-17 19:46:14 瀏覽:24