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)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          友情鏈接

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 新巴尔虎右旗| 天峨县| 樟树市| 普安县| 乌拉特中旗| 辉南县| 澜沧| 崇礼县| 广丰县| 镇平县| 崇州市| 二手房| 界首市| 任丘市| 延川县| 蕲春县| 塔河县| 巩留县| 汤阴县| 尚义县| 宁强县| 监利县| 黄大仙区| 修武县| 木里| 临朐县| 临汾市| 芦溪县| 普宁市| 彰化市| 宝山区| 星子县| 大姚县| 伊宁市| 广西| 昌平区| 中超| 班戈县| 双牌县| 资源县| 衢州市|