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 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能