Java 1.6-Oracle11g-JDBC(OJDBC 6を使用)コード(以下)を作成しました。例外が発生します-java.sql.SQLException: Missing IN or OUT parameter at index:: 1
なぜこれが発生し、どうすれば修正できますか?
私の出力は-
create CREATE TABLE employee(emp_name varchar(25),emp_address varchar(25))
insert INSERT INTO employee(jim,germany) values(?,?)
Exception: java.sql.SQLException: Missing IN or OUT parameter at index:: 1
コードは-
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
public class Oracle {
public static void main(String[]args)
{
try
{
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@//localhost:1521/xe", "newman", "123456");
Statement stmt = con.createStatement();
String create = "CREATE TABLE employee(emp_name varchar(25),emp_address varchar(25))";
System.out.println("create " + create);//
stmt.execute(create);
//insert 1st row
String inserting = "INSERT INTO employee(hans,germany) values(?,?)";
System.out.println("insert " + inserting);//
PreparedStatement ps = con.prepareStatement(inserting);
ps.executeUpdate();
//insert 2nd row
inserting = "INSERT INTO employee(david,austria) values(?,?)";
System.out.println("insert " + inserting);//
ps = con.prepareStatement(inserting);
ps.executeUpdate();
}catch(SQLException ex){System.out.println("Exception: " + ex);}
}
}
編集-コードを修正するには、次を使用します-
//1行目を挿入
String inserting = "INSERT INTO
employee(emp_name,emp_address) values(?,?)";
PreparedStatement ps = con.prepareStatement(inserting);
System.out.println("insert " + inserting);//
ps.setString(1, "hans");
ps.setString(2, "germany");
ps.executeUpdate();
//2行目を挿入
inserting = "INSERT INTO
employee(emp_name,emp_address) values(?,?)";
ps = con.prepareStatement(inserting);
System.out.println("insert " + inserting);//
ps.setString(1, "david");
ps.setString(2, "austria");
ps.executeUpdate();