第一种要设odbc的数据源
不提倡使用
还是用jdbc比较好

解决方案 »

  1.   

    第一种要设数据源。
    当然是第二种(类型4:本地协议纯JAVA驱动程序)好。是最小和最有效率的。(书上说)
      

  2.   

    兄弟,现在几乎所有人都用JDBC的,
      

  3.   

    第一种这样:
    jdbc.drivers=sun.jdbc.odbc.JdbcOdbcDriver
    jdbc.url=jdbc:odbc:corejava
    jdbc.username=PUBLIC
    jdbc.password=PUBLICmakedb.java/**
     * @version 1.20 1999-08-16
     * @author Cay Horstmann
     */import java.net.*;
    import java.sql.*;
    import java.io.*;
    import java.util.*;class MakeDB
    {  public static void main (String args[])
       {  try
          {  Connection con = getConnection();
             Statement stmt = con.createStatement();         String tableName = "";
             if (args.length > 0)
                tableName = args[0];
             else
             {  System.out.println("Usage: MakeDB TableName");
                System.exit(0);
             }         BufferedReader in = new BufferedReader(new
                FileReader(tableName + ".dat"));         createTable(tableName, in, stmt);
             showTable(tableName, stmt);         in.close();
             stmt.close();
             con.close();
          }
          catch (SQLException ex)
          {  System.out.println ("SQLException:");
             while (ex != null)
             {  System.out.println ("SQLState: "
                   + ex.getSQLState());
                System.out.println ("Message:  "
                   + ex.getMessage());
                System.out.println ("Vendor:   "
                   + ex.getErrorCode());
                ex = ex.getNextException();
                System.out.println ("");
              }
          }
          catch (IOException ex)
          {  System.out.println("Exception: " + ex);
             ex.printStackTrace ();
          }
       }   public static Connection getConnection()
          throws SQLException, IOException
       {  Properties props = new Properties();
          String fileName = "MakeDB.pro";
          FileInputStream in = new FileInputStream(fileName);
          props.load(in);      String drivers = props.getProperty("jdbc.drivers");
          if (drivers != null)
             System.setProperty("jdbc.drivers", drivers);
          String url = props.getProperty("jdbc.url");
          String username = props.getProperty("jdbc.username");
          String password = props.getProperty("jdbc.password");      return
             DriverManager.getConnection(url, username, password);
       }   public static void createTable(String tableName,
          BufferedReader in, Statement stmt)
          throws SQLException, IOException
       {  String line = in.readLine();
          String command = "CREATE TABLE " + tableName
             + "(" + line + ")";
          stmt.executeUpdate(command);      while ((line = in.readLine()) != null)
          {  command = "INSERT INTO " + tableName
                + " VALUES (" + line + ")";
             stmt.executeUpdate(command);
          }
       }   public static void showTable(String tableName,
          Statement stmt) throws SQLException
       {  String query = "SELECT * FROM " + tableName;
          ResultSet rs = stmt.executeQuery(query);
          ResultSetMetaData rsmd = rs.getMetaData();
          int columnCount = rsmd.getColumnCount();
          while (rs.next())
          {  for (int i = 1; i <= columnCount; i++)
             {  if (i > 1) System.out.print(", ");
                System.out.print(rs.getString(i));
             }
             System.out.println();
          }
          rs.close();
       }
    }要编译上述程序,必须在系统中设置一个corejava的 odbc数据源,makedb.pro和makedb.java必须在一个目录 如果需要建立 表的几个数据文件(authors.dat以便用java  makedb authous 在corejava中生成authors表 其他数据文件类似)资料来自 java核心技术一书 卷2