ssbellows
ssbellows

Reputation: 35

How do I go about creating and then connecting to and querying a database in Java?

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.

Upvotes: 2

Views: 222

Answers (4)

Eli Acherkan
Eli Acherkan

Reputation: 6411

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!

Upvotes: 4

amarillion
amarillion

Reputation: 24917

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)

Upvotes: 8

Andreas Dolk
Andreas Dolk

Reputation: 114777

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

Upvotes: 0

mP.
mP.

Reputation: 18266

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

Upvotes: -1

Related Questions