iavere 2011-05-09 14:30
浏览 244
已采纳

java中文件读取文件操作数据库

java中文件读取文件操作数据库 给个例子 谢谢

  • 写回答

1条回答 默认 最新

  • iteye_11009 2011-05-09 14:35
    关注

    连接数据库:
    public class DBConnect {

    public Connection getConnect(String driver, String url, String user, String pwd) {
        Connection conn = null;
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url, user, pwd);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
    
    public void closeConnect(Connection conn) {
        if(conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    

    }
    SqlExecutor类:
    public class SqlExecutor {

    private Connection conn;
    private Statement stmt;
    private DBConnect db = new DBConnect();
    
    public int[] exeBatch(String driver, String url, String user, String pwd, String[] sqls) {
        int[] result = new int[sqls.length];
        conn = db.getConnect(driver, url, user, pwd);
        try {
            stmt = conn.createStatement();
            for (int i = 0; i < sqls.length; i++) {
                if (sqls[i].endsWith(";"))
                    stmt.addBatch(sqls[i]);
            }
            result = stmt.executeBatch();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            db.closeConnect(conn);
        }
        return result;
    }
    

    }

    读取方法
    public String readFile(String filePath) {
    String doc = null;
    File file = new File(filePath);
    if (file == null) {
    return null;
    }
    try {
    // 用文件输入流构建输入流
    InputStream is = new BufferedInputStream(new FileInputStream(file));
    BufferedReader bReader = new BufferedReader(new InputStreamReader(
    is, "utf-8"));
    StringBuilder sb = new StringBuilder();
    String rLine = null;
    while ((rLine = bReader.readLine()) != null) {
    String tmp_rLine = rLine;
    int str_len = tmp_rLine.length();
    if (str_len > 0) {
    sb.append(tmp_rLine + " ");
    }
    tmp_rLine = null;
    }
    doc = sb.toString();
    bReader.close();
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return doc;
    }

    测试类:
    public class Test {

    /**
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
    
        FileUtil util = new FileUtil();
        String sqlPath = "g:/db.sql";
        String[] sqls = util.readSqlFile(sqlPath);
        for (String s : sqls) {
            System.out.println(s);
        }
        // 数据库生成测试
        String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://192.168.37.111:3306/mysql?useunicode=true&amp;characterEncoding=utf-8";
        String username = "root";
        String pwd = "ipanel";
        SqlExecutor creator = new SqlExecutor();
        System.out.println(creator.exeBatch(driver, url, username, pwd, sqls));
    
    }
    

    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况