import java.sql.*;
import java.io.*;
import java.util.*;
public class A {
public static void main(String args[]){
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con= DriverManager.getConnection("jdbc:odbc:debjeet", "system", "tiger");
PreparedStatement st=con.prepareStatement("insert into emp values('?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?')"); //there are 17 fields in the database
BufferedReader dis= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the desired username");
String us=dis.readLine();
st.setString(1,(String)us); //this was an attempt made by me to remove the exception ,otherwise all were below as from index 4.also the exception occurs here.
System.out.println("Enter the desired password");
String pw=dis.readLine();
st.setString(2,pw);
System.out.println("Enter your First Name");
String fn=dis.readLine();
st.setString(3, fn);
System.out.println("Enter your Middle Name");
String mn=dis.readLine();
System.out.println("Enter your Last Name");
String ln=dis.readLine();
System.out.println("Enter your Email");
String em=dis.readLine();
System.out.println("Enter your Date of Birth");
int dob= Integer.parseInt(dis.readLine());
System.out.println("Enter your Month of Birth");
String mob=dis.readLine();
System.out.println("Enter your Year of Birth");
int yob=Integer.parseInt(dis.readLine());
System.out.println("Enter your Gender(M/F)");
String gen=dis.readLine();
System.out.println("Enter your Institute");
String inst=dis.readLine();
System.out.println("Enter your Address");
String add=dis.readLine();
System.out.println("Enter your City");
String ct=dis.readLine();
System.out.println("Enter your State");
String state=dis.readLine();
System.out.println("Enter your Pincode");
int pin=Integer.parseInt(dis.readLine());
System.out.println("Enter your Country");
String cn=dis.readLine();
System.out.println("Enter your 10 digit mobile number");
String phnum=dis.readLine();
System.out.println("Thank you :D");
st.setString(4, mn);
st.setString(5, ln);
st.setString(6, em);
st.setString(7, gen);
st.setString(8, inst);
st.setString(9, add);
st.setString(10, ct);
st.setString(11, state);
st.setInt(12, pin);
st.setString(13, cn);
st.setString(14, phnum);
st.setInt(15,dob);
st.setString(16, mob);
st.setInt(17, yob);
int i=st.executeUpdate();
if(i>0)
System.out.println("SUCCESS");
con.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
出力:
Enter the desired username
grg
java.lang.NullPointerException
このコードは、マシンでヌル ポインター例外を示し、他のマシンで無効な列インデックス メッセージを示しています。一方、ステートメントオブジェクトを使用して値を挿入しようとすると、データベースは正常に機能します。準備されたステートメントオブジェクトに問題があると思いますが、解決できません。