Results 1 to 12 of 12

Thread: object in to Db

  1. #1
    miko5054 is offline Member
    Join Date
    Mar 2010
    Posts
    50
    Rep Power
    0

    Default object in to Db

    i have an obj that contain few details like name and lastname....
    what is the best way to put them into a Db (using jdbc)

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,123
    Rep Power
    20

    Default

    Have a table that holds the fields for your object (assuming it's a pretty basic object)?

  3. #3
    HerrK. is offline Member
    Join Date
    Jun 2010
    Location
    Berlin
    Posts
    22
    Rep Power
    0

    Default

    I think the correct answer might be: depends.

    There is no (correct) way to trivialize how to put things best into a database. The structure of the database might be more important than the way you access the DB from Java.

    But even if we don't take the DB structure into account, the JDBC driver has to, while this might influence the methods that are offered to put data into the database.

    Tolls is mostly right (imho), it should be quite easy to put data just into the table. But while you mentioned that things like name and forename are put into it, you might ensure that you quote any string (for prevention of SQL injection).

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,123
    Rep Power
    20

    Default

    Well, to prevent SQL injection you use a PreparedStatement.
    Concatenating SQL Strings is a Bad Thing in general.

    But yes, without actualy knowing what data is being put into the db, and what other data is already in there, we can't do more than make tentative suggestions.

  5. #5
    miko5054 is offline Member
    Join Date
    Mar 2010
    Posts
    50
    Rep Power
    0

    Default

    i think i fund the solution for insert an object....

    but now i cant connecting to the DB in using ubuntu and tring to connect with this code....

    im trying to connect to an open office Db with the eclipse but i guess im doing something wrong
    this is the connection code
    Java Code:
      try {
    	      Class.forName("org.hsqldb.jdbcDriver");
    	      connection = DriverManager.getConnection("jdbc:hsqldb:file:/home/mikmik/שולחן עבודה/New Database", "SA", "");
    	    } catch (Exception e) {
    	      System.err.println("Unable to find and load driver");
    	      System.exit(1);
    	    }

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,123
    Rep Power
    20

    Default

    And?
    What's the exception?
    Hint: don't just print "I've got an exception" in the catch block, print the stack trace (e.printStackTrace()), and then tell us what it says.

  7. #7
    miko5054 is offline Member
    Join Date
    Mar 2010
    Posts
    50
    Rep Power
    0

    Default

    "Unable to find and load driver"

  8. #8
    miko5054 is offline Member
    Join Date
    Mar 2010
    Posts
    50
    Rep Power
    0

    Default

    java.lang.ClassNotFoundException: org.hsqldb.jdbcDriver
    at java.net.URLClassLoader$1.run(URLClassLoader.java: 217)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.j ava:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:3 21)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:2 66)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:186)
    at dbexemple.Db2.<init>(Db2.java:21)
    at dbexemple.Db2.main(Db2.java:63)
    Unable to find and load driver

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,123
    Rep Power
    20

    Default

    Do you have the jar file?
    If so it needs to be on the runtime classpath.

  10. #10
    miko5054 is offline Member
    Join Date
    Mar 2010
    Posts
    50
    Rep Power
    0

    Default

    which jar file??

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,123
    Rep Power
    20

    Default

    The jar file for the jdbc driver for the database you're using, in this case the hsldb driver.
    The one containg org.hsqldb.jdbcDriver.

  12. #12
    miko5054 is offline Member
    Join Date
    Mar 2010
    Posts
    50
    Rep Power
    0

Similar Threads

  1. Replies: 2
    Last Post: 11-26-2010, 03:12 AM
  2. Replies: 2
    Last Post: 01-29-2010, 03:01 AM
  3. Replies: 1
    Last Post: 03-04-2009, 07:14 PM
  4. Replies: 1
    Last Post: 07-22-2008, 05:27 PM
  5. Replies: 1
    Last Post: 04-17-2008, 11:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •