Home Discussion Discuss: Insert Serialized Object into Database Table

  • StudentDetails classnewbProgrammer May 1, 2012 at 8:22 PM

    Could you add a StudentDetails class or describe it atleast

  • Serialized object stored into databaseMoulika Thirumalasetty April 10, 2013 at 12:57 PM

    I am not getting the output while executing the below code..Could anyone tell please????It is not giving any errors but data is not inserted into database.... import java.io.*; public class Studentdetails implements Serializable { String name; String age; String sex;} import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class InsertSerialObject { public static void main(String[] args) throws Exception { Class.forName("oracle.jdbc.driver.OracleDriver"); Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","SYSTEM","admin"); Studentdetails s1 = new Studentdetails(); s1.name="moulika"; s1.age="22"; s1.sex="female"; PreparedStatement ps; try { ps = con .prepareStatement("INSERT INTO serializationtest VALUES (?,?,?)"); write(s1, ps); System.out.println("inserted"); ps.close(); Statement st = con.createStatement(); ResultSet rs = st.executeQuery("SELECT * FROM serializationtest"); while (rs.next()) { Object obj = read(rs, "name"); Studentdetails p = (Studentdetails) obj; System.out.println(p.name + "\t" + p.age + "\t" + p.sex); } rs.close(); st.close(); } catch (Exception e) { } } public static void write(Object obj, PreparedStatement ps) throws SQLException, IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oout = new ObjectOutputStream(baos); oout.writeObject(obj); oout.close(); ps.setBytes(1, baos.toByteArray()); int count=0; ps.setInt(2, ++count); } public static Object read(ResultSet rs, String column) throws SQLException, IOException, ClassNotFoundException { byte[] buf = rs.getBytes(column); if (buf != null) { ObjectInputStream objectIn = new ObjectInputStream( new ByteArrayInputStream(buf)); return objectIn.readObject(); } return null; } }

Post your Comment



Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Related Articles
Java-Tutorials

Insert Serialized Object into Database Table
Insert Serialized Object into Database Table In the previous tutorial, you... it into a file. In this section, you will learn how to Insert a serialized object into database table, deserialize it after fetching it from database table 

Java-Tutorials

Writing and reading from/to a serialized file through Hash Table in Java
Writing and reading from/to a serialized file through Hash Table in Java... illustrates you how to read and write from/to a serialized file through the hash table.... Following program has the facility if the specified serialized file does not exist 

Programming Tutorials

Retrieving the Image from a database Table
the image from the database table so there is need of a ResultSet object which...Retrieving the Image from a database Table Consider a case where we want... to retrieve the image from the database table. You can do it very easily after 

JDBC

Description of Database Table
Description of Database Table     ... about the database table. As we all know that, table has some specific information... by the database. By using a simple query we can get all information about the table 

SQL Tutorial

Drop Table
; Drop Table in SQL get rid of an object or table from the database. Using... is removed from database. The Drop table cannot get it back, once you run the drop...)); Insert Data into Stu_Table The insert into add the records or rows 

Java Beginners

table
input from oracle table(my database table..) This is a very important table of my 

Java-Tutorials

Deserializing an Object in java
. The given program shows how to read any data or contents from the serialized object or file. It takes a file name and then converts into java object. If any exception... Deserializing an Object in java      

Hibernate Interview Questions

Map java Object to database.
Map java Object to database.  How do you map Java Objects with Database tables 

Development process Questions

Delete database Table through hibernate and Spring
Delete database Table through hibernate and Spring  Hi, I am using... once without repetation,or Detete table after data is fetched. Here... userDao implementation * @param userDao : object (implementation of UserDao 

JDBC

Creating a Database Table
Creating a Database Table     ... the same database in many different ways.  Table: A table is basic component... and it's description that helps you to create a database table by using java file. Brief 

Programming Tutorials

Drop Table
Table in SQL get rid of an object or table from the database. Using a drop Query... from database. The Drop table cannot get it back, once you run the drop Query...)); Insert Data into Stu_Table The insert into add the records or rows to the  

Servlets

Deleting All Rows From the database Table
the PreparedStatement object. If the rows has been deleted from the database table... From the database Table      ... helps us to write on the browser. To delete all the rows from our database table 

Servlets

Inserting Data In Database table using Statement
to insert the data in the database from our java program in the table stored... Inserting Data In Database table using Statement... of the PrintWriter class. We can insert the data in the database only and only 

JDBC

Database Table Creation Example with JDBC Batch
for Batch processing for alter database table. 1. Create Statement object...Database Table Creation Example with JDBC Batch: In this example, we are discuss about table creation in the database using JDBC Batch process. First 

JDBC

Retrieving All Rows from a Database Table
Retrieving All Rows from a Database Table   ... from a database table. You know that table contains the data in rows and columns... a specific database table. If any exception occurs then shows a message "SQL code 

Ajax Questions

ajax+options is null or not an Object
(e); } %> <%=empname%> Database table table name...();' > <div id='data'> <table name='data' >...="<table name='data'>"; try{ Class.forName 

JDBC

Alter Database Table Using JDBC Batch Process
for alter database table. 1. Create Statement object using createStatement...Alter Database Table Using JDBC Batch Process: In this example, we are discuss about alter database table using JDBC Batch process. First of all, we 

SQL Question

updating a table in a database - SQL
updating a table in a database  give me complete source code in java to execute the sqlquery such that (update table_name set phone no=? where cous_id=?). or in simple way give me source code to update my table in MsAccess 

Java-Tutorials

JDBC: Create Table Example
JDBC and using database MySql. Create Table : Database table is collection... table record. For creation of database table we need to understand some terms... are creating table, named student in the database. For that we create 

JSP Tutorials

Create a Table in Mysql database through SQL Query in JSP
Create a Table in Mysql database through SQL Query in JSP...; This is detailed java code to connect a jsp page to mysql database and create a table of given... the table creation. 1. welcome_to_database_query.jsp 2.create_table.jsp 1 

JSP Tutorials

Navigation in a database table through jsp
Navigation in a database table through jsp  ... :- mysql> create database student; Then create a table named 'stu...; <HEAD> <TITLE>Navigating in a Database Table </TITLE> < 

JDBC

Getting Column Names from a database table in Java
Getting Column Names from a database table in Java  ... with code that retrieves all columns name in a specific database table. Sometimes... of the table, so you can retrieve it with the help of this example. See detail information 

JSP Servlet Questions

inserting dropdown values into database table
inserting dropdown values into database table   hi i want to insert dropdown values into a database table by using jsp 

Struts 2 Tutorials

Data Access Object
;adminuser") maps the class with the database table. The @Column(name = "...Creating Data Access Object (DAO) Design Pattern The Data Access Object.... It provides a technique to separate the object persistence and data access logic 

Servlets

How to delete a table in mysql
from a database.  To delete a table from the database firstly we need...; </web-app> Table in the database...: Table in the database after deletion 

Java-Tutorials

Deleting Blob Data from Database table using servlet
Deleting Blob Data from Database table using servlet In this section, we will discuss about how to insert a blob data into a database table. A Blob stores a binary large object in the database table's row. Blob object 

Java Interview Questions

java properties serialized
 

SQL Question

Designing a Database with a junction table or without it?
Designing a Database with a junction table or without it?  Designing a group of table with, where many tables reference single parent table. In that case is it a better option to create a junction table to maintain 

SQL Question

What do you mean by Serialized?
What do you mean by Serialized?  What do you mean by Serialized?   Hi, It is the part of the isolation level , Its bassically use for the Data read by the current transaction cannot be changed by another transaction 

JDBC Questions

DATABASE
DATABASE  I can't send different information from different form into a single table in my database 

DMCA.com