programmatically create an mysql table from java

I need to create a mysql database from inside a java program. the usu jdbc api does not work, since you can not establish a connection before the database exists.

please tell me how to go about it.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kim RyanIT ConsultantCommented:
You just need to create a text streing containg the correct SQL statements , for example
"create table x (a integer, b char(3), c varchar(20))"
Then you need to execute that statement by connecting to the mysql dtabase, and supplying username, password etc. There should be some java library or ODBC module to do this.

PreparedStatmenet pst = connection.prepareStatment("SQL Statment that create a table");

Open in new window

bhomassAuthor Commented:
you both missed the point. my point is that I wasn't able to make the connection before the database is created. yet, you both start with, or simply take for granted that i have established a connection.

I finally got it going by using url which only has server address, but not database name, and was able to get a connection.

thanks for the response, though


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
after downloading mysql driver for java from here ,,,
u can also see this site

use the following code
import java.sql.*;
   public class connect
       public static void main (String[] args)
           Connection connection = null;
           Statement statement=null;
	String dataSourceName = "DataBaseName";
        String dbURL = "jdbc:mysql://localhost/" + dataSourceName;
        Class.forName ("").newInstance ();
        connection= DriverManager.getConnection(dbURL, "",""); 
         statement = connection.createStatement();
         	catch (Exception err) {
        System.out.println( "Error: " + err );

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.