问题描述
代码如下:publicclassWriteToAccess{publicvoidsaveFamilyInfo(List<Family>list){StringJDriver="sun.jdbc.odbc.JdbcOdbcDriver";StringconnectDB="jdbc:odbc:test";//StringconnectDB="jdbc:odbc:driver={MicrosoftAccessDriver(*.mdb,*.accdb)};DBQ="+"D://access_test//Output_data.accdb";try{Class.forName(JDriver);}catch(java.lang.ClassNotFoundExceptione){System.out.println("ForName:"+e.getMessage());}try{Connectioncon=DriverManager.getConnection(connectDB,"","");//此处报错Statementstmt=con.createStatement();Stringquery=null;for(inti=0;i<list.size();i++){Familyfamily=list.get(i);StringfamilyId=family.getFamilyId();IntegervillageCommittee=family.getVillageCommittee().getVillageCommitteeId();Integerprovince=family.getProvince().getProvinceId();Integercounty=family.getCounty().getCountyId();IntegerfamilySize=family.getFamilySize();Integerincome=family.getIncome();Integeruser=family.getUser().getUserID();StringcreateTime=family.getCreateTime();query="insertinto家庭信息表(familyId,familySize,income,createTime,province,villageCommittee,county,user)values('"+familyId+"',"+familySize+","+income+",'"+createTime+"',"+province+","+villageCommittee+","+county+","+user+")";stmt.executeUpdate(query);}stmt.close();con.close();}catch(SQLExceptione){System.out.println("SQLException:"+e.getMessage());}}}
报错如下:在线等,谢谢
解决方案
解决方案二:
我单独写了个程序importjava.sql.*;importsun.jdbc.odbc.*;publicclassCreateTable02_4_2{publicstaticvoidmain(String[]args){StringJDriver="sun.jdbc.odbc.JdbcOdbcDriver";StringconnectDB="jdbc:odbc:Output_data";//StringconnectDB="jdbc:odbc:driver={MicrosoftAccessDriver(*.mdb,*.accdb)};DBQ="+"D://access_test//Book.accdb";try{Class.forName(JDriver);}catch(java.lang.ClassNotFoundExceptione){System.out.println("ForName:"+e.getMessage());}try{Connectioncon=DriverManager.getConnection(connectDB);Statementstmt=con.createStatement();Stringquery="createtable书目_by_java("+"编号Text(20),"+"书名Text(20),"+"作者Text(10),"+"书价Number"+")";stmt.executeUpdate(query);stmt.close();con.close();}catch(SQLExceptione){System.out.println("SQLException:"+e.getMessage());}}}
运行这个小程序的时候是没错误的