博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java 从Access数据库读取数据,插入到JavaDB数据库表中
阅读量:4030 次
发布时间:2019-05-24

本文共 2028 字,大约阅读时间需要 6 分钟。

一段代码,实现的功能是从Access数据库读取表数据,然后依次全部插入到JavaDB数据库表中,类似于做拷贝.

       import java.sql.*;

        String sql_insert =null;//将sql_insert定义成全局变量,是为了出错时,在catch部分能打印sql_insert内容,有助于调试.
        
        ResultSet rs = null;
        try {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    
    String url = "jdbc:odbc:Driver={Microsoft Access Driver " +
            "(*.mdb, *.accdb)};DBQ=path to  database\\Database1.accdb";
//红色处应填入实际地址
        Connection con = DriverManager.getConnection(url);
        System.out.println("Connected!");
        
            Statement stmt = null;
            
 
           // SQL query command
            String SQL = "SELECT * FROM data";
            stmt = con.createStatement();
            //stmt.execute(SQL);
            
            rs = stmt.executeQuery(SQL);
 
          
           
String driver = "org.apache.derby.jdbc.ClientDriver";//在derby.jar里面
        //String dbName="EmbeddedDB";
       String dbURL = "jdbc:derby://localhost:1527/PassWords;territory=zh_CN;user=admin;password=123456"
;// create=true表示当数据库不存在时就创建它        
       try {          
            Class.forName(driver);
            Connection conn = DriverManager.getConnection(dbURL);//启动嵌入式数据库
            Statement st = conn.createStatement();
                        
            while(rs.next()){
                byte[] bts=null;
                String des = null;
                String usname = null;
                 String psw = null;
                String tips = null;
                String other = null;
                
                String ID = new String(rs.getBytes("ID"),"gbk");
                int id = Integer.parseInt(ID);
                
                bts = rs.getBytes("DESCRIPTION");
                if(bts!=null )
                    des = new String(bts,"gbk");
                
                bts = rs.getBytes("USERNAME");
                if(bts !=null)
                    usname = new String(bts,"gbk");
                
                bts = rs.getBytes("PASSWORD");
                if(bts !=null)
                    psw = new String(bts,"gbk");
               
                bts = rs.getBytes("TIPS");
                if(bts !=null)
                    tips = new String(bts,"gbk");
               
                
                bts = rs.getBytes("OTHERS");
                if(bts != null)
                    other =  new String(bts,"gbk");
                
                sql_insert = "insert into APP.PSW(ID,DESCRIPTION,USERNAME,PASSWORD,TIPS,OTHERS) VALUES
("+id+",'"+des+"','"+usname+"','"+psw+"','"+tips+"','"+other+"')";
            st.executeUpdate(sql_insert);//插入一条数据
                
            }
        } catch(Exception e){
            System.out.println(sql_insert);
            e.printStackTrace();
            
        }
                    
            
        con.close();
        } catch (SQLException e) {
            System.out.println("SQL Exception: "+ e.toString());
        } catch (ClassNotFoundException cE) {
            System.out.println("Class Not Found Exception: "+
                cE.toString());
        }
       

转载地址:http://soqbi.baihongyu.com/

你可能感兴趣的文章
CCF 分蛋糕
查看>>
解决python2.7中UnicodeEncodeError
查看>>
小谈python 输出
查看>>
Django objects.all()、objects.get()与objects.filter()之间的区别介绍
查看>>
python:如何将excel文件转化成CSV格式
查看>>
Django 的Error: [Errno 10013]错误
查看>>
机器学习实战之决策树(一)
查看>>
[LeetCode By Python] 2 Add Two Number
查看>>
python 中的 if __name__=='__main__' 作用
查看>>
机器学习实战之决策树二
查看>>
[LeetCode By Python]7 Reverse Integer
查看>>
[LeetCode By Python]9. Palindrome Number
查看>>
[leetCode By Python] 14. Longest Common Prefix
查看>>
[LeetCode By Python]107. Binary Tree Level Order Traversal II
查看>>
[LeetCode By Python]108. Convert Sorted Array to Binary Search Tree
查看>>
[leetCode By Python]111. Minimum Depth of Binary Tree
查看>>
[LeetCode By Python]118. Pascal's Triangle
查看>>
[LeetCode By Python]121. Best Time to Buy and Sell Stock
查看>>
[LeetCode By Python]122. Best Time to Buy and Sell Stock II
查看>>
[LeetCode By Python]125. Valid Palindrome
查看>>