|
SYS-CON.TV Webcasts
Comments
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
Top Links You Must Click On
General Java Serialized Database Connectivity
Serialized Database Connectivity
By: Ashton Hobbs
Mar. 1, 1997 12:00 AM
With the release of the Java 1.1 API, Java developers have even more tools to work with. Two important abilities that were added to the Java 1.1 API include the Java Database Connectivity (JDBC) objects and objects to handle Serialization. JDBC provides developers with the means to connect to any type of database and perform queries and updates against the data located in the database. The JDBC API finally gives developers the ability to create usable business solutions using Java. Another new ability added to the Java language is the ability to use Serialization. Serialization is the process by which the application can send program objects through a stream, which can be a file stream or a network stream. Sending objects through a stream will allow developers to create solutions that were not available until now. In this article, I will demonstrate how to use Java Serialization and the JDBC to save objects to a database and then access these objects from the same database. This process will allow an application to save a current session to a database and then reload the session from the database at a later date. Unlike current programming languages, Java allows me to save the object itself and not just the data contained within the object. Most programming languages allow the application to save the data contained within an object, but the data has to be reloaded into the object to be usable. Using Java's Serialization, I can save the entire object, including the look and feel of the object, to a file. For this article, I am using Sybase SQL Anywhere as my database. An evaluation copy of this database is available from www.sybase.com. I am also using the JDBC-ODBC Bridge, available from JavaSoft at www.javasoft.com, to provide a JDBC driver to access the database. There are many different databases and drivers that could be used to provide the same solution; however, I chose these two products since they both provide an evaluation copy that can be used. I will be using only one table to store the objects I will be saving in this article. The table, tObject, will contain two columns, ObjName and Object. ObjName will be a character field that will store the name of the object. The Object column will be a LongBinary column that will contain the binary data of the object. Depending on the database being used, the LongBinary data type could also be known as a Blob, LongVarBinary, or sometimes just Binary. I will create two applications in this article. The first application will create a basic window with a Close button. The window will be serialized and saved to the tObject table. I will use JDBC to access the database and insert a new record into the tObject table. I will store a reference to the window and a stream of data that contains the serialized window object. The second application will simply connect to the database, retrieve the window object and open it. The first application will extend the Frame object to create a new window. I will name this new object AppFrame. In the constructor for the AppFrame class, I will initially call the ancestor method to create the Frame object. Once the frame has been created, I will place a button with the label Close on the window using the FlowLayout.
this.setLayout(new FlowLayout(FlowLayout.CENTER));
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Once the Driver object is declared and loaded, it is registered with the DriverManager object. I can now use the DriverManager object to create a connection to a database with a given Uniform Resource Locator (URL). The URL of the data source will simply be jdbc:odbc:
Connection c = DriverManager.getConnection
FileOutputStream ostream = new FileOutputStream("temp.dat");
Once the object has been written to a local file, I will save the contents of the file in the tObject table, located on the database. I will use the PreparedStatement object to create a SQL Insert statement that I can later insert specific values into. The SQL Insert statement will insert a record into the tObject table with the ObjName equal to Frame1 and the contents of the local file saved above. The following lines will create a PreparedStatement object with the SQL Insert statement needed to allow the object information to be specified using the setBinaryStream() method of the PreparedStatement object.
String sql = "insert into tObject values (Frame1', ?)";
FileInputStream istream = new FileInputStream("temp.dat");
prepare.setBinaryStream(1, istream, count);
The Insert statement is now ready to be sent to the database and executed. Calling the executeUpdate() method of the PreparedStatement object will send the completed SQL statement to the database for processing.
prepare.executeUpdate();
This finishes the first application needed to save the serialized object to the database. Listing 1 contains the complete code for the AppFrame class.
The second application will be responsible for connecting to the database and getting the Frame object that was saved using the AppFrame application. The retrieved frame will then be displayed to the user exactly as it appeared in the AppFrame application. Using this method for saving objects, it is possible to create applications that can save all of the components within an application to the database and then use the database as a persistent object server. The database and serialization method also could be used to create an extensible and usable version control system. Not only could the source and compiled files be stored in the versioning system, but the actual objects the application uses could be versioned and stored as well.
The second application I will create will be the GetFrame application. It will not contain any code in the constructor, but the main() method for the application will be responsible for connecting to the database and retrieving the object. Once the Frame object has been retrieved from the database, it will be displayed to the user in the same fashion as a normal Frame object.
The connection to the database will be established in exactly the same fashion as it was established in the AppFrame application. Once the connection to the database has been established, I will execute a SQL Select statement to return the Object column from the database for the record that has ObjName equal to Frame1. The SQL Select statement will be as follows:
String sql = "select Object from
Statement s = c.createStatement();
byte b[] = r.getBytes(1);
I passed parameter 1 to the getBytes() method to indicate that I wanted the data from the first column. I also could have passed the string Object to indicate that I wanted the contents of the Object column from the result set. Once I have the array of bytes for the object, I will need to write those bytes to a local file using a FileOutputStream and then read those bytes back into the application using a FileInputStream and the ObjectInputStream. To write the bytes to a local file, I will create a FileOutputStream object, specifying the name of the file to create, and then use the write() method of the FileOutputStream object to write the bytes. The following three lines of code will create a file to save the byte stream in, write the array of bytes to the output stream and close the output stream and file.
FileOutputStream ostream = new FileOutputStream("temp,dat");
FileInputStream istream = new FileInputStream("temp.dat");
Frame f = (Frame)in.readObject();
Using a combination of Java Serialization and JDBC, I was able to use a database to store Java objects. Using JDBC and Serialization, Java applications can be developed that provide functionality as well as flexibility. Applications can store themselves in a database and then be recreated later, or developers can store completed objects within a database to be accessed by users directly. Using JDBC and Serialization was not complex, but it allows for some complex solutions to be created. Reader Feedback: Page 1 of 1
Your Feedback
Enterprise Open Source Magazine Latest Stories . . .
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||