JDBC PreparedStatement Like參數(shù)報(bào)錯(cuò)解決方案
由于我們的項(xiàng)目不大,所以剛開(kāi)始決定時(shí)我為了省事想用SSH,可是后來(lái)覺(jué)得只有Struts2好了,后來(lái)的查詢(xún)等數(shù)據(jù)庫(kù)操作我自己寫(xiě)方法不行了嘛!
剛才寫(xiě)一個(gè)公共查詢(xún)的方法,在增加參數(shù)時(shí)出了點(diǎn)錯(cuò)誤,就是使用模糊查詢(xún)時(shí)犯暈了。
我寫(xiě)的方法如下:
/**
* @說(shuō)明 執(zhí)行一條查詢(xún)SQL語(yǔ)句,可以帶參數(shù)
*/
public static List<Object[]> excuteQuery(String sql, Object[] objs) {
Connection conn = null;
PreparedStatement psta = null;
ResultSet rs = null;
List<Object[]> iResult = null;
Object[] objArr = null;
try {
conn = getConn(); // 得到鏈接
PreparedStatement state = conn.prepareStatement(sql);
if(null != objs){
for (int i = 0; i < objs.length; i++) {
state.setObject(i + 1, objs[i]);
}
}
ResultSet resultSet = state.executeQuery(); // 執(zhí)行查詢(xún),返回結(jié)果接集合
iResult = new ArrayList<Object[]>();
int count = resultSet.getMetaData().getColumnCount(); // 一共有多少列數(shù)據(jù)
while (resultSet.next()) {
objArr = new Object[count];
for (int i = 1; i <= count; i++) {
objArr[i - 1] = resultSet.getObject(i); // 增加到返回的集合中
}
iResult.add(objArr);
}
} catch (Exception e) {
e.printStackTrace();
iResult = null;
} finally {
try {
if (rs != null) {
rs.close();
}
if (psta != null) {
psta.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e2) {
}
}
return iResult;
}
后來(lái)我輸入這樣的一個(gè)參數(shù)進(jìn)行查詢(xún):
public static void main(String[] args) {
Object[] para = new Object[]{"c"};
List<Object[]> list = excuteQuery("select * from s_user t where t.userName like '%?%'",para);
for (Object[] o : list) {
for (Object ob : o) {
System.out.print(ob + "-");
}
System.out.println();
}
}
結(jié)果報(bào)錯(cuò)如下:
java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0). at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1056) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927) at com.mysql.jdbc.PreparedStatement.checkBounds(PreparedStatement.java:3279) at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3263) at com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:4087) at com.mysql.jdbc.PreparedStatement.setObject(PreparedStatement.java:3513) at org.apache.commons.dbcp.DelegatingPreparedStatement.setObject(DelegatingPreparedStatement.java:166) at com.nms.common.db.ConnectionManager.excuteQuery(ConnectionManager.java:86) at com.nms.common.db.ConnectionManager.main(ConnectionManager.java:20) Exception in thread "main" java.lang.NullPointerException at com.nms.common.db.ConnectionManager.main(ConnectionManager.java:21)
后來(lái)才知道,模糊查詢(xún)時(shí)要這樣寫(xiě):
public static void main(String[] args) {
Object[] para = new Object[]{"%c%"};
List<Object[]> list = excuteQuery("select * from s_user t where t.userName like ?",para);
for (Object[] o : list) {
for (Object ob : o) {
System.out.print(ob + "-");
}
System.out.println();
}
}
就這么簡(jiǎn)單,閑話不說(shuō)了!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java Swing JToggleButton開(kāi)關(guān)按鈕的實(shí)現(xiàn)
這篇文章主要介紹了Java Swing JToggleButton開(kāi)關(guān)按鈕的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
關(guān)于SpringBoot配置文件加載位置的優(yōu)先級(jí)
這篇文章主要介紹了關(guān)于SpringBoot配置文件加載位置的優(yōu)先級(jí),我們也可以通過(guò)spring.config.location來(lái)改變默認(rèn)的配置文件位置,項(xiàng)目打包好后,我們可以通過(guò)命令行的方式在啟動(dòng)時(shí)指定配置文件的位置,需要的朋友可以參考下2023-10-10
Java基于裝飾者模式實(shí)現(xiàn)的圖片工具類(lèi)實(shí)例【附demo源碼下載】
這篇文章主要介紹了Java基于裝飾者模式實(shí)現(xiàn)的圖片工具類(lèi),結(jié)合完整實(shí)例形式分析了裝飾者模式實(shí)現(xiàn)圖片的判斷、水印、縮放、復(fù)制等功能,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2017-09-09
使用SpringCache進(jìn)行緩存數(shù)據(jù)庫(kù)查詢(xún)方式
這篇文章主要介紹了使用SpringCache進(jìn)行緩存數(shù)據(jù)庫(kù)查詢(xún)方式,具有很好的參考價(jià)值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
Java中使用Preconditions來(lái)檢查傳入?yún)?shù)介紹
這篇文章主要介紹了Java中使用Preconditions來(lái)檢查傳入?yún)?shù)介紹,本文只是作為一個(gè)簡(jiǎn)單的用法介紹,需要的朋友可以參考下2015-06-06

