从控制台到数据库java.sql.sqlexception的Java jdbc记录:

时间:2018-01-31 14:50:24

标签: java jdbc

从用户获取并插入数据库但出现错误的值的代码

" java.sql.SQLException:列位置' 45'超出范围。此ResultSet的列数为' 2'。"

当用户输入45作为数字时,代码将其作为列号,即2 必须更改的内容,以便number是列id中的文字,表中有两列名为ID和Name;

package Rdbms;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

class Jdbcfrnd {

    public static void main(String[] args) throws SQLException {

        String drv = "org.apache.derby.jdbc.ClientDriver";
        String url = "jdbc:derby://localhost:1527/Oracle;create=true;";
        String user = "Android";
        String password = "java";

        Connection myConn = null;
        PreparedStatement myStmt = null;
        Connection dbConnection = null;

        Scanner scanner = null;

        try {
            // 0. read user input from command line: last name, first name and email
            scanner = new Scanner(System.in);

                        Scanner sc = new Scanner(System.in);
                        System.out.print("Enter number 1: ");
                        int a;
                        a = sc.nextInt();                                                                                   

            System.out.print("Enter your Name: ");
            String firstName = scanner.nextLine();

                        //System.out.print("Enter your email: ");
                        //String email = scanner.nextLine();

            // 1. Get a connection to database
            myConn = DriverManager.getConnection(url, user, password);

            // 2. Create a statement
            String sql = "insert into GOOD "
                    + " (ID, Name)" + " values (?, ?)";

            myStmt = myConn.prepareStatement(sql);

            // set param values
            myStmt.setString(a , firstName);
            //myStmt.setString(ID, "Android");

            // 3. Execute SQL query
            myStmt.executeUpdate();

            System.out.println("Insert complete.");
            } catch (Exception exc) {
            exc.printStackTrace();
            } finally {
            if (myStmt != null) {
                myStmt.close();
            }

            if (myConn != null) {
                myConn.close();
            }

            if (scanner != null) {
                scanner.close();
            }
        }
    }

 }

2 个答案:

答案 0 :(得分:1)

  

java.sql.SQLException:列位置“45”超出范围。此ResultSet的列数为“2”

正如您所提到的,当用户输入45作为数字时,代码将其作为列号,即2 。现在,您已指定为列位置的相同输入

int a;
a = sc.nextInt();
...
...
myStmt.setString(a , firstName);

请注意,setString的第一个论点是parameterIndex,你的误解是有价值的。相反,你应该

myStmt.setInt(1, a);
myStmt.setString(2, firstName);

答案 1 :(得分:0)

  

当用户输入45作为数字时,代码将其作为列号,即2

这没有意义。您的代码将列号设为45,错误显示为

  

列位置“45”超出范围。

尝试为您要设置的列

创建单独的变量
myStmt = myConn.prepareStatement(sql);
int col = 1;

Scanner sc = new Scanner(System.in);
System.out.print("Enter number 1: ");
int a= sc.nextInt();
myStmt.setInt(col++, a);

System.out.print("Enter your Name: ");
String firstName = sc.nextLine();
myStmt.setString(col++ , firstName);