假设现在有一个test.mdb数据库放在C盘根目录下,里边有表cust_info,字段为cust_id,cust_name,怎么连接到这个数据库,并插入一条数据?
哪里有相应的例子也可以给我,多谢了。

解决方案 »

  1.   

    jdbc
    jdbc:odbc
    例子网上自己找。
      

  2.   

    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance(); 
    String url="jdbc:odbc:people";//people为你数据源的名字
    String user=""; 
    String password=""; 
    Connection conn= DriverManager.getConnection(url,user,password); 
    Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); 
    String sql="select * from test"; 
    ResultSet rs=stmt.executeQuery(sql); 
    while(rs.next()) {
    out.print(rs.getString(1));//或者out.print(rs.getString("字段名"))
    out.print(rs.getString(2));}
    out.print("数据库操作成功,恭喜你"); 
    rs.close(); 
    stmt.close(); 
    conn.close();
      

  3.   

    先到ODBC里建一个系统数据源"test"连上test.mdb
    数据库然后就可以用下面的代码向数据库里插入数据了 
    database.java 
    ------------------ 
    import java.sql.*; class database { 
      public static void main(String args[]) { 
        try{ 
          Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
          String url="jdbc:odbc:test"; 
          Connection connection=DriverManager.getConnection(url); 
          Statement statement = connection.createStatement(); 
          String sql="INSERT INTO cust_info(cust_id, cust_name) VALUES('0001', 'TEST')"; 
          statement.executeUpdate(sql); 
          connection.close(); 
        } catch(Exception ex){ 
          System.out.println(ex); 
          System.exit(0); 
        } 
      } 
    } 如果不会在ODBC里建系统数据源的话,也可以直接把数据库的地址写入:
    database.java 
    ------------------ 
    import java.sql.*; class database { 
      public static void main(String args[]) { 
        try{ 
          Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
          String dburl ="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=C:\\test.mdb";//注意空格
          Connection connection=DriverManager.getConnection(dburl); 
          Statement statement = connection.createStatement(); 
          String sql="INSERT INTO cust_info(cust_id, cust_name) VALUES('0001', 'TEST')"; 
          statement.executeUpdate(sql); 
          connection.close(); 
        } catch(Exception ex){ 
          System.out.println(ex); 
          System.exit(0); 
        } 
      } 

      

  4.   

    关键是驱动!连接后select狗狗一搜一大堆
      

  5.   

    同问一下
    分页显示的查询特定条数位置的语句是不是和在SQL2000里的一样
    是什么啊,恭候大侠出招
      

  6.   

    1.连接
    public static synchronized Connection getConnect() throws SQLException {
    Connection conn = null;
    try {
    //Jdbc-odbc bridge
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)}; DBQ=C:\\test.mdb";
    conn = DriverManager.getConnection(url, "", "");
    conn.setAutoCommit(true);
    } catch (Exception e) {
    throw new SessionException("E001");
    }
    return conn;
    }
    2.
        Connection conn = getConnect();
    Connection conn = DBUtil.getConnect();
    PreparedStatement ps = null;
    String strSql = "INSERT cust_info  (cust_id,cust_name) VALUES(?,?)";
    try {
      ps = conn.prepareStatement(strSql);
      ps.setString(1, VoteCountUnion[i]);
      ps.setString(2, "1");
      ps.executeUpdate();
    } catch (SQLException e) {
      logger.error(e.getMessage());
      throw new SessionException("E002");
    } finally{
      ps.close();
      conn.close();
    }
      

  7.   

    ps.setString(1, VoteCountUnion[i]);
    改为
    ps.setString(1, "customercode");