乖,別哭的薄殼
          ~一份耕耘,一份收獲~
          posts - 23,comments - 260,trackbacks - 0

          一個簡單的用jdbc操作數(shù)據(jù)庫的例子,有時候我們處理一些小問題的時候會發(fā)現(xiàn)很有用.

          這是用來從一個Access的數(shù)據(jù)庫文件area.mdb(一個全國省份城市的數(shù)據(jù)庫)中提取出我需要的信息到MS SQLServer 2000數(shù)據(jù)庫里的例子.

          package com.test;

          import java.io.IOException;
          import java.sql.Connection;
          import java.sql.DriverManager;
          import java.sql.PreparedStatement;
          import java.sql.ResultSet;
          import java.sql.SQLException;
          import java.sql.Statement;

          /**
          ?*
          ?* CopyRight (C) http://www.aygfsteel.com/ilovezmh? All rights reserved.<p>
          ?*
          ?* WuHan Inpoint Information Technology Development,Inc.<p>
          ?*
          ?* Author zhu<p>
          ?*
          ?* @version 1.0??? 2007-1-17
          ?*
          ?* <p>Base on : JDK1.5<p>
          ?*
          ?*/

          public class City {
          ?
          ?static String driver1="sun.jdbc.odbc.JdbcOdbcDriver";
          ?static String driver2="net.sourceforge.jtds.jdbc.Driver";
          ?static String url1="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=D:\\TDdownload\\area\\area.mdb";
          ?static String url2="jdbc:jtds:sqlserver://localhost:1433/test;SelectMethod=cursor;characterEncoding=GBK";
          ?
          ?public static void main(String arg[]) throws IOException,SQLException{
          ??
          ??
          ??Connection conn1=null;
          ??Connection conn2=null;
          ??Statement ps1=null;
          ??//Statement ps2=null;
          ??ResultSet rs1=null;
          ??//ResultSet rs2=null;
          ??String sql1=null;
          ??String sql2=null;
          ??PreparedStatement pstmt =null;
          ??
          ??try{
          ???Class.forName(driver1);
          ???Class.forName(driver2);
          ???conn1 = DriverManager.getConnection(url1,"","");
          ???conn2= DriverManager.getConnection(url2,"sa","sa");
          ???ps1 = conn1.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
          ???//ps2 = conn2.createStatement();
          ??}
          ??catch(ClassNotFoundException e){
          ???System.out.print(e);
          ??}
          ??catch (SQLException e) {
          ???// TODO 自動生成 catch 塊
          ???e.printStackTrace();
          ??}
          ??
          ??try{???
          ???sql1="select * from area";
          ???rs1 = ps1.executeQuery(sql1);
          ???sql2 = "insert into tbcity(code,name,parentid,type) values (?,?,?,?)";
          ???pstmt=conn2.prepareStatement(sql2);
          ???
          ???int code=0;
          ???int parentid=0;
          ???String name=new String();
          ???while(rs1.next()){????
          ????code=rs1.getInt(2);
          ????name=rs1.getString(3);
          ????parentid=rs1.getInt(4);
          ????//sql2="insert into TBCITY(code,name,parentid,type) values ("+code+",'"+name+"',"+parentid+",3)";
          ????//ps2.executeUpdate(sql2);
          ????pstmt.setInt(1,code);
          ????pstmt.setString(2, name);
          ????pstmt.setInt(3,parentid);
          ????pstmt.setInt(4, 3);
          ????pstmt.addBatch();
          ???}???
          ???pstmt.executeBatch(); ???
          ?????
          ???System.out.println("轉(zhuǎn)換完成!謝謝使用");
          ???ps1.close();
          ???//ps2.close();
          ???pstmt.close();
          ???conn1.close();
          ???conn2.close();
          ??}
          ??catch(Exception e){
          ???System.out.print(e);
          ??}
          ??
          ?}
          ?
          }

          posted on 2007-02-01 14:09 小祝 閱讀(3297) 評論(9)  編輯  收藏 所屬分類: java技術(shù)

          FeedBack:
          # re: 一個用jdbc操作數(shù)據(jù)庫的簡單例子
          2007-02-01 22:40 | 施偉
          支持支持!  回復(fù)  更多評論
            
          # re: 一個用jdbc操作數(shù)據(jù)庫的簡單例子
          2007-02-02 11:24 | 梅穎
          你占了我的位置。。。嗚嗚嗚  回復(fù)  更多評論
            
          # re: 一個用jdbc操作數(shù)據(jù)庫的簡單例子
          2007-02-02 13:05 | 小祝
          ...原來在搶位置啊.  回復(fù)  更多評論
            
          # re: 用jdbc操作數(shù)據(jù)庫的簡單例子
          2007-02-05 19:51 | 睿不可當(dāng)
          不錯
          我想能不能結(jié)合你上一篇
          java讀配置文件(xml、property)的簡單例子
          把連接的信息寫在配置文件(xml、property)中就完美勒  回復(fù)  更多評論
            
          # re: 用jdbc操作數(shù)據(jù)庫的簡單例子
          2007-02-05 21:28 | 小祝
          謝謝支持和建議啊!
          我是這么想的,分開來講算是不同的知識點,合到一起的話有些不太好,我想別人也不會寫這樣的配置文件去連數(shù)據(jù)庫的,現(xiàn)在已經(jīng)有很方便的東西了。呵呵。
          關(guān)于事務(wù)的問題,第一個想法和我先想的差不多,好像是可以,但覺得會有更好的方法。
          至于借助于其它的框架來做事務(wù)當(dāng)然很方便,不過這本身就是一個很簡單的東西。再把框架拿起來的話就失去了他的意義,對吧。  回復(fù)  更多評論
            
          # re: 用jdbc操作數(shù)據(jù)庫的簡單例子
          2007-02-05 22:22 | 施偉
          JDBC是基礎(chǔ),務(wù)必通,透,精  回復(fù)  更多評論
            
          # re: 用jdbc操作數(shù)據(jù)庫的簡單例子
          2007-02-06 11:42 | 睿不可當(dāng)
          今天上午我突然想到你的這個例子有種想法
          對于你的第二個作操是否能批量執(zhí)行呢來提高效率
          sql2="insert into TBCITY(code,name,parentid,type) values ("+code+",'"+name+"',"+parentid+",3)";
          ps2.executeUpdate(sql2);//插入到表tbcity中
          jdbc3個接口用來處理sql的執(zhí)行,是Statement PreparedStatement CallableStatement
          提供適當(dāng)?shù)腟tatement接口批量執(zhí)行sql從數(shù)據(jù)庫批量獲取數(shù)據(jù)
          PreparedStatement 比Statement性能要好
          主要體現(xiàn)在一個sql語句多次重復(fù)執(zhí)行的情況
          PreparedStatemnt只編譯解析一次而Statement每次編譯一次.
          批量修改數(shù)據(jù)庫
          Statement 提供了方法addBatch(String)和executeBatch()
          調(diào)用方法為
          stmt.addBatch("insert.....");
          stmt.addBatch("update.....")
          stmt.executeBatch();
          也可以用PreparedStatement從而更好的提高性能.
          pstmt=conn.preparedStatement("insert into tbcity(......) values(....?)");
          pstmt.setString(1,"aaa");
          ...
          pstmt.addBatch();
          pstmt.setString(1,"bbb");
          ...
          pstmt.addBatch();
          .....
          pstmt.executeBatch();  回復(fù)  更多評論
            
          # re: 用jdbc操作數(shù)據(jù)庫的簡單例子
          2007-02-06 13:06 | 小祝
          @睿不可當(dāng)
          嗯,我按你說的這種方式改進(jìn)了下,速度還不錯。thank you!  回復(fù)  更多評論
            
          # re: 用jdbc操作數(shù)據(jù)庫的簡單例子
          2007-02-07 10:48 | 梅穎
          這個風(fēng)格還滿好看的,呵呵,向左走,向右走。。  回復(fù)  更多評論
            
          主站蜘蛛池模板: 天祝| 耿马| 内黄县| 昂仁县| 乌兰浩特市| 措勤县| 吴旗县| 稻城县| 乐业县| 河北省| 徐汇区| 揭阳市| 承德市| 额尔古纳市| 安龙县| 枣强县| 磐安县| 苏州市| 磐石市| 临西县| 甘泉县| 习水县| 濮阳县| 怀远县| 永修县| 新乐市| 梧州市| 平湖市| 买车| 诸城市| 格尔木市| 秭归县| 台南市| 临清市| 成安县| 莱西市| 泸定县| 阜平县| 新民市| 当涂县| 松溪县|