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.executeQuery("select top 10 * from customers");
while (rs.next()){
System.out.print(rs.getString("CustomerID"));
System.out.println();
}
con.close();
}
catch(SQLException ex){
ex.printStackTrace();
}
}
}
注:
如果不加上Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
则会提示出错
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"); //加上这一句
String url = "jdbc:odbc:student";
Connection conn = DriverManager.getConnection(url,"","");
连接前先注册驱动程序