I know that it's a very undesireable scenario, but I need to create a database straight from java code. Yes, a database. Not a table. A database. The database will be dropped at the end of the program and will never be directly touched by the user, so all I'm really concerned with is the actual creation.

Also, using a pre-existing database as a jumping point isn't really an option.

I'm certain there must be some way to do this, but I haven't really found anything yet that sounds very promising....is this actually possible?

Sure, with an embedded database it's no trouble. We do this with H2 for every new project when it's first created. You just run a script with your data definition code in it.

Edit: H2 can also create an in-memory database that does not persist when closed.

commented: Exactly what I needed. +1

I'm more confused by the question. Do you want to literally write a database software/server, or do you desire to connect to a pre-existing database server such as MySQL and then run SQl like "CREATE DATABASE mydb;". If you are trying to do the first then it seem like Ezzaral's suggestion has that covered. If you want to just connect to a dbserver, then there are db drivers available like JDBC and Connector/J. There is also Java DB.

Ezzaral's suggest does in fact seem like exactly what I was looking for. However, I'm still trying to get H2 working (I'm having trouble setting the classpath?), and I didn't want to mark this as answered if H2 did not end up working.

The problem I'm having at the moment is that I have never worked with multiple packages or assigning classpaths before. I understand how it -should- work, but that doesn't seem to be working. What I'm trying is:

c:>java -classpath C:\H2\src\main org.h2

Am I missing something that should be painfully obvious?

>java -cp c:/H2/bin/h2-1.2.141.jar ClassYouWantToRun The compiled jar file is located in the /bin directory.

Thanks, Ezzaral. That's pretty much what I thought....but I'm still getting this error:

Exception in thread "main" java.lang.NoSuchMethodError: main

when I enter >java -cp c:\H2\bin\h2-1.2.141.jar org.h2.Driver

this:
both
c:\>java -cp C:/H2/bin/h2-1.2.141.jar org.h2
and
c:\>java -cp C:/H2/bin/h2-1.2.141.jar org

give me:

Exception in thread "main" java.lang.NoClassDefFoundError: org/h2

The way I'm understanding it, I need to specify a directory for java to start in when looking for classes. So if I set the classpath to look in the "org" directory, then java will start there, go to h2, then go to Driver.class when the program tries Class.forName("org.h2.Driver").

Perhaps what I'm really looking for is just a "if you set the classpath to <this>, it will take the input and be happy, whether it's what you're looking for longterm or not. <This other path> is probably what you're looking for right now to get things rolling."

You need to the jar itself in the classpath. Java will look for the necessary files in that jar. After the classpath, you need to specify the name of your class to run your compiled program. That is why I had "ClassYouWantToRun" in my statement above.

Oooooh, I see what you're saying. I totally misread that the first time...

I'm still not entirely clear on getting the classpath set from the command line....but I think I got it working via the classpath selector in jgrasp.

Thanks for all the help (and patience). H2 is definitely exactly what I was looking for :)

Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.