Java with database storage

I need to write a program that can take orders, store them and then access them later, the application needs to be written in Java. Because of this, I was looking into various ways to use a database with Java.

I was looking into using JDBC with mySQL driver database as well as looking at javaDB. What would you recommend me to do to create this program? Does anyone have any experience with writing a program that uses a DB in java and can give me any advice?

Thank!

+2


source to share


6 answers


I'm sure every Java developer has written an application that talks to a database using JDBC. Many people stop using raw JDBC pretty quickly, either using something like Spring JDBC wrapper or full blown ORM like Hibernate . All of these will make writing the database layer a little easier, but I usually feel like you should have a reasonable understanding of what is going on under the hood before diving into them.



So, a question in progress or a dedicated database? It depends. How many people will immediately access the app? (A database on the fly will be useless if you need multiple people to access the same database.) How much control do you have over the environment? (it's a waste of time offering a dedicated database if you can't install it). Is the app a website or a desktop app? (A web solution can use the database in the process and allow multiple users to access it). How important is data? (The database in the process is less likely to have the same level of redundancy as a dedicated database.)

+3


source


In your case, I would use Java6 and Java DB (aka Derby). It's not that installing and using MySQL is difficult (it's actually quite simple), but okay, why are you doing this if you already have a database?

With that said, to get started with JavaDB check out the Java DB Reference , there are many technical articles there. Pay special attention to Working with a Java DB (Derby) Database - The NetBeans IDE 6.5 Tutorial .



For data access itself, you can use the Java Persistence API (JPA) as in Creating a Custom Java Desktop Database Application . But I wouldn't do it for homework. Instead, I'll start with the basics, which is JDBC, and do it by hand. You mentioned this and I think this is a good idea. Take a look at Tutorial: Java Database with Derby, Java's own open source database , this can be very useful (I am not saying the code shown there, contributes to all the best practices, but it's pretty simple and will get you started.)

Don't pollute your mind with advanced topics like pooling, don't use frameworks like Hibernate, JPA or even Spring (these frameworks know how to do things, you don't, and it's not about learning how to use frameworks at least not now). Keep it simple and sexy, but do it by hand.

+2


source


One very simple approach is to use Spring JDBC classes, which provide many convenience methods for raw JDBC and automatically manage resource release. Spring also provides a rich exception hierarchy that allows you to take specific actions based on certain errors (like retrying a database lock).

Also, unlike many other resiliency layers, Spring does not hide the JDBC implementation under the covers (it acts like a thin layer), allowing you to use raw JDBC as needed.

Initialization

// First create a DataSource corresponding to a single connection.
// Your production application could potentially use a connection pool.
// true == suppress close of underlying connection when close() is called.
DataSource ds = new SingleConnectionDataSource("com.MyDriver", "Database URL", "user", "password", true);

// Now create a SimpleJdbcTemplate passing in the DataSource.  This provides many
// useful utility methods.
SimpleJdbcTemplate tmpl = new SimpleJdbcTemplate(ds);

      

SQL update (or insert)

// Simple example of SQL update.  The resources are automatically release if an exception
// occurs.  SQLExceptions are translated into Spring rich DataAccessException exception
// hierarchy, allowing different actions to be performed based on the specific type of
// error.
int nRows = tmpl.update("update Foo set Name = ? where Id = ?", "Hello", 5);
assert nRows == 1;

      

SQL Query

// Example of SQL query using ParameterizedRowMapper to translate each row of the
// ResultSet into a Person object.
tmpl.query("select * from Person", new ParameterizedRowMapper<Person>() {
  Person mapRow(ResultSet rs, int rowNum) {
    return new Person(rs.getString("FirstName"), rs.getString("LastName"));
  }
});

      

+1


source


Sun has two JDBC tutorials on the Internet that you might find useful, JDBC Introduction and JDBC Basics . This is part of a much larger Java Tutorials series that has a lot of great information. They are usually the first thing I look at when I come across the fact that I don't know how to do this in Java.

+1


source


Check out the following examples: http://www.roseindia.net/jdbc/jdbc-mysql/ . Lots of code examples for using JDBC with MySQL.

0


source


I can of course recommend JavaDb (the database that ships with Java 6). It was the original Derby database from IBM and is very capable. It integrates well with the Java program (either runs as a standalone process or in the same virtual machine as the client program). You don't have to worry about additional non_Java installation (which can be important depending on how / when you deploy).

If you want / need an ORM (Object Relational Mapping), Hibernate is the de facto Java standard these days ( here's an introduction ). However, if this is overkill for your project, at least check out Apache Commons DbUtils which will minify your boilerplate code.

0


source







All Articles