通过JDBC来实现。
import java.sql.*;
import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;public class TestDB
{ public static void main(String args[])
{
System.out.println("aaaaaaaaaa");
Connection connection=null;
PreparedStatement statement=null;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connection=DriverManager.getConnection("jdbc:odbc:TestDB","sa","");
}
catch(Exception e)
{
e.printStackTrace();
} try{
String sql="select * from people";
statement=connection.prepareStatement(sql);
ResultSet result=statement.executeQuery();
while(result.next())
{
int nid=result.getInt("id");
String strid=new String("id "+nid);
System.out.println(strid);
String name=result.getString("name");
System.out.println("name "+name);
String sex=result.getString("sex");
System.out.println("sex "+sex); // int age=result.getInt("age");
// String strAge=new String(""+age);
// System.out.println("age "+strAge); String phone=result.getString("phone");
System.out.println("phone "+phone);
String other=result.getString("other");
System.out.println("other "+other); }
result.close();
statement.close(); }catch(Exception e)
{
System.out.println(e.toString()); }
}
}
import java.sql.*;
import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;public class TestDB
{ public static void main(String args[])
{
System.out.println("aaaaaaaaaa");
Connection connection=null;
PreparedStatement statement=null;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connection=DriverManager.getConnection("jdbc:odbc:TestDB","sa","");
}
catch(Exception e)
{
e.printStackTrace();
} try{
String sql="select * from people";
statement=connection.prepareStatement(sql);
ResultSet result=statement.executeQuery();
while(result.next())
{
int nid=result.getInt("id");
String strid=new String("id "+nid);
System.out.println(strid);
String name=result.getString("name");
System.out.println("name "+name);
String sex=result.getString("sex");
System.out.println("sex "+sex); // int age=result.getInt("age");
// String strAge=new String(""+age);
// System.out.println("age "+strAge); String phone=result.getString("phone");
System.out.println("phone "+phone);
String other=result.getString("other");
System.out.println("other "+other); }
result.close();
statement.close(); }catch(Exception e)
{
System.out.println(e.toString()); }
}
}
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connection = DriverManager.getConnection("jdbc:odbc:SAMPLE","sa","");
}
catch(Exception e)
{ }
2、访问数据库: statement=connection.prepareStatement(strSql);
ResultSet result=statement.executeQuery();
connection=DriverManager.getConnection("jdbc:odbc:TestDB","sa","");我要读取的是TestDB信息。
比如说ODBC中有TestDB,怎样通过程序知道TestDB的存在?
可以这么做,调用一本地方法,去读取odbc数据源信息,存到一××.properties文件中,然后再用java去读该文件!