tags:

views:

95

answers:

4

I've recently started my first programming job, and what I need to do as my main project is to create a program for simulating diesel generator behaviour.

I'm creating this program using Java, which I've never used before (all of my limited experience is with C++), and the first problem I need to overcome is to create a database to store the necessary data that will act as input to the simulator.

This database problem strikes me as something that has, in general, been done many times before. Could anyone get me started on the right track towards achieving this with suggestions on what to use to first create the database, and then what to use to access it with the simulator? In regards to the latter, I've been reading over the java.sql package, and it seems as though it would suit my purposes. What do you think?

Thanks very much in advance.

A: 

In one word Google or buy a book, there are a million resources that have answered this very question.

mP
I didn't downvote, but it looked to me, the OP didn't knew where to start, and in this case, google is pretty useless.
Andreas_D
"Use Google" can be an answer to most things on SO, but that doesn't really help people who are "all at sea" like the original poster. A post suggesting *what* to search for in Google would have been far more useful.
Ben Poole
+8  A: 

Database access in Java goes via JDBC. (Indeed all those classes are in the java.sql package)

Check the JDBC Tutorial

Here is a sample snippet from that tutorial to give you an impression:

Connection con = DriverManager.getConnection
           ( "jdbc:myDriver:wombat", "myLogin","myPassword");

Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM Table1");
while (rs.next()) {
 int x = rs.getInt("a");
 String s = rs.getString("b");
 float f = rs.getFloat("c");
}

JDBC is compatible with any Relational Database Management System. I like MySQL myself, as it is free and has plenty of documentation on the web. But any RDBMS goes.

If you're going for MySQL, a simple way to get started is to install MySQL and create the database directly from the MySQL shell. PhpMyAdmin is another simple option to manage a MySQL database. (On windows, in combination with XAMPP. On linux, from your distro's package manager)

amarillion
Please add code that shows that you need to close `Connection`, `Statement` and, sometimes, the `ResultSet` to prevent resource leaks in general case and in the presence of exceptions. If you do, I'll give you +1.
Alexander Pogrebnyak
Of course, but this is just a first impression to get you started. It also doesn't show how to deal with SQLException etc.
amarillion
A: 

JDBC is the keyword. Use this to look for Tutorials on the net.

Andreas_D
+4  A: 

There are 2 parts to your question: how to create a database, and how to access it from your code.

Part 1: there are several light-weight database projects that you can choose from. Check out HSQLDB, it's quite popular and user-friendly. You'll have to download it, and you can use the built-in tools to create and populate your database with data.

Part 2: The JDBC standard defines how a Java program connects to the database. The tutorial (as suggested by amarillion) is a good place to start. You'll need to put the database's JDBC driver in your classpath, and you'll be able to connect to the DB from your code. Note that parts of the process (such as the driver and the connection string) are specific for the database you choose (meaning, the connection string for HSQLDB is different from the one for MySQL).

However, JDBC is rather low-level: you have to work with objects that represent connections and SQL statements (the ones you noticed in java.sql). This is an excellent place to start, but later on, as your code grows, you might want to consider using an O/R mapping tool. It will make your life easier, but it has a learning curve. The most popular ORM for Java is Hibernate.

Good luck!

Eli Acherkan
I would not recommend Hibernate to somebody who's never used JDBC. It might be low-level, but so is a newcomer's understanding and comfort. Your two part answer is good otherwise.
duffymo
@duffymo: agreed, I meant Hibernate as a later stage. Edited the answer to better reflect my meaning.
Eli Acherkan