Creating mySQL database in JDBC?

Creating mySQL database in JDBC?

Post by Caroline Clabau » Fri, 23 May 2003 10:56:52



Hello there -

I would like to create a new database from a JSP (connecting to a
mySQL database, running Apache and Tomcat 4.1.24) - Is this possible?
I've poked around on newsgroups etc but have not been able to find a
suitable answer.

Can I create a database directly, or perhaps call some sort of script?
I am rather new to all of this, so if you know what that script might
look like I'd really appreciate the code.

Here is what I've tried so far: (i found this code on a newsgroup and
modified it slightly. it results in a "No suitable driver" error)

String tmpdbURL="jdbc:mysql://server";
Class.forName("org.gjt.mm.mysql.Driver");
java.sql.Connection tmpCon =  null;
tmpCon = java.sql.DriverManager.getConnection(tmpdbURL, <user>,
<pwd>);
java.sql.Statement tmpStmt = null;
tmpStmt = tmpCon.createStatement();
tmpStmt.executeUpdate("CREATE DATABASE testuser31"); // + i +";");
tmpStmt.close();
tmpCon.close();

Thank you so much for any insight!

=)Caroline

 
 
 

Creating mySQL database in JDBC?

Post by Mark Matthew » Fri, 23 May 2003 11:43:27


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


> Hello there -

> I would like to create a new database from a JSP (connecting to a
> mySQL database, running Apache and Tomcat 4.1.24) - Is this possible?
> I've poked around on newsgroups etc but have not been able to find a
> suitable answer.

> Can I create a database directly, or perhaps call some sort of script?
> I am rather new to all of this, so if you know what that script might
> look like I'd really appreciate the code.

> Here is what I've tried so far: (i found this code on a newsgroup and
> modified it slightly. it results in a "No suitable driver" error)

> String tmpdbURL="jdbc:mysql://server";

You need a trailing slash, here-------^

        -Mark

- --
For technical support contracts, visit https://order.mysql.com/?ref=mmma

    __  ___     ___ ____  __

  / /|_/ / // /\ \/ /_/ / /__ MySQL AB, SW Dev. Manager - J2EE/Windows
 /_/  /_/\_, /___/\___\_\___/ Flossmoor (Chicago), IL USA
        <___/ www.mysql.com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQE+zDlPtvXNTca6JD8RAuthAKDJiQ6f9y2wMgvt7iuVy4efb5S7DACfZFX1
9FcgAVK8zTX0HvXIJVSiuTU=
=WXeN
-----END PGP SIGNATURE-----

 
 
 

Creating mySQL database in JDBC?

Post by Andree Gro? » Tue, 27 May 2003 19:09:02



> Hello there -

> I would like to create a new database from a JSP (connecting to a
> mySQL database, running Apache and Tomcat 4.1.24) - Is this possible?
> I've poked around on newsgroups etc but have not been able to find a
> suitable answer.

> Can I create a database directly, or perhaps call some sort of script?
> I am rather new to all of this, so if you know what that script might
> look like I'd really appreciate the code.

> Here is what I've tried so far: (i found this code on a newsgroup and
> modified it slightly. it results in a "No suitable driver" error)

> String tmpdbURL="jdbc:mysql://server";

right url: jdbc:mysql:/host:port/dbname
sample:    jdbc:mysql:/127.0.0.1:3306/test
            jdbc:mysql:/localhost:3306/test

Quote:> Class.forName("org.gjt.mm.mysql.Driver");
> java.sql.Connection tmpCon =  null;
> tmpCon = java.sql.DriverManager.getConnection(tmpdbURL, <user>,
> <pwd>);

you can do that in 1 line

Quote:> java.sql.Statement tmpStmt = null;
> tmpStmt = tmpCon.createStatement();

see above, useless line: java.sql.Statement tmpStmt = null
java.sql.Statement tmpStmt = con.createStatement();

Quote:> tmpStmt.executeUpdate("CREATE DATABASE testuser31"); // + i +";");

do not use executeUpdate() because it's for table updates
to execute DDL commands use: stmt.execute();

Quote:> tmpStmt.close();
> tmpCon.close();

if you close connection the Statement-object will be closed
automatically.

Regards, A.G.