8 Replies Latest reply: Jun 7, 2007 2:34 AM by 807606 RSS

    database - insert

    807606
      Hello All ...

      I have jsp that displays a table like

      Student Roll Subject

      Dave 1 Math
      John 2 blank

      the user can edit the subject or add new student

      I use transfer objects ...so my question is how to insert/update to the database ...I know the SQL but I'm not able to understand how to pass the parameters to sql using transfer objects....If it is a single row in the table I know how to do ...but not sure how to do for a list of data...

      Thanks,
      greeshma...
        • 1. Re: database - insert
          796440
          http://java.sun.com/developer/onlineTraining/Database/JDBC20Intro/JDBC20.html
          • 2. Re: database - insert
            807606
            http://www.hibernate.org and look for a tutorial, plenty available online. You don't really need SQL that. It's a persistence framework with object - relational mapping

            null
            • 3. Re: database - insert
              796440
              http://www.hibernate.org and look for a tutorial,
              plenty available online. You don't really need SQL
              that. It's a persistence framework with object -
              relational mapping

              null
              I'd say it's pretty important to understand SQL and JDBC before trying to use Hibernate.
              • 4. Re: database - insert
                807606
                true, but I believe setting up a good backend is easier with a hibernate tutorial than with a jdbc + mysql + jndi + ... tutorial.
                • 5. Re: database - insert
                  807606
                  true, but I believe setting up a good backend is
                  easier with a hibernate tutorial than with a jdbc +
                  mysql + jndi + ... tutorial.
                  I'd advise against that, as an experienced J2EE programmer who spent a lot of time fighting with Hibernate and with plenty of JDBC experience...
                  • 6. Re: database - insert
                    807606
                    what do you mean by transfer objects? i mean it is'nt hard at all to update, insert or delete data from a database. I am sure you know how to connect to a database via JDBC? once you are connected then store the sql commands in a string like
                    //assuming you have a table user with fields firstname and lastname and id which is a primary key.
                    String id = request.getParameter("id");
                        String firstName = request.getParameter("firstName");
                        String lastName = request.getParameter("lastName");
                        try {
                                String sql = "UPDATE user" +
                            " SET FirstName='" +firstName + "'," +
                            " LastName='" + lastName + "'," +
                            " WHERE id=" + id;
                             Connection con = DriverManager.getConnection(dbUrl);
                          Statement s = con.createStatement();
                          int i = s.executeUpdate(sql);
                         }
                     
                    not sure if you want this or not.
                    • 7. Re: database - insert
                      807606
                      check this thread

                      http://forum.java.sun.com/thread.jspa?threadID=5180358&tstart=0
                      http://www.sdnshare.com/view.jsp?id=525
                      • 8. Re: database - insert
                        807606
                        Ok I might revisit my suggestion.

                        When using jdbc I recommend using a prepared statement.

                        http://java.sun.com/docs/books/tutorial/jdbc/basics/prepared.html

                        Basically you'll configure your statement once and then loop your collection to set parameters and execute statement for each instance of your ... object.

                        Example for inserts only (update will need other statement)


                        //...
                        PreparedStatement stmt = con.prepareStatement(
                        "INSERT INTO mytable (col1,col2,col3) VALUES(?,?,?)");
                        //...
                        for(Object obj : collection){
                        stmt.setString(1,obj?getStringPramater());
                        stmt.setInt(2,obj.getIntParameter());
                        //...
                        stmt.execute();
                        }

                        If possible look for connection pool and let application server manage.