Connecting to a database in Java
It should be pretty easy right? Well it is, but difficult to troubleshoot. It will either work or won't, and you won't have a clue why not. So here's a skeleton program that you can use:
import java.sql.*;
import import oracle.jdbc.pool.OracleDataSource;
public class DoDatabaseStuff
{
public static void main(String[] args) throws Exception
{
// Put connection code here..
}
}
Mysql 5
Make sure you put mysql-connector-java-3.1.8-bin.jar into your classpath when running.
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection mysqlConn = DriverManager.getConnection("jdbc:mysql://localhost/DBNAME?user=root&password=WhateverYouEntered");
mysqlConn.close();
Oracle 10g Express
Make sure you put ojdbc-14.jar into your classpath when running. Oh yeah, and the syntax has changed a bit.
OracleDataSource ds = new OracleDataSource();
ds.setURL("jdbc:oracle:thin:username/password@localhost:1521");
Connection conn2 = ds.getConnection();
conn2.close();
Once you've got your connection you can then start executing SQL by using a PreparedStatement (look it up in the Java API's). Tommorow I'll post about how to set up your test data using DBUnit.