Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Update database with Bean

Status
Not open for further replies.

Khanjan

Programmer
Feb 24, 2004
41
NL
Hi,

What is wrong with this code? I want to update database with this code.

The bean code:
public void UpdateNieuws() {

try{
db.connect();
String sql = "Update Nieuws set Nieuwsid '"+nieuwsid+"' " +
"Titel ='"+titel+"', Content ='"+content+"', auteur='"+auteur+"'"+
"datum='"+datum+"', nieuwstype ='"+nieuwstype+"'"+
"where Titel ='"+titel+"';";

db.query(sql);
}
catch (Exception e) {System.out.print("error");}


The JSP code is:
<jsp:useBean id="updatenieuws" class="bean.Nieuws" scope="request"/>
<jsp:setProperty name="updatenieuws" property="*"/>
<% updatenieuws.setTitel(titels); %>
<% updatenieuws.UpdateNieuws(); %>
 
Hi,

In the String sql you are missing = after Nieuwisd and there is no need of ; with in the string.

SQL should be

String sql = "Update Nieuws set Nieuwsid='"+nieuwsid+"' " +
"Titel ='"+titel+"', Content ='"+content+"', auteur='"+auteur+"'"+
"datum='"+datum+"', nieuwstype ='"+nieuwstype+"'"+
"where Titel ='"+titel+"'";

Cheers
Venu
 
Venu Thanx for your comment,

I had forgotten the = symbol.

I know mine sql statment works perfect, when i am using it in Mysql. But when i am calling in jsp the UpdateNieuws()methode, there is no update at the database. I have put System.out.println in the bean.

The Update Methode:
public void UpdateNieuws() {

try{ db.connect();
String sql = "Update Nieuws set Titel ='"+titel+"', Content ='"+content+"', auteur='"+auteur+"'"+
"datum='"+datum+"', nieuwstype ='"+nieuwstype+"'"+
"where NIEUWSID ='"+nieuwsid+"'";

System.out.println("Content|" + sql + "|");
db.query(sql);
}
catch (Exception e) {System.out.print("error");}
}

I do this firs in JSP:
<% String titels = request.getParameter("titel");%>
<% String content = request.getParameter("content");%>
<% String nieuwsid = request.getParameter("nieuwsid");%>

<% String auteur = request.getParameter("auteur");%>
<% String datum = request.getParameter("datum");%>
<% String nieuwstype = request.getParameter("nieuwstype");%>

AND then, i call the update methode:

<jsp:useBean id="show"class="bean.Nieuws"scope="request"/>
<jsp:setProperty name="show" property="*"/>
<%
show.setNieuwsID(Integer.parseInt(nieuwsid));
show.UpdateNieuws();


%>

At the systeem.out i get this:

Content |Update Nieuws set Titel='bla',Content='dd',auteur='aa',datum='2003-3-2',
nieuwstype='ddd' where nieuwsid='2'|


CAN ANYONE TELL ME, WHY I CANT UPDATE DATABASE WITH A BEAN FROM JSP PAGE?
 
See what mysql is telling you :

Code:
catch (Exception e) {
   System.out.print("error");
   System.out.printe.getMessage() ) ;
}

Maybe your bumping into something on the mysql side.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top