Help with JavaDB embedded
First time working with Databases, I'm trying to make an application with an embedded JavaDB. I'm looking in my book Big Java, and I came across this class which the book uses,
Code Java:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
/**
A simple data source for getting database connections.
*/
public class SimpleDataSource
{
/**
Initializes the data source.
@param fileName the name of the property file that
contains the database driver, URL, username, and password
*/
public static void init(String fileName)
throws IOException, ClassNotFoundException
{
Properties props = new Properties();
FileInputStream in = new FileInputStream(fileName);
props.load(in);
String driver = props.getProperty("jdbc.driver");
url = props.getProperty("jdbc.url");
username = props.getProperty("jdbc.username");
password = props.getProperty("jdbc.password");
Class.forName(driver);
}
/**
Gets a connection to the database.
@return the database connection
*/
public static Connection getConnection() throws SQLException
{
return DriverManager.getConnection(url, username, password);
}
private static String url;
private static String username;
private static String password;
}
I created a properties file to give url, name, and password.
In another class, the code contains a line that says :
SimpledataSource.init(args[0])
I'm confused what this means. Does args[0] somehow point to the properties file?
And since this is the first time I'm using databases, am I heading in the right approach with this project? I plan on creating connections throughout another class to communicate with the database.
Re: Help with JavaDB embedded
I don't know if this is what you want, but why don't you try to create an application using netbeans. It will do this job for you. Connect to the database, create GUI etc.
Read this tutorial: http://http://netbeans.org/kb/docs/java/gui-db.html
Just to mentioned something that it's not written in that tutorial. Bydefault, netbeans create a database using Java Db Network Driver. But you want to create an application which is going to use Embeded Driver. For this, you have to follow this: http://http://forums.netbeans.org/ptopic36401.html
and that in the last post user ni62 suggests.
Now you will be able to create a project, but (at least for me) can't run, because of a problem at Persistence unit. To solve this, you have to change Persistence library from TopLink to EclipseLink.
This can be done after opening persistence.xml from netbeans, and congiguring it properly. (you have to choose EclipseLink from a drop-down menu)
Then you have to add EclipseLink to project's libraries.
Now you are allright!
Re: Help with JavaDB embedded
nevermind: I found a topic that relates to my problem