日期:2014-05-20 浏览次数:20779 次
private void initSql(){ try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection conn = DriverManager.getConnection("jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=chemical.mdb;"); //Statement stmt=conn.createStatement(); //ResultSet rs=stmt.executeQuery("select * from books"); }catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.Statement; public class AccessTest { public static void main(String[] args) { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String url = "jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=C:\\Test.mdb"; Connection conn = DriverManager.getConnection(url, "", ""); Statement stmt = conn.createStatement(); PreparedStatement ps ; for(int i=0;i<1000;i++){ String sex = ""; if(i%2==0){ sex="M"; }else{ sex="F"; } ps = conn.prepareStatement ("insert into test values('"+i+"','Yaku"+i+"','"+sex+"')"); ps.executeUpdate(); ps.close(); } stmt.close(); conn.close(); } catch (Exception ex) { ex.printStackTrace(); } } }
------解决方案--------------------
String url = "jdbc:odbc:数据源名; 这样试试
------解决方案--------------------
根据错误提示,楼主用ODBC连接数据库,请问搭建了ODBC数据源,没有的话先搭建ODBC驱动,建议楼主直接用JDBC的方法连接数据库。