java.sql.SQLException: No value specified for parameter 1 異常分析
錯誤原始碼 java.sql.SQLException: No value specified for parameter 1,因為執行executeQuery()先於對sql語句中的佔位符賦值
@Test
public void test2() {
InputStream is = null;
FileOutputStream fos = null;
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = JDBCUtils.getConnection();
String sql = "select id,name,email,birth,photo from customers where id = ?";
ps = conn.prepareStatement(sql);
// ps.setInt(1,23);
rs = ps.executeQuery();
if (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
String email = rs.getString("email");
Date birth = rs.getDate("birth");
Customers customers = new Customers(id, name, email, birth);
System.out.println(customers);
//將Blob型別的欄位下載下來,以檔案的方式儲存在本地
Blob photo = rs.getBlob("photo");
is = photo.getBinaryStream();
fos = new FileOutputStream("erxiong.jpg");
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (is != null)
is.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fos != null)
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
JDBCUtils.closeResource(conn, ps, rs);
}
}
修改後,執行executeQuery()方法之前,先填寫佔位符
@Test
public void test2() {
InputStream is = null;
FileOutputStream fos = null;
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = JDBCUtils.getConnection();
String sql = "select id,name,email,birth,photo from customers where id = ?";
ps = conn.prepareStatement(sql);
//先對sql佔位符進行賦值
ps.setInt(1,23);
rs = ps.executeQuery();
if (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
String email = rs.getString("email");
Date birth = rs.getDate("birth");
Customers customers = new Customers(id, name, email, birth);
System.out.println(customers);
//將Blob型別的欄位下載下來,以檔案的方式儲存在本地
Blob photo = rs.getBlob("photo");
is = photo.getBinaryStream();
fos = new FileOutputStream("erxiong.jpg");
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (is != null)
is.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fos != null)
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
JDBCUtils.closeResource(conn, ps, rs);
}
}
相關文章
- Idea資料庫引入異常:java.sql.SQLException: The server time zone value ‘Öйú±ê׼ʱ¼ä‘ is unrecognizedIdea資料庫JavaSQLExceptionServerZed
- java.sql.SQLException: Io 異常: Connection refusedJavaSQLException
- The value (30) of MAXTRANS parameter ignored. 問題分析
- java.sql.SQLException: Incorrect string valueJavaSQLException
- 使用DBMS_UTILITY.get_parameter_value檢視session的parameterSession
- Flutter 常見異常分析Flutter
- SQL java.sql.SQLException: Parameter metadata not available for the given statementSQLJavaExceptionAI
- java.sql.SQLException: Io 異常: The Network Adapter could not establish the connecJavaSQLExceptionAPT
- java.sql.SQLException: The server time zone value ‘???ú±ê×??±??‘ is unrecognized or represents moreJavaSQLExceptionServerZed
- 異常處理1
- 異常-try...catch的方式處理異常1
- tomcat連線池不夠-java.sql.SQLException: Io 異常: Connection resetTomcatJavaSQLException
- Android異常分析(轉)Android
- 異常解決——The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than oneServerZed
- ORA-00838: Specified value of MEMORY_TARGET is too small
- OGG-01453 Database login information not specified in parameter fileDatabaseORM
- ibatis resultMap引數填充異常:java.sql.SQLException: Column 'XXX' not found.BATJavaSQLException
- tomcat 啟動應用報錯:java.sql.SQLException: Io 異常: Connection resetTomcatJavaSQLException
- bug及異常處理1
- c++ 異常處理(1)C++
- binlog 異常暴漲分析
- 跑批SQL效能異常分析SQL
- java.sql.SQLException: The server time zone value ‘Öйú±ê׼ʱ¼ä‘ is unrecognized...報錯解決JavaSQLExceptionServerZed
- ORA-31600: invalid input value INDEX PARTITION for parameterIndex
- wireshark、異常資料分析、常見RST介紹
- java.sql.SQLException: Parameter index out of range (3 > number of parameters, which is 2)的解決方法JavaSQLExceptionIndex
- ORA-32004: obsolete and/or deprecated parameter(s) specified ORACLE instance staOracle
- ORA-32004: obsolete and/or deprecated parameter(s) specified ORACLE instance startedOracle
- 深入淺出話異常-(1) (轉)
- 當機導致slave異常分析
- Oracle JOB異常中斷原因分析Oracle
- ORA-29707: inconsistent value 2 for initialization parameter cluster_database_Database
- 資料庫報錯java.sql.SQLException: Field ‘id‘ doesn‘t have a default value資料庫JavaSQLException
- 異常篇——異常處理
- 異常和異常呼叫鏈
- 「Window平臺」異常掛鉤大法(1)
- [異常等待事件latch undo global data]分析事件
- RocketMQ Series---No route info of this topic異常分析MQ