database - insert
Hello All ...
I have jsp that displays a table like
StudentRollSubject
Dave 1Math
John 2blank
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...
http://java.sun.com/developer/onlineTraining/Database/JDBC20Intro/JDBC20.html
jverda at 2007-7-12 18:06:26 >

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 mappingnull
> 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.
jverda at 2007-7-12 18:06:26 >

true, but I believe setting up a good backend is easier with a hibernate tutorial than with a jdbc + mysql + jndi + ... tutorial.
> 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...
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.
check this thread http://forum.java.sun.com/thread.jspa?threadID=5180358&tstart=0 http://www.sdnshare.com/view.jsp?id=525
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.