(1)请先在控制面板的ODBC数据源中配置名为star的数据源。
(2)代码
import java.sql.*;
public class Example1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Connection con;
Statement sql;
ResultSet rs;
try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e){
System.out.print(e);
}
try{
con=DriverManager.getConnection("jdbc:odbc:star","sa","sa123456");
sql=con.createStatement();
rs=sql.executeQuery("select * from s where sage>20");
while(rs.next()){
String number=rs.getString(1);
String name=rs.getString(2);
String sex=rs.getString(3);
int age=rs.getInt(4);
String deptment=rs.getString(5);
System.out.printf("%s\t",number);
System.out.printf("%s\t",name);
System.out.printf("%s\t",sex);
System.out.printf("%s\t",age);
System.out.printf("%s\t",deptment);
System.out.println();
}
con.close();
}
catch(SQLException e){
System.out.println(e);
}
}
} |
|