Jody

           

          Insert text file into MySQL

          import? java.io.File;
          import? java.io.FileInputStream;
          import? java.sql.Connection;
          import? java.sql.DriverManager;
          import? java.sql.PreparedStatement;

          public?class? InsertTextFileToMySQL? {

          ?? public?static? Connection?getConnection ()? throws? Exception? {
          ???? String?driver?=? "org.gjt.mm.mysql.Driver" ;
          ???? String?url?=? "jdbc:mysql://localhost/databaseName" ;
          ???? String?username?=? "root" ;
          ???? String?password?=? "root" ;

          ???? Class .forName ( driver ) ;
          ???? Connection?conn?=?DriverManager.getConnection ( url,?username,?password ) ;
          ???? return? conn;
          ?? }

          ?? public?static? void? main ( String []? args ) throws? Exception? {
          ???? String?id?=? "001" ;
          ???? String?fileName?=? "fileName.txt" ;
          ????
          ???? FileInputStream?fis?=? null ;
          ???? PreparedStatement?pstmt?=? null ;
          ???? Connection?conn?=? null ;
          ???? try? {
          ?????? conn?=?getConnection () ;
          ?????? conn.setAutoCommit ( false ) ;
          ?????? File?file?=? new? File ( fileName ) ;
          ?????? fis?=? new? FileInputStream ( file ) ;
          ?????? pstmt?=?conn.prepareStatement ( "insert?into?DataFiles(id,?fileName,?fileBody)?values?(?,??,??)" ) ;
          ?????? pstmt.setString ( 1 ,?id ) ;
          ?????? pstmt.setString ( 2 ,?fileName ) ;
          ?????? pstmt.setAsciiStream ( 3 ,?fis,? ( int )? file.length ()) ;
          ?????? pstmt.executeUpdate () ;
          ?????? conn.commit () ;
          ???? }? catch? ( Exception?e )?{
          ?????? System.err.println ( "Error:?"? +?e.getMessage ()) ;
          ?????? e.printStackTrace () ;
          ???? }? finally? {
          ?????? pstmt.close () ;
          ?????? fis.close () ;
          ?????? conn.close () ;
          ???? }
          ?? }
          }

          posted on 2006-09-13 11:33 zhaofei1394 閱讀(668) 評論(0)  編輯  收藏 所屬分類: SQL Server

          導航

          統計

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          友情鏈接

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 乐平市| 临湘市| 阿拉善盟| 达拉特旗| 达孜县| 扎鲁特旗| 开原市| 金塔县| 镇赉县| 车致| 湘阴县| 武威市| 莱州市| 南华县| 宣威市| 靖西县| 广安市| 罗定市| 临沭县| 清流县| 栾城县| 都江堰市| 富平县| 吴川市| 泊头市| 黄陵县| 应城市| 娱乐| 安顺市| 清徐县| 宕昌县| 冷水江市| 锡林郭勒盟| 张家口市| 京山县| 施秉县| 江华| 博客| 乌拉特前旗| 泸溪县| 惠州市|