JAVA如何访问ACCESS数据库

JAVA如何访问ACCESS数据库,第1张

所以首先需要进行数据源的配置工作: 创建ODBC过程: 控制面板-->管理工具--〉数据源。 选择“系统DSN”--〉“添加” 选择“driver do Microsoft Access”,点击“完成” 给数据源起个名字,例如accessTest 点击“选择”,选择你的数据库文件即可。 这样就配置了一个数据源。 下面是访问数据库的一个例子: package comhfaccessTest; import javasqlConnection; import javasqlDriverManager; import javasqlPreparedStatement; import javasqlResultSet; import javasqlSQLException; public class ConnectionManager { static { try { ClassforName("sunjdbcodbcJdbcOdbcDriver"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block eprintStackTrace(); } } public static Connection getConnection(){ Connection con=null; //String url = "jdbc:odbc:driver={Microsoft Access Driver(mdb)};DBQ=f:\\testmdb"; String url=new String("jdbc:odbc:accessTest");//test時資料來源 try { con= DriverManagergetConnection(url); } catch (SQLException e) { // TODO Auto-generated catch block eprintStackTrace(); } return con; } / @param args/public static void main(String[] args) {//得到连接Connection con = ConnectionManagergetConnection();try {PreparedStatement st = conprepareStatement("select id,name from test1 ");ResultSet rs = stexecuteQuery();while (rsnext()){String id");String name");Systemoutprintln("id:"+id+" name: "+name );}rsclose();stclose();

1增加\x0d\String s1="insert into tableNames (id,name,password) values(myseqnextval,,);"\x0d\ClassforName(driver);\x0d\Connection conn = DriverManagergetConnection(url,dbUser,dbPwd);\x0d\PreparedStatement prepStmt = connprepareStatement(s1);\x0d\prepStmtsetString(1,name);\x0d\prepStmtsetString(2,password);\x0d\ResultSet rs=stmtexecuteUpdate();\x0d\2、删除\x0d\String s2="delete from tbNames where name=";\x0d\ClassforName(driver);\x0d\Connection conn = DriverManagergetConnection(url,dbUser,dbPwd);\x0d\PreparedStatement prepStmt = connprepareStatement(s2);\x0d\prepStmtsetString(1,name);\x0d\ResultSet rs=stmtexecuteUpdate();\x0d\3、修改\x0d\String s3=“update tbNames set name= where id=”;\x0d\ClassforName(driver);\x0d\Connection conn = DriverManagergetConnection(url,dbUser,dbPwd);\x0d\PreparedStatement prepStmt = connprepareStatement(s3);\x0d\prepStmtsetString(1,name);\x0d\prepStmtsetString(2,id);\x0d\ResultSet rs=stmtexecuteUpdate();\x0d\4、查询\x0d\String s4="select id,name,password from tbNames";\x0d\ClassforName(driver);\x0d\Connection conn = DriverManagergetConnection(url,dbUser,dbPwd);\x0d\Statement stmt=conncreateStatement();\x0d\ResultSet rs = stmtexecuteQuery(s4);\x0d\while(rsnext){\x0d\int id=rsgetInt(1);\x0d\String name = rsgetString(2);\x0d\String pwd=rsgetString(3);\x0d\Systemoutprintln(id+name+pwd); } \x0d\\x0d\以上四步必须都得关闭连接;!!!\x0d\rsclose();\x0d\stmtclose();\x0d\connclose();

//将数据库中的数据条数分段 public void division(){ //获取要导入的总的数据条数 String sql3="SELECT count() FROM [CMD][dbo][mycopy1]"; try { pss=consprepareStatement(sql3); rss=pssexecuteQuery(); while(rssnext()){ Systemoutprintln("总记录条数:"+rssgetInt(1)); sum=rssgetInt(1); } //每30000条记录作为一个分割点 if(sum>=30000){ n=sum/30000; residue=sum%30000; }else{ residue=sum; } Systemoutprintln(n+" "+residue); } catch (SQLException e) { // TODO Auto-generated catch block eprintStackTrace(); } }线程类public MyThread(int start,int end) { thisend=end; thisstart=start; Systemoutprintln("处理掉余数"); try { Systemoutprintln("--------"+ThreadcurrentThread()getName()+"------------"); ClassforName(SQLSERVERDRIVER); Systemoutprintln("加载sqlserver驱动"); cons = DriverManagergetConnection(CONTENTS,UNS,UPS); stas = conscreateStatement(); Systemoutprintln("连接SQLServer数据库成功!!"); Systemoutprintln("加载mysql驱动"); ClassforName(MYSQLDRIVER); con = DriverManagergetConnection(CONTENT,UN,UP); sta = concreateStatement(); // 关闭事务自动提交 consetAutoCommit(false); Systemoutprintln("连接mysql数据库成功!!"); } catch (Exception e) { eprintStackTrace(); } // TODO Auto-generated constructor stub } public ArrayList<Member> getAll(){ Member member; String sql1="select from (select row_number() over (order by pmcode) as rowNum," + " from [CMD][dbo][mycopy1]) as t where rowNum between "+start+" and "+end; try { Systemoutprintln("正在获取数据"); allmembers=new ArrayList(); rss=stasexecuteQuery(sql1); while(rssnext()){ member=new Member(); membersetAddress1(rssgetString("address1")); membersetBnpoints(rssgetString("bnpoints")); membersetDbno(rssgetString("dbno")); membersetExpiry(rssgetString("expiry")); membersetHispoints(rssgetString("hispoints")); membersetKypoints(rssgetString("kypoints")); membersetLevels(rssgetString("levels")); membersetNames(rssgetString("names")); membersetPmcode(rssgetString("pmcode")); membersetRemark(rssgetString("remark")); membersetSex(rssgetString("sex")); membersetTelephone(rssgetString("telephone")); membersetWxno(rssgetString("wxno")); membersetPmdate(rssgetString("pmdate")); allmembersadd(member); // Systemoutprintln(membergetNames()); } Systemoutprintln("成功获取sqlserver数据库数据!"); return allmembers; } catch (SQLException e) { // TODO Auto-generated catch block Systemoutprintln("获取sqlserver数据库数据发送异常!"); eprintStackTrace(); } try { rssclose(); stasclose(); } catch (SQLException e) { // TODO Auto-generated catch block eprintStackTrace(); } return null; } public void inputAll(ArrayList<Member> allmembers){ Systemoutprintln("开始向mysql中写入"); String sql2="insert into testmycopy2 values (,,,,,,,,,,,,,)"; try { ps=conprepareStatement(sql2); Systemoutprintln("-------------------------等待写入数据条数: "+allmemberssize()); for(int i=0;i<allmemberssize();i++){ pssetString(1, allmembersget(i)getPmcode()); pssetString(2, allmembersget(i)getNames()); //Systemoutprintln(allmembersget(i)getNames()); pssetString(3, allmembersget(i)getSex()); pssetString(4, allmembersget(i)getTelephone()); pssetString(5, allmembersget(i)getAddress1()); pssetString(6, allmembersget(i)getPmdate()); pssetString(7, allmembersget(i)getExpiry()); pssetString(8, allmembersget(i)getLevels()); pssetString(9, allmembersget(i)getDbno()); pssetString(10, allmembersget(i)getHispoints()); pssetString(11, allmembersget(i)getBnpoints()); pssetString(12, allmembersget(i)getKypoints()); pssetString(13, allmembersget(i)getWxno()); pssetString(14, allmembersget(i)getRemark()); //插入命令列表 //psaddBatch(); psexecuteUpdate(); } //psexecuteBatch(); concommit(); psclose(); conclose(); thisflag=false; Systemoutprintln(ThreadcurrentThread()getName()+"--->OK"); } catch (SQLException e) { // TODO Auto-generated catch block Systemoutprintln("向mysql中更新数据时发生异常!"); eprintStackTrace(); } } @Override public void run() { // TODO Auto-generated method stub while(true&&flag){ thisinputAll(getAll()); } }

场景:在家远程办公,使用***访问公司微服务, 浏览器能正常访问, 但java程序发起的请求不能

解决方法:加入java启动参数: -DjavanetpreferIPv4Stack=true

如果使用的是tomcat则在tomcat启动参数里面添加 -DjavanetpreferIPv4Stack=true

如果使用的是springboot则

>

以上就是关于JAVA如何访问ACCESS数据库全部的内容,包括:JAVA如何访问ACCESS数据库、怎样编写一个java程序能通过jdbc访问数据库实现对数据库的插入、删除、修改和查询、java中如何用多线程访问数据库等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/10194741.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-06
下一篇 2023-05-06

发表评论

登录后才能评论

评论列表(0条)

保存