一个简单的批处理题目

有一个超过10w行的文本文件,每一行的格式为

学号,姓名,班级,课程,成绩

解析每一行文件,并采用jdbc批处理方式,尽可能高效的将其全部数据写入数据库表中。

3、将上题中,数据库表中的数据导出为以下格式到一个文本文件:

+----------------------------------------------------------------------------------------------+

| 学号       |     姓名      |     班级     |    课程    |  成绩   |

+----------------------------------------------------------------------------------------------+

| SN0001     |     张三      |     一班     |    数学    |  100    |

| SN0002     |     李四      |     一班     |    数学    |  100    |

+----------------------------------------------------------------------------------------------+

 

       这个题,用到了简单的流输入输出操作,以及jdbc的批处理操作,这是我来公司第一周学习完的练手项目,我自己的java学的很垃圾,但是还是勉强写了出来,愁死我了,这几天睡觉梦到穿衣服都在想这个操作是堆栈还是队列。

 

       题目的思路是这个样子的,首先,从文本中读出一行内容,对内容进行解析,解析我用的是java的String.split,把一行按,分开五个String数组,然后直接写jdbc的流操作,把这一行提交到数据库,然后再进行下一行。(其实之前我想的是先写个方法,把都出来的String放在一个字符数组里,返回然后再调用,太麻烦。)

       这个是主要的问题所在,然后的从数据库按格式输出的话,就直接查询全数据库然后按格式输出就好。

@Test                                         
    public void add1() throws IOException {
        
        String url = "jdbc:oracle:thin:@localhost:1521:orcl";
        String driver = "oracle.jdbc.driver.OracleDriver";
        String username = "abistudy";
        String password = "abistudy";

        SimpleConnectionFactory fct = new SimpleConnectionFactory(driver, url, username, password, "debug");
        //连接数据库的基本操作,然后这里可以将上面的文本编辑成一个配置文件,节约时间。
        String[] st1 = new String[100];      定义一个字符数组
        System.out.println("test add");      为了看看程序运行过来没,测试用。
        String tableName = "test";            规范化,定义表名,之后要修改表的话也只要改这里就好

        Vfs2 vfs = svr.getVfs();                用了vfs的读取操作,因为要按行读取,这个vfs只学了一个存储和读取,有时间要再看看。
        VfsFile2 file = vfs.getVfsFile("/big.txt", svr.getVfsOperatorAsAdmin());    指定读取文件。
        InputStream input = file.getInputStream();        流操作定义

        try {                                                                    批处理的基本流程
            Connection conn = fct.getConnection();
            try {
                PreparedStatement ps = null;                   prepared批处理方式
                try {
                    String sql = "insert into " + tableName + " (id,name,class,subject,score) values(?,?,?,?,?)";
                    ps = conn.prepareStatement(sql);
                    try {
                        for (int i = 1; i <= 100000; i++) {
                            st1[i] = StmFunc.readLine(input, StrFunc.GBK);               读取一行文字。
                            String[] st2 = st1[i].split(",");                                               按,号解析字符
                            
                            ps.setString(1, st2[0]);                                                        对应sql语句的问号处,输入内容·
                            ps.setString(2, st2[1]);
                            ps.setString(3, st2[2]);
                            ps.setString(4, st2[3]);
                            ps.setString(5, st2[4]);
                            ps.addBatch();                                                                 
                            if (i % 2 == 0) {
                                ps.executeBatch();                                                               提交操作
                                ps.clearBatch();
                            }
                        }
                        ps.executeBatch();
                    } finally {                                                                                   关闭进程


                        input.close();
                    }
                } finally {
                    ps.close();
                }
            } finally {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

然后从数据库读出操作。

@Test
    public void testSelect() throws Exception  {
        String url = "jdbc:oracle:thin:@localhost:1521:orcl";
        String driver = "oracle.jdbc.driver.OracleDriver";
        String username = "abistudy";
        String password = "abistudy";

        SimpleConnectionFactory fct = new SimpleConnectionFactory(driver, url, username, password, "debug");
        
        System.out.println("test select");
        String tableName="test";
        String biaotou = "|学号\t|\t姓名\t|\t班级\t|\t课程\t|\t成绩\t|";
        File file=new File("D:\\test\\2.txt");
        FileOutputStream output = new FileOutputStream(file);
        StmFunc.writeLine(output, biaotou,StrFunc.UTF8);                                      //相当于输出一句,并指定编码,自动回车。
        try {
        Connection conn = fct.getConnection();
            try {
                String sql = "select * from "+tableName;
                
                Statement    stmt = conn.createStatement();
                try {
                ResultSet rs=stmt.executeQuery(sql);
                    try {
                        while(rs.next()) {
                        String shuchu="|"+rs.getString(1)+"\t|\t"+rs.getString(2)+"\t|\t"+rs.getString(3)
                                                                                                    +"\t|\t"+rs.getString(4)+"\t|\t"+rs.getString(5)+"\t|";
                        StmFunc.writeLine(output, shuchu,StrFunc.UTF8);
                        }
                    }finally {
                        rs.close();
                    }
                }finally {
                    stmt.close();
                }
            }finally {
        conn.close();
            }
    }catch(SQLException e) {
        e.printStackTrace();
    }
    }

       打完收工,最后结果截图如下:

    

 

 

      

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值