Requirements: Other items, code buckle over, the database is also taken over, but the database is empty table, a total of 700 tables, the demand is to add false data to the table, so that it can run.
This is the code implementation:
1. Database connection:
Public StaticConnection getconnection () {Connection conn=NULL; Try{class.forname ("Com.mysql.jdbc.Driver"); String URL= "Jdbc:mysql://192.168.0.12:3306/dmp_report_testdb?"; String User= "Root"; String Pass= "Ycmedia_2015"; Conn=drivermanager.getconnection (Url,user,pass); } Catch(ClassNotFoundException e) {e.printstacktrace (); } Catch(SQLException e) {e.printstacktrace (); } returnConn; }
2. Get all the table names:
Public StaticSet<string> Gettablename ()throwssqlexception{Connection Conn=getconnection (); DatabaseMetaData Data=Conn.getmetadata (); ResultSet Colret= Data.getcolumns (NULL,"%", "%","%"); Set<String> set=NewTreeset<string>(); while(Colret.next ()) {String TypeName=colret.getstring ("table_name"); Set.add (TypeName); } returnset; }
3. Get the data type of the table, MySQL has dozens of kinds, commonly used about more than 10 kinds:
* @return* @throwsSQLException*/ Public StaticSet<string> GetType ()throwssqlexception{Connection Conn=getconnection (); DatabaseMetaData Data=Conn.getmetadata (); ResultSet Colret= Data.getcolumns (NULL,"%", "%","%"); Set<String> set=NewTreeset<string>(); while(Colret.next ()) {String TypeName=colret.getstring ("Type_name"); Set.add (TypeName); } returnset; }
4 Categories:
/** * BIGINT BIGINT UNSIGNED (no symbols) 1-10 int int UNSIGNED SMALLINT SMALLINT UNSIGNED TINYINT TINYINT UNSIGNED BIT DATE last week DATETIME TIMESTAMP (last week) DECIMAL 11.11 DOUBLE 11.11 longtext mediumtext TEXT VARCHAR
5 String type Handling
//Random String Public StaticString getrandomstring (intlength) {String str= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; Random Random=NewRandom (); StringBuffer SB=NewStringBuffer (); for(inti=0;i<length;i++){ intNumber=random.nextint (62); Sb.append (Str.charat (number)); } returnsb.tostring (); }
6. Time Type Processing
/*** Get a random day for the last one months *@return */ Public StaticString Getmoth (Integer type) {Random R=NewRandom (); SimpleDateFormat SDF=NULL; if(type==1) {SDF=NewSimpleDateFormat ("Yyyy-mm-dd"); }Else{SDF=NewSimpleDateFormat ("Yyyy-mm-dd 00:00:00"); } Calendar C=calendar.getinstance (); C.add (Calendar.date,-R.nextint (30)); Date Monday=C.gettime (); returnSdf.format (Monday); }
7. Get the SQL
/*** Get SQL *@paramlist *@paramTableName *@return */ Public StaticString GetSQL (list<clumnbean>list,string TableName) {StringBuffer SB=NewStringBuffer (); Random Random=NewRandom (); Sb.append ("INSERT into" +tablename + "values" ("); for(inti = 0; I < list.size (); i++) { if(List.get (i). Getcolumntype (). Contains ("INT") {sb.append (Random.nextint (10) + ","); } Else if(List.get (i). Getcolumntype (). Contains ("CHAR") | | List.get (i). Getcolumntype (). Contains ("TEXT") {sb.append ("'" +getrandomstring (5) + "',"); } Else if(List.get (i). Getcolumntype (). Equals ("BIT") {sb.append ("10,"); } Else if(List.get (i). Getcolumntype (). Equals ("DOUBLE") | | List.get (i). Getcolumntype (). Equals ("DECIMAL") {sb.append ("11.11,"); } Else if(List.get (i). Getcolumntype (). Equals ("DATETIME") | | List.get (i). Getcolumntype (). Equals ("TIMESTAMP") {sb.append ("'" +getmoth (2) + "',"); }Else if(List.get (i). Getcolumntype (). Equals ("DATE") {sb.append ("'" +getmoth (1) + "',"); } } returnSb.tostring (). substring (0, sb.tostring (). Length ()-2) + ")"; }
8. The final step, the main branch
Public Static voidMain (string[] args)throwsexception{Connection Conn=getconnection (); Set<String> set =Gettablename (); List<ClumnBean> list =NewArraylist<clumnbean>(); for(String str:set) {list=getcolumnlist (str); for(inti = 0; I < 20; i++) {String SQL=GetSQL (LIST,STR); SYSTEM.ERR.PRINTLN (SQL); Try{conn.preparestatement (SQL). Execute (); } Catch(Exception e) {System.err.println ("Encountered exception"); Continue; }} List=NewArraylist<clumnbean>(); } }
===================================================== Split Line ======================================================= ==============
Of course, this scene is very rare, most of the projects are not more than 100 tables, dozens of of the most, the actual development, and the data here are more chaotic, because many tables are related, just roughly inserted table, when the part of the run can be artificially modified that piece, than their own slowly plug the data better
java+ MySQL add fake data to all tables