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)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          友情鏈接

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 南川市| 辽中县| 古丈县| 万宁市| 平谷区| 交口县| 蚌埠市| 荆州市| 黄山市| 仁化县| 静安区| 曲周县| 邻水| 霍邱县| 墨玉县| 菏泽市| 长海县| 蓬安县| 东港市| 富平县| 土默特左旗| 佳木斯市| 北宁市| 应城市| 久治县| 西青区| 肥西县| 南昌市| 美姑县| 金堂县| 盐山县| 逊克县| 顺昌县| 宁德市| 灌南县| 万荣县| 彰化市| 华坪县| 中牟县| 辛集市| 彝良县|