Je ne sais pas si cela peut etre utile mais une solution peut etre celle-là:
public class createDBSchema
{
private dbConnection dbc;
static final String dbName = "test";
static final String user = "root";
static final String psswrd = "";
Connection conn;
Statement stmt;
public createDBSchema()
{
dbc = new dbConnection();
this.conn = dbc.getDbConnection(dbName, user, psswrd);
}
public boolean createTable( String streamToExecute )
{
if(streamToExecute.equals("STREAM FILE NOT FOUND"))
return false;
System .out.println(
" CREATE TABLE \n " + streamToExecute);
try
{
Statement stmt = conn.createStatement();
stmt.executeUpdate(streamToExecute);
stmt.close();
this.conn.close();
return true;
}
catch ( SQLException sqlex)
{
System .out.println(sqlex);
try
{
if(this.conn != null)
this.conn.close();
}
catch (Exception ex)
{
}
return false;
}
} // createTable
public boolean dropTable( String tableName )
{
try
{
Statement stmt = conn.createStatement();
stmt.executeUpdate( "DROP TABLE " + tableName);
stmt.close();
return true;
}
catch (SQLException sqlex)
{
System .out.println(sqlex);
return false;
}
} // dropTable
public void ReadScriptFile(String myfile)
{
BufferedReader bfr = null;
StringBuffer strBuff = new StringBuffer();
try
{
String stream = null;
// input file name
File file = new File(myfile);
// Create FileReader Object
FileReader inputFileReader = new FileReader(file);
// Create Buffered/PrintWriter Objects
bfr = new BufferedReader(inputFileReader);
while ((stream = bfr.readLine()) != null) {
strBuff.append(stream);
}
createTable(strBuff.toString());
//return true;
}
catch(FileNotFoundException fnfEx){
//affiche(fnfEx.getMessage());
//return false;
}
catch(IOException IOEx){
//affiche("ReadFile(String myfile) - IOEx " + IOEx.getMessage());
//return false;
}
finally
{
try
{
bfr.close();
}
catch (Exception e)
{
}
}
}
/*
public static void main(String[] args)
{
//dbc.getDbConnection("test", "root", ""); //A passer au constructeur
createDBSchema cdbs = new createDBSchema();
cdbs.ReadScriptFile("C:\\Documents and Settings\\user\Desktop\\Activity\\2005\\PRIN\\cat_create2.sql");
}
*/
}
//You can pass the string "CREATE DATABASE DBNAME " to the method createTable(String str)
Je l'ai testé et ça marche.
Une astuce est celle de créer la connection en passant comme BD soit Mysql, soit test car ils st créés par défaut. et de là créer la base.
J'espère que mon petit code servira.
TTornado