1

I am working on a stand-alone Java project (not Java EE), I am currently learning JDBC, I am creating a database where it contains Employee information, such as Personal information, Contact Information, Employee Information and Tax information, all of these are classes with references with each other and they have setters and getters, I am wondering how would I insert their data value in the database/tables I created in the database? in short I have Employee an employee object with like this

Employee(PersonalInformation information,ContactInformation cInformation)

Something like that, how would I add their data in the tables I made in the database?

4
  • possible duplicate of Java Database with Mapping? Commented Nov 28, 2011 at 8:34
  • check out this tutorial docs.oracle.com/javase/tutorial/jdbc/basics/index.html Commented Nov 28, 2011 at 8:35
  • is it possible without mapping? since I do not have experience or idea about that whatsoever and our instructor forbids us to use that for this project Commented Nov 28, 2011 at 8:37
  • @KyelJmD, sure, if the objective is to learn JDBC, then go ahead with it, you'll come back to see better solutions with ORM later on. Commented Nov 28, 2011 at 8:51

4 Answers 4

2

how would I add their data in the tables I made in the database?

By using JDBC to send INSERT statements to the database, one for each object in your object graph (assuming you have one table per class).

And yes, that's a lot of boring, repetitive code to write, which is likely to contain errors and quite a burden to maintain.

Which is why people have written Object-Relational mappers like Hibernate, and there's a Java standard for that called JPA. Which is part of Java EE, but that doesn't mean you have to run a Java EE server to use it.

Update: OK, so you cannot use an ORM, probably because you're supposed to understand how JDBC works first, which makes sense because ORMs are based on JDBC and sometimes you have to go down to that level when there is a problem.

When using JDBC directly, you typically create a DAO (data ccess object) layer that is responsible for writing and reading objects to/from the database. For dealing with a nested object, one DAO can call the other.

And no, the DAOs are not called in the setters and getters. There called by the part of your application that reacts to user input. E.g. when the user opens the application and the start screen shows a list of employees, you'd call an EmployeeDAO.findAll() method, and when the user makes changes to an employee and clicks on "save", you'd call the EmployeeDAO.save() method.

Sign up to request clarification or add additional context in comments.

8 Comments

so inside the setters and getter there is an INSERT statements??? our instructor forbids us to use ORM/mapping. I don't know why
and Yes Ih ave on table per clas
can you prove an example for these? I am kinda confused
@KyelJmD : Here's the best one I could find on short notice: balusc.blogspot.com/2008/07/dao-tutorial-data-layer.html Look especially at the UserDAO class.
@KyelJmD: Sure. The only thing that would require a server would be the connection pool, but that is optional (and really only important for a server anyway).
|
0

I recommend you to look at Spring's JdbcTemplate (http://www.java2s.com/Code/Java/Spring/ControlParameterTypeInJdbcTemplateQuery.htm)

Its looks like this:

SingleConnectionDataSource ds = new SingleConnectionDataSource();
ds.setDriverClassName("org.hsqldb.jdbcDriver");
ds.setUrl("jdbc:hsqldb:data/tutorial");
ds.setUsername("sa");
ds.setPassword("");

JdbcTemplate jt = new JdbcTemplate(ds);
jt.execute("insert into employee (id, name) values (?, ?)", 1, "Tom");

Use one jdbcTemplate per DAO class instance.

1 Comment

as U said, We are not allowed to use Mapping or any ORM
0

This may help you

  • Pass all the required values to the respective objects.
  • get all their values and frame a query using those values like

     Employee em = new Employee(perinfo, continfo);
     String emp_per_info = em.getSomeInfo();   // it this returns a string
     ...
     //so on
     query="insert into table_name values(variables go here);"
    

    let me know if it didn't work

4 Comments

Alright, is that possible with UI? if I am going to create a UI?? with it?
Yes, I have done it..with animations.... Its a standalone and its used for maintaining the students records.
em.getSomeINfo() soo for each attribute it will have that?? that' would be long I guess
But what if? I want to create another object? can I override the previous object? can you show me an example? and btw I'll be working on a GUI to do this, so I don't know how to create these objects if I want to add multiple users/employees
0

What you are trying to do is called "Object to Relational Mapping". You have the java Objects which make up your object domain and there is the DB Table which make up the relational model. You have relate them. Save the data and query to get them back. You have two approaches to write a kind of your own limited framework or use an existing framework. Existing frameworks include :

  • Hibernate
  • iBatis and so on.

To do one on your own, simplest way to do would be writing simple result set to object mappers and creating insert strings. I would suggest you study some ORM docs to know more. Regards

1 Comment

can you provide a very simple example of this? let's say I have tables for each object, but without mapping, how would I specifically do that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.