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.
bhomassAsked:
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.
0
mohammedfCommented:

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

Open in new window

0
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

0

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
mohammedfCommented:
after downloading mysql driver for java from here ,,,
http://dev.mysql.com/downloads/connector/j/5.1.html
u can also see this site
http://bobcat.webappcabaret.net/javachina/scjd/team/mysql.htm

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

Open in new window

0
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.