Read SQL files and execute CREATE DATABASE insert Database

Source: Internet
Author: User

/* Execution Program */
Package enginedb;

Import java. SQL .*;
Import java. util .*;
Import java. Io .*;

Public class jbsql {

Public String query_statement = NULL;/* define an SQL statement */
Public connection conn = NULL;
Public Statement stmt = NULL;
Public resultset res = NULL;

Public String Param [];/* query condition, or a new record */
Public jbsql (){}

Public void setparam (string [] PARAM ){
This. Param = Param;
}

Public void setquerystatement (string query_statement ){
This. query_statement = query_statement;
}

Public void setconnection (string drivername, string jdbcurl, string username,
String passwd) throws exception {
Connection conn1;
Class. forname (drivername );
Conn1 = drivermanager. getconnection (jdbcurl, username, passwd );
Conn1.setautocommit (false );
This. Conn = conn1;
}

/* Obtain the query result */
Public resultset getresult (){
Try {
Preparedstatement select_stm = conn. preparestatement (query_statement,
Java. SQL. resultset. type_scroll_insensitive,
Java. SQL. resultset. concur_read_only );
If (Param! = NULL ){
For (INT I = 0; I <Param. length; I ++ ){
Select_pai.setstring (I + 1, Param [I]);
}
}
Res = select_pai.executequery ();
}
Catch (exception e ){
System. Out. println (E );
}
Return res;
}
// Create a database
Public void createdatebase () throws sqlexception,
Java. Io. unsupportedencodingexception {
Try {
// Program transcoding to solve Chinese problems
Preparedstatement create_stm = conn. preparestatement (new string (
Query_statement.getbytes (), "iso8859_1 "));
Create_executeupdate ();
Create_pai.close ();
Conn. Commit ();
}
Catch (exception e ){
System. Out. println (E );
Conn. rollback ();
}
}

// Create a workflow Database
Public void createenginedb () throws sqlexception,
Java. Io. unsupportedencodingexception {
Try {
Preparedstatement create_stm = conn. preparestatement (query_statement );
Create_executeupdate ();
Create_pai.close ();
Conn. Commit ();
}
Catch (exception e ){
System. Out. println (E );
Conn. rollback ();
}
}

// Find the ';' location
Public arraylist findplace (string filename) throws exception {
Filereader = new filereader (filename );
Arraylist array = new arraylist ();
Char [] chrbuffer = new char [1024*35];
// Put all data into the buffer
Filereader. Read (chrbuffer );
// Strstream: contains all read strings
String strstream = new string (chrbuffer );
Int Index = 0;
For (INT I = 0; I <chrbuffer. length; I ++ ){
If (chrbuffer [I]! = '/0') {// if it is not at the end
// Locate ';'
If (chrbuffer [I] = ';') {// output'; 'location
Char [] chars = new char [1024];
Strstream. getchars (index, I + 1, chars, 0 );
// Add the extracted characters to the arraylist
Array. Add (new string (chars ));
Index = I + 1;
} // End if
} // End if
}
Filereader. Close ();
Return array;
}
}

/* Execution Program */
Package enginedb;
Import java. util .*;

Public class jbmain {
Public jbmain (){
}
Public static void main (string [] ARGs ){
Jbsql mysqlbean = new jbsql ();
String drivername = "org. gjt. Mm. MySQL. Driver ";
String jdbcurl = "JDBC: mysql: // FIG: 3306 /";
String username = "root ";
String passwd = "";

String engine_ SQL = "CREATE DATABASE"; // creation statement
String engine_db = "enginedb"; // Database Name

Try {
// Create an enginedb database statement
Engine_ SQL = engine_ SQL + engine_db + ";";
Mysqlbean. setconnection (drivername, jdbcurl, username, passwd );
Mysqlbean. setquerystatement (engine_ SQL );
Mysqlbean. createdatebase ();

Arraylist alist = mysqlbean. findplace ("wfdb_setup. SQL ");
Iterator it = alist. iterator ();
While (it. hasnext ()){
String strstream = (string) it. Next (). tostring ();
String query_statement2 = strstream;
Jdbcurl = "JDBC: mysql: // 127.0.0.1: 3306/" + engine_db;
Mysqlbean. setconnection (drivername, jdbcurl, username, passwd );
Mysqlbean. setquerystatement (query_statement2 );
Mysqlbean. createdatebase ();
}
}
Catch (exception ex ){
Ex. printstacktrace ();
}
}
}

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.