Netbeans上のJavaを使用してSQLデータベースにデータを追加する際に問題が発生しました。Javaアプリケーション用SQLデータベースの結果セットエラーの列数
String bladeSerial;
String bladeType;
LocalTime startTime1;
private void startButton2ActionPerformed(java.awt.event.ActionEvent evt) {
Connection conn = null;
Statement st = null;
try {
conn = DriverManager.getConnection ("jdbc:derby://localhost:1527/db01", "Administrator", "admin"); //run procedure getConnection to connect to the database - see below
st = conn.createStatement(); //set up a statement st to enable you to send SQL statements to the database.
} catch (SQLException ex) {
Logger.getLogger(FormTwo1.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println ("Successful Connection");
String query = "insert into TB01(SERIAL,BLADETYPE,STARTT1) values ('+bladeSerial+', '+itemText+', '+(String.valueOf(startTime1))+')";
try (PreparedStatement pstmt = conn.prepareStatement(query)) {
pstmt.setString(1, bladeSerial);
pstmt.setString(2, bladeType);
pstmt.setString(3, String.valueOf(startTime1));
pstmt.executeUpdate();
} catch (SQLException ex) {
Logger.getLogger(FormTwo1.class.getName()).log(Level.SEVERE, null, ex);
}
私は、データベース内のThe column position '1' is out of range. The number of columns for this ResultSet is '0'.
エラーを取得し、シリアルがVARCHAR(5)
で、BladetypeはVARCHAR(80)
あるとStartT1はstartTime1変数がフォーマットHHに保存されているVARCHAR(12)
です:MM:SS.SSS 。
私はあなたのクエリにプレースホルダを与える必要があり、このエラー
準備文がどのように機能するか分かりません。クエリは 'TB01(SERIAL、BLADETYPE、STARTT1)の値(?、?、?)に挿入する'でなければなりません。疑問符はパラメータのプレースホルダです。クエリのパラメータ値を連結すると目的が完全に無効になります。 –