programmatically create an mysql table from java

Posted on 2008-02-08
Medium Priority
Last Modified: 2012-05-05
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.
Question by:bhomass
  • 2
LVL 19

Expert Comment

by:Kim Ryan
ID: 20855685
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.

Expert Comment

ID: 20855727

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

Open in new window


Accepted Solution

bhomass earned 0 total points
ID: 20855839
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


Expert Comment

ID: 20855861
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 ("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


Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

589 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question