Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to save collection objects in Database

 
M. Dooze
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all, I am having a little issue with saving a set into an oracle db using plain JDBC.
I have a transfer object LoggedInUserInfo and want to persist its filds in my login table but can't find any way to handle the collection part.

Here's the object

public class LoggedInUserInfo implements Serializable
{
private String userName;
private Date loginTime;
private String remoteAddress;

private Set <String> permissions;


public LoggedInUserInfo(String userName)
{
this.userName = userName;
this.loginTime = new Date();
}


public Date getLoginTime()
{
return loginTime;
}

public String getUserName()
{
return userName;
}


public String getRemoteAddress()
{
return remoteAddress;
}
public Set<String> getPermissions()
{
return permissions;
}

In my dao class, I issue a prepared statment and proced to set its place holders like so:

LoggedInUserInfo info --my transfer object that I want to save.
PrepareStatement loginStmt--my prepared statement

loginStmt.setString(0,info.getUsername());
..
loginStmt.setxx(1, info.getPermissions)....DOESN'T WORK HERE..WHICH METHOD CAN I USE, HOW ELSE CAN I DO THIS? setArray() is not working either...
 
M. Dooze
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
never mind..I figured out the appropriate method..so i wanted to share it for anyone who may have the same issue:

loginStmt.setObject(2, info.getPermissions()); will do...

For those who aren't that familiar with jdbc placehoders, the number in this methos just refers to the location of the ?-question marke(ie the place holder) that you are setting value for in the preparedstatement...
you create the preparestatment like so:

private String loginStr = "insert insert into vn_wsg_login (user_name, remote_address, permissions, token, login_time, last_authenticated) " +
"values (?, ?, ?, ?, ?, ?)";


con = getConnection(); //my own method created for use

loginStmt = con.prepareStatement(loginStr); //this is where I am creating the prepared statement

loginStmt.setString(0,info.getUserName()); //zero for the firs ?
loginStmt.setString(1, info.getRemoteAddress()); //1 for the 2nd ? and
loginStmt.setObject(2, info.getPermissions()); //2 for the 3rd one..

good luck!
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"dooze dooze",
Welcome to the JavaRanch.

We're a friendly group, but we do require members to have valid display names.

Display names must be two words: your first name, a space, then your last name. Fictitious names are not allowed.

Please edit your profile and correct your display name since accounts with invalid display names get deleted.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic