• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception in thread "main" java.sql.SQLException

 
vicky verma
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.sql.DriverManager;
import java.sql.*;
import java.io.*;
public class DataConnect {
public static void main(String[] args) throws SQLException,ClassNotFoundException,UnsupportedClassVersionError{

Connection con=null;
Statement st=null;
ResultSet rs=null;
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbc racle:thin:@sun:1521:roshan","vikas","verma");

st=con.createStatement();
rs=st.executeQuery("select emp_id,f_name,l_name from emp");
while(rs.next()){

System.out.println("Emp_id"+rs.getInt(1));
System.out.println("First_name"+rs.getInt(2));
System.out.println("Last_name"+rs.getInt(3));

}
rs.close();
con.close();

}

}
Will this program run or not.if not then what is the reason please explain me.

emp_id is of type number.
f_name,l_name is of type varchar.

before answering ,please read this paragraph carefully

For example, the method getInt can be used to retrieve any of the numeric or character types. The data it retrieves will be converted to an int; that is, if the SQL type is VARCHAR , JDBC will attempt to parse an integer out of the VARCHAR. The method getInt is recommended for retrieving only SQL INTEGER types, however, and it cannot be used for the SQL types BINARY, VARBINARY, LONGVARBINARY, DATE , TIME, or TIMESTAMP.
 
Jan Cumps
Bartender
Posts: 2602
13
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vikas,

Please show us the error message.
Upon first review, I think first_name and Last_name are Strings:Regards, Jan
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Post the exception stacktrace.

As your exception is going out of main method , the main thread would be killed once exception is thrown.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic