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 閱讀(660) 評論(0)  編輯  收藏 所屬分類: SQL Server

          導航

          統計

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          友情鏈接

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 榆林市| 桃园县| 交口县| 明星| 开封县| 东莞市| 阳山县| 灵山县| 旌德县| 孙吴县| 土默特右旗| 富阳市| 阿瓦提县| 黄陵县| 项城市| 通州区| 定陶县| 昌图县| 鹿邑县| 湟源县| 讷河市| 通城县| 富平县| 呼伦贝尔市| 杭锦后旗| 德阳市| 南投县| 同仁县| 英吉沙县| 昌图县| 云林县| 阿克苏市| 共和县| 疏附县| 长武县| 建水县| 浦北县| 厦门市| 佛教| 大英县| 望奎县|