网上的资料,呵呵,谢谢那位仁兄了,又学到一点点
本程序是JAVA通过ODBC连接到SQL SERVER2000上面的一个数据库
的TEST表,并输出TEST表中的所有内容
//package lesson0;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.sql.*;
public class ODBCBridge {
public static void main(String[] args) {
String url="jdbc:odbc:GoodsSupply";//这个系统DSN的名
Statement sm=null;
String command=null;
ResultSet rs=null;
String tableName=null;
String cName=null;
String result=null;
try {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //加载驱动
}catch(ClassNotFoundException e){
System.out.println("Can not load Jdbc-Odbc Bridge Driver");
System.err.print("ClassNotFoundException:");
System.err.println(e.getMessage());
}
Connection con=DriverManager.getConnection(url,"USER","PASSWORD"); //使用SQL-SERVER2000认证
DatabaseMetaData dmd=con.getMetaData(); //DMD为连接的相应情况
System.out.println("连接的数据库:"+dmd.getURL());
System.out.println("驱动程序:"+dmd.getDriverName());
sm=con.createStatement();
command="select * from TEST";
rs=sm.executeQuery(command); //执行查询
System.out.println("查询结果为:/nID NAME");
do
{
result=rs.getString("ID");
System.out.print(result+" ");
result=rs.getString("NAME");
System.out.println(result);
}while(rs.next());
break;
}
}catch(SQLException ex) {
System.out.println("SQLException:");
while(ex!=null) {
System.out.println("Message:"+ex.getMessage());
ex=ex.getNextException();
}
}catch(Exception e) {
System.out.println("IOException");
}
}
}