給它一個名字並查找它。
該列是字符串類型還是數字?
空的catch塊是錯誤的。打印或記錄堆棧跟蹤。你永遠不會知道是否拋出異常。
確定你不想要select count(*) from root.gledi
?這個查詢看起來不對我。
您不需要像finally
塊那樣關閉Connection
,Statement
或ResultSet
。
您應該將此代碼封裝在一個方法中並給它一個Connection
,而不是每次都創建它。
這麼少的代碼,太多的錯誤。
以下是我可能會寫它:
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* JdbcDemo
* @author Michael
* @link http://stackoverflow.com/questions/21205161/how-to-fix-a-db-probblem-in-java-with-derby-db/21205183#21205183
* @since 1/18/14 9:51 AM
*/
public class JdbcDemo {
private static final String SELECT_MAX_ROW_NUMBER = "SELECT MAX(NR) as maxnr from ROOT.GLEDI";
private Connection connection;
public JdbcDemo(Connection connection) {
this.connection = connection;
}
public String getMaxRowNumber() {
String maxRowNumber = "";
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = connection.prepareStatement(SELECT_MAX_ROW_NUMBER);
rs = ps.executeQuery();
while (rs.next()) {
maxRowNumber = rs.getString("maxnr");
}
} catch (Exception e) {
e.printStackTrace(); // better to log this.
maxRowNumber = "";
} finally {
close(rs);
close(ps);
}
return maxRowNumber;
}
// belongs in a database utility class
public static void close(Statement st) {
try {
if (st != null) {
st.close();
}
} catch (SQLException e) {
e.printStackTrace(); // better to log this
}
}
// belongs in a database utility class
public static void close(ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace(); // better to log this
}
}
}
告訴我們什麼是與該行發生。 – Henry
String nr1 = rs.getString(1); –