Java read file stored to MySQL

Source: Internet
Author: User
Tags lua stmt

Tag: Direct user value substr Manager int IMP Red

Wrote a batch of Lua script, to put in the database, the call when the calculation, because there are too many Lua script, wrote a program to input.

The following are mainly divided into three parts:

 Public Static string readtostring (string fileName)  Public Static void throws FileNotFoundException  Public Static List<string> getfilelist (String strpath)

Readtostring, which is used to read a script from a LUA script, which is a file, sends a parameter that is the absolute path to the folder.

Writetomysql primarily for database write functions, the main function also calls this function, which means that the function calls the other two functions.

Getfilelist, traverse the folder of the specified path, search through all LUA scripts, and process the absolute path of the Lua script (plus \, because the Java processing absolute path needs to be added) to a list.

Writetomysql originally intended to pass a parameter as a path in, think about it once, directly in the inside to write dead absolute path.

The complete code:

ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;Importjava.io.IOException;Importjava.io.UnsupportedEncodingException;Importjava.sql.Connection;ImportJava.sql.DriverManager;Importjava.sql.SQLException;Importjava.sql.Statement;Importjava.util.LinkedList;Importjava.util.List;Importcom.mysql.jdbc.PreparedStatement; Public classTest { Public Static voidMain (string[] args)throwsIOException {wirtetomysql (""); }     Public Staticstring readtostring (String fileName) {string encoding= "UTF-8"; File File=NewFile (fileName); Long Filelength=file.length (); byte[] Filecontent =New byte[Filelength.intvalue ()]; Try{FileInputStream in=Newfileinputstream (file);            In.read (filecontent);        In.close (); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }        Try {            return NewString (filecontent, encoding); } Catch(unsupportedencodingexception e) {System.err.println ("The OS does not support" +encoding);            E.printstacktrace (); return NULL; }    }     Public Static voidWirtetomysql (String string)throwsFileNotFoundException {FinalString jdbc_driver = "Com.mysql.jdbc.Driver"; FinalString Db_url = "JDBC:MYSQL://192.168.**.***:3306/CQC"; FinalString USER = "* * *"; FinalString PASS = "* * * *"; Connection Conn=NULL; Statement stmt=NULL; List<String> id = getfilelist ("C:\\users\\yefeifan\\desktop\\lua"); List<String> fileList =NewLinkedlist<>();  for(inti = 0; I < id.size (); i++) {Filelist.add (Id.get (i)); }        //get the file name list that is ID list c001002000_001         for(inti = 0; I < id.size (); i++) {Id.set (I, Id.get (i). substring (35, 49)); }        //get the script contentlist<string> scripts =NewLinkedlist<>();  for(inti = 0; I < filelist.size (); i++) {            //System.out.println (readtostring (Filelist.get (i)));Scripts.add (readtostring (Filelist.get (i))); }        Try{class.forname ("Com.mysql.jdbc.Driver"); System.out.println ("Connect Database ..."); Conn=drivermanager.getconnection (Db_url, USER, PASS);            PreparedStatement pstmt; //Execute Query             for(inti = 0; I < scripts.size (); i++) {String SQL= "Update capability set function_script= '" + scripts.get (i) + "' Where id= '" +Id.get (i)+ "'"; Pstmt=(PreparedStatement) conn.preparestatement (SQL);                Pstmt.executeupdate ();            Pstmt.close ();        } conn.close (); } Catch(SQLException se) {//Handling JDBC ErrorsSe.printstacktrace (); } Catch(Exception e) {//Handling Class.forName ErrorsE.printstacktrace (); } finally {            //Close Resource            Try {                if(stmt! =NULL) Stmt.close (); } Catch(SQLException se2) {}//don't do anything.            Try {                if(Conn! =NULL) Conn.close (); } Catch(SQLException se) {se.printstacktrace (); }        }    }     Public StaticList<string>getfilelist (String strpath) {List<String> filelist =NewLinkedlist<string>(); File dir=NewFile (strpath); file[] Files= Dir.listfiles ();//all the files in the file directory are put into the array        if(Files! =NULL) {             for(inti = 0; i < files.length; i++) {String fileName=Files[i].getname (); if(Files[i].isdirectory ()) {//decide if it's a file or a folder                    return NULL;//get file absolute path}Else if(Filename.endswith ("Lua")) {//determine if the file name ends with an. aviStringBuffer strFileName =NewStringBuffer (Files[i].getabsolutepath ()); Strfilename.insert (2, ' \ \ '); Strfilename.insert (9, ' \ \ '); Strfilename.insert (19, ' \ \ '); Strfilename.insert (28, ' \ \ '); Strfilename.insert (33, ' \ \ ');                Filelist.add (Strfilename.tostring ()); } Else {                    Continue; }            }        }        returnfilelist; }}

Java read file stored in MySQL

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.