
import java.sql.*;
public class JDBCExample{ public static void main(String args[]) {
try{
Connection con;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con=DriverManager.getConnection("jdbc:odbc:datastud"); try{ System.out.println("Getting All Rows from a table!"); Statement st = con.createStatement(); ResultSet res = st.executeQuery("SELECT * FROM college"); System.out.println("studentcode: " + "\t" + "studentname: "); while (res.next()) { int i = res.getInt("stud_code"); String s = res.getString("stud_name"); System.out.println(i + "\t\t" + s); } con.close(); } catch (SQLException s){ System.out.println("SQL code does not execute."); }} catch (Exception e){ System.out.println("Error:connection not created"); }

Hi,
I tried the following updated code and it is working. Please copy and run it on your computer.
import java.sql.*;
public class JDBCExample{
public static void main(String args[]) {
try{
Connection con;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:datastud");
try{
System.out.println("Getting All Rows from a table!");
Statement st = con.createStatement();
ResultSet res = st.executeQuery("SELECT * FROM college");
System.out.println("studentcode: " + "\t" + "studentname: ");
while (res.next()) {
String i = res.getString("stud_code");
String s = res.getString("stud_name");
System.out.println(i + "\t\t" + s);
}
con.close();
} catch (SQLException s){
System.out.println("Error is:" + s.getMessage());
}
} catch (Exception e){
System.out.println(e.getMessage());
System.out.println("Error:connection not created");
}
}
}
Thanks

Hi,
Please check the field stud_code in database. In my case it is Text type. So, I changed the code:
int i = res.getInt("stud_code");
into
String i = res.getString("stud_code");
Thanks

Hi,
Please check the thread JDBC ODBC error and the specified dsn contains an architecture mismatch between the driver and application windows.
Thanks
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.