0

In my Java application I have written a class that starts the Derby database server programmatically.

Can I also write a new class that uses JDBC to create a new Derby database from raw Java code rather than going through the Netbeans prompts to set up the derby database?

In my research I have found the CREATE DATABASE sql statement but I can not get this to work. Here is the code I have so far:

public class DbConnect
{
   String host = "jdbc:derby://localhost:1527/";
   String userName = "fam";
   String password = "fam";
   public static Connection con;
   Statement s;
   static final String drive = "com.derby.jdbc.Driver";

   protected DbConnect()  { }

   public void getDBConnection(Connection con)
   {
      try
      {   
         s = con.createStatement();
         String newDB = "CREATE  DATABASE NEWDB";

         s.executeUpdate(newDB);

         String host = "jdbc:derby://localhost:1527/NEWDB";
         String userName = "fam";
         String password = "fam";
         con = DriverManager.getConnection(host, userName, userName);
      }
      catch(SQLException err)
      {    
         System.out.print(err.getMessage());
      } 
 }
  • Are you just creating a new, throwaway database for usage on startup for testing/sandbox purposes? – Makoto Jan 07 '15 at 00:27
  • no I want the database to remain with the program so that any time the family budget data is changed the updates data can go back into the database. The main point for me is to be able to use java code to get the job done rather than let net beans do all the work for me. I a writing a family budget program to practice core Java, so I am purposely trying to do harder and harder coding tasks. – user3317065 Jan 07 '15 at 02:46
  • Do you have any config files for this? Have you done database programming before (`create database` is... heavy)? What is this supposed to do (I suspect you're going down the wrong path, but as I'm not 100% sure what you're trying to do, it is difficult to gide you to the correct one if that is the case). As an aside, you might wish to consider [sqllite](http://stackoverflow.com/q/41233/289086) instead (its a file based sql database rather than firing up another process/thread for the database). –  Jan 07 '15 at 03:25
  • What do you mean by CREATE DATABASE is heavy? – user3317065 Jan 07 '15 at 11:39

0 Answers0