Package demo;
/**
* <P> title: </P>
* <P> Description: </P>
* <P> copyright: Copyright (c) 2005 </P>
* <P> company: </P>
* @ Author not attributable
* @ Version 1.0
*/
Import java. SQL .*;
Public class printdata {
Public printdata (){
}
Public static void main (string [] ARGs ){
Connection con;
Statement SQL;
Resultset RS;
Try {
Try {
Class. forname ("Sun. JDBC. ODBC. jdbcodbcdriver ");
}
Catch (classnotfoundexception ex1 ){
Ex1.printstacktrace ();
}
Con = drivermanager. getconnection ("JDBC: ODBC: Test", "sa ","");
SQL = con. createstatement ();
Rs = SQL .exe cutequery ("select top 10 * from MERs ");
While (Rs. Next ()){
System. Out. Print (Rs. getstring ("customerid "));
System. Out. println ();
}
Con. Close ();
}
Catch (sqlexception ex ){
Ex. printstacktrace ();
}
}
}
Note:
If class. forname ("Sun. JDBC. ODBC. jdbcodbcdriver") is not added ");
An error is prompted.
Java. SQL. sqlexception: no suitable driver
At java. SQL. drivermanager. getconnection (drivermanager. Java: 532)
At java. SQL. drivermanager. getconnection (drivermanager. Java: 171)
At demo. printdata. Main (printdata. Java: 21)
Class. forname ("Sun. JDBC. ODBC. jdbcodbcdriver"); // Add this sentence
String url = "JDBC: ODBC: Student ";
Connection conn = drivermanager. getconnection (URL ,"","");
Register the driver before connection