Java initialization database read SQL configuration file build table

Source: Internet
Author: User
Tags stringbuffer

Create a Init.sql file in the SQL folder under Src/main/resources, and put the build table statement:

CREATE TABLE {DB}. ' Agent_status ' (
' Device_num ' varchar (+) not NULL,
' Start_time ' timestamp not NULL the DEFAULT current_timestamp on UPDATE current_timestamp,
' STATUS ' int (one) is not NULL,
' DURATION ' int (one) DEFAULT NULL,
' End_time ' timestamp null DEFAULT NULL,
' Call_type ' int (one) DEFAULT NULL,
' session_id ' varchar (255) DEFAULT NULL,
' SOURCE ' varchar (255) DEFAULT NULL,
' custom_num ' varchar DEFAULT NULL,
' Media_type ' int (one) DEFAULT NULL
) Engine=innodb DEFAULT Charset=utf8 checksum=1 delay_key_write=1 row_format=dynamic;

CREATE TABLE {DB}. ' Associate_data ' (
' SESSIONID ' varchar (255) is not NULL,
' Device_num ' varchar (255) DEFAULT NULL,
' Agent_name ' varchar (255) DEFAULT NULL,
' skill_group_id ' int (one) DEFAULT NULL,
' Skill_group_name ' varchar (255) DEFAULT NULL,
' Time ' timestamp not NULL for DEFAULT current_timestamp on UPDATE current_timestamp,
' SOURCE ' varchar (255) DEFAULT NULL,
' Call_type ' int (one) DEFAULT NULL,
' agent_id ' varchar DEFAULT NULL,
PRIMARY KEY (' SESSIONID ')
) Engine=innodb DEFAULT Charset=utf8 checksum=1 delay_key_write=1 row_format=dynamic;

Read the. SQL Tool class Reportformsqlutil, with the following code:

Package com.channelsoft.col.colweb.util;

Import Java.io.InputStream; Import java.util.ArrayList; Import java.util.List;

Import Org.slf4j.Logger; Import Org.slf4j.LoggerFactory;

public class Reportformsqlutil {

private static Logger log = Loggerfactory.getlogger (Configutil.class);

public static list<string> Loadsql () throws Exception {

InputStream Sqlfilein = null; list<string> sqllist = new arraylist<string> ();

try {

Sqlfilein = ReportFormSqlUtil.class.getResourceAsStream ("/sql/reportform.sql"); Configuration file path

StringBuffer sqlsb = new StringBuffer ();

byte[] buff = new byte[1024];

int byteread = 0;

while ((Byteread = Sqlfilein.read (buff))! =-1) {

Sqlsb.append (New String (buff, 0, Byteread, "Utf-8"));

}

Sqlfilein.close ();

Windows line break is \ r \ n, Linux under

string[] Sqlarr = sqlsb.tostring (). Split ("(; \\s*\\r\\n) | (; \ \s*\\n) ");

for (int i = 0; i < sqlarr.length; i++) {

String sql = Sqlarr[i].replaceall ("--. *", ""). Trim ();

if (!sql.equals ("")) {sqllist.add (SQL); }

}

return sqllist;

} catch (Exception ex) {

Log.error ("Read report SQL build table file [Reportform.sql] failed.", ex);

throw new Exception (Ex.getmessage ());

}

}

}

Initialize Database Build table:

Set up a reporting database for users
list<string> listsql = Reportformsqlutil.loadsql ();//read. SQL Build Table statement
String crsql = "CREATE database Col_user_" + user.getid () + "DEFAULT CHARACTER SET UTF8";//Build Library statement
try {
Build Library
Jdbctemplate.update (Crsql);
for (int i = 0;i < Listsql.size (); i++) {
Build a Table statement
String SQLL = Listsql.get (i). Replacefirst ("\\{db\\}", "Col_user_" +user.getid ());
System.out.println (SQLL);
Jdbctemplate.update (SQLL);
}
} catch (Exception e) {
Logger.error ("Initialize User Report database error", E);
Throw e;
}

Java initialization database read SQL configuration file build table

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.