I am getting an SQL error while establishing database connection in Java (Jdbc:Odbc). How do I fix this error?
Here is the relevant code:
import java.sql.*;
import java.lang.*;
import java.io.*;
public class db {
public static void main(String args[]) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:data","system","password");
System.out.println("\n connection established");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from stud");
System.out.println("details of empdata\n");
while(rs.next()) {
System.out.println(rs.getInt(1)+"\t"+rs.getString(2)+"\t"+rs.getString(3));
}
} catch(Exception e) {
System.out.println("sql error");
}
}
}
/* Execute query requires string as argument and u have not use semi colon at query if u are getting class not found exception than handle it first THIS SHOULD BE PASS IN DOUBLE QOUTES="select * from stud;"*/
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:data","system","password");
System.out.println("\n connection established");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from stud;");//----HERE--*/
System.out.println("details of empdata\n");
while(rs.next()) {
System.out.println(rs.getInt(1)+"\t"+rs.getString(2)+"\t"+rs.getString(3));
}
} catch(Exception e) {
System.out.println("sql error");
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.