• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to save collection objects in Database

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic