Code for values that are taken from user and inserted into database but gives error
"java.sql.SQLException: The column position '45' is out of range. The number of columns for this ResultSet is '2'."
when user enters 45 as number, the code takes that as column number which is 2 what must be changed so that number is the literal that goes in the column id the table has two columns named ID and 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();
}
}
}
}