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
此代码显示机器中的空指针异常和其他机器中的无效列索引消息。不知道如何进行。另一方面,如果我尝试使用语句对象插入值,则数据库工作正常。我猜准备好的语句对象存在问题,但无法解决。