?
Solved

programmatically create an mysql table from java

Posted on 2008-02-08
4
Medium Priority
?
1,006 Views
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.
0
Comment
Question by:bhomass
  • 2
4 Comments
 
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.
0
 
LVL 6

Expert Comment

by:mohammedf
ID: 20855727

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

Open in new window

0
 

Accepted Solution

by:
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

0
 
LVL 6

Expert Comment

by:mohammedf
ID: 20855861
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

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