I need a little help connecting to a database in Java.

The example in the book I have uses the following:

static final String JDBC_DRIVER = "com.ibm.db2j.jdbc.DB2jDriver";
static final String DATABASE_URL = "jdbc:db2j:books";

It goes on to use it:

System.setProperty("db2j.system.home", "C:/mysql");
Class.forName(JDBC_DRIVER);
connect = DriverManager.getConnection(DATABASE_URL);

First off the book uses a Cloudscape DB, I'm using mysql. My question is, where does the information for the JDBC_DRIVER and DATABASE_URL come from? Are the drivers included in the Java software or do I need to download it?

Thanks
Ken

Hello Squires,

Creating a JDBC connection can be a bit confusing the first few times through. The values for the JDBC_DRIVER and DATABASE_URL (Which I commonly see referred to as the 'connection string') are dependant on which type of database you're using. Each database vendor that supports JDBC distributes the drivers that make it work. I have yet to use mysql, but I can only assume that they are distributed with the database software. Just in case they aren't, I believe that you can download them here

Sorry about that, I accidentally submitted and then took too long editing, so here's the entire reply.

Hello Squires,

Creating a JDBC connection can be a bit confusing the first few times through. The values for the JDBC_DRIVER and DATABASE_URL (Which I commonly see referred to as the 'connection string') are dependant on which type of database you're using. Each database vendor that supports JDBC distributes the drivers that make it work. I have yet to use mysql, but I can only assume that they are distributed with the database software. Just in case they aren't, I believe that you can download them here.

The first step is usually to add the drivers to your classpath. They are commonly distributed as either a .jar or .zip file, so when you edit your classpath variable, make sure it looks something like $CLASSPATH=.:/path/to/jar/drivers.jar. or on Windows you right click on My computer, click properties, then the advanced tag, then environment variables. Then edit or create your CLASSPATH variable with "C:\path\to\jar\drivers.jar"

Now, as far as where those values for JDBC_DRIVER AND DATABASE_URL come from. The JDBC_DRIVER variable will be completely vendor dependant, and at this stage in the game, your best bet for finding it out would be to type something like "mysql jdbc" into Google and see what comes up. But just for kicks I think it's "com.mysql.jdbc.Driver". What this refers to is the directory structure inside the .jar or .zip file that you added to your CLASSPATH, ending with the class name of responsible for creating the connection. The DATABASE_URL, which I'm much more comfortable calling the "connection string" is also highly vendor dependant, but follows a certain format. The connection string will look like "jdbc:drivertype:databaselocation:connectioninfo". Lets look at the connection string for a couple of different vendors.

Oracle thin client:

jdbc:oracle:thin:@machine_name:port_number:instance_name

which would look like

jdbc:oracle:thin:@mycomputernameorIP:1521:TestDB

MySQL:

jdbc:mysql://host_name/dbname

which would look like:

jdbc:mysql://mycomputernameorIP/TestDB.

You can also supply the username/password connection parameters as part of the connection string. This would make the above string look like this.

jdbc:mysql://mycomputernameorIP/TestDB?user=scott&password=tiger

but I usually just pass that information as parameters to the DriverManager.getConnection method.

Whenever you're going to be using JDBC the best advice I could give you is to just look online for this info and as long as you're not using a DB that you wrote yourself you're almost guaranteed to find everything you're looking for in the first few tries. I hope this helps, happy coding.

If u use Microsoft Oracle, Microsoft Access or u using sun Microsoft Products means then use the following instruction,
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); :cry:
//if Oracle means
Connection con=DriverManager.getParameter("Jdbc:odbc:"+db); :D
//db is ur database name
//this can connect ur database
//for using query
Statement st=con.executeQuery("insert into tablename value(xxxxxx)");
} ;)

This article has been dead for over six months. Start a new discussion instead.