简体   繁体   English

Java应用程序的SQL数据库中结果集错误的列数

[英]Number of columns for result set error in SQL database for Java app

I'm having an issue with adding data to a sql database through Java on Netbeans. 我在通过Netbeans上的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);
    }

I get the error The column position '1' is out of range. The number of columns for this ResultSet is '0'. 我收到错误The column position '1' is out of range. The number of columns for this ResultSet is '0'. The column position '1' is out of range. The number of columns for this ResultSet is '0'.

In the database, Serial is VARCHAR(5) , Bladetype is VARCHAR(80) and StartT1 is VARCHAR(12) 在数据库中,Serial为VARCHAR(5) ,Bladetype为VARCHAR(80) ,StartT1为VARCHAR(12)

The startTime1 variable is saved in the format HH:mm:ss.SSS. startTime1变量以HH:mm:ss.SSS格式保存。

数据库结构

I appreciate any help on this error 感谢您对此错误的任何帮助

You need to give placeholder in your query. 您需要在查询中提供占位符。 Change your code as given here... 更改您的代码,如下所示...

String query = "insert into TB01(SERIAL,BLADETYPE,STARTT1) values (?, ?, ?)";
PreparedStatement pstmt = conn.prepareStatement(query);
pstmt.setString(1, bladeSerial);
pstmt.setString(2, bladeType);
pstmt.setString(3, String.valueOf(startTime1));
pstmt.executeUpdate();

You don't need to give column names in query when you are using Prepared statement. 使用Prepared语句时,无需在查询中提供列名。 Do the following changes: 进行以下更改:

 String query = "insert into TB01(SERIAL,BLADETYPE,STARTT1) values (?, ?, ?)";

Hope it helps!! 希望能帮助到你!!

Here you are forming query like simple statement and used it in prepared statement which is not possible, so change your query with place holder like below. 在这里,您将形成简单语句之类的查询,并在不可能的预准备语句中使用它,因此请使用如下所示的占位符更改查询。

String query = "insert into TB01(SERIAL,BLADETYPE,STARTT1) values (?, ?, ?)";
PreparedStatement pstmt = conn.prepareStatement(query);
pstmt.setString(1, bladeSerial);
pstmt.setString(2, bladeType);
pstmt.setString(3, String.valueOf(startTime1));
pstmt.executeUpdate();

If you want to directly use variables names like bladeSerial , then you should use these String variables as if you're adding multiple Strings. 如果要直接使用bladeSerial类的变量名称,则应使用这些String变量,就像要添加多个String一样。

String query = "insert into TB01(SERIAL,BLADETYPE,STARTT1) values ("+bladeSerial+", "+itemText+", "+(String.valueOf(startTime1))+")";

But this is strictly not recommended as it would introduce serious security issues. 但是严格建议不要这样做,因为这会带来严重的安全问题。

The recommended way is to use PreparedStatement . 推荐的方法是使用PreparedStatement The query you've written is correct, it's just that you have to use placeholders instead of variable names. 您编写的查询是正确的,只是您必须使用占位符而不是变量名。

String query = "insert into TB01(SERIAL,BLADETYPE,STARTT1) values (?, ?, ?)";
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) {
    // Exception handling
    Logger.getLogger(FormTwo1.class.getName()).log(Level.SEVERE, null, ex);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM