Reputation: 119
my database consist of 2 columns username and balance. username has the value Mike, and balance has the value null. i am trying to update balance, but for some reason it doesn't update. also i don't get any errors. any suggestions
<p>This is the deposit page</p>
<form action="deposit.jsp" method="POST">
<label>deposit: </label><input type="text" name="deposit"><br>
<label>name: </label><input type="text" name="name"><br>
<input type="submit" value="deposit">
<%
String deposit=request.getParameter("deposit");
String name=request.getParameter("name");
try{
Connection conn = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
Statement st=null;
st=conn.createStatement();
st.executeUpdate("update username set balance='"+deposit+"' where username='"+name+"'");
}
catch(Exception e){
System.out.println(e);
}
%>
</form>
</body>
</html>
Upvotes: 0
Views: 864
Reputation: 28389
If I understand what's happening here correctly, you're actually calling the db insert as soon as the page loads (not when the user hits submit)... In which case your sql is going to looking for a username = '', right? Maybe I'm not understanding what you're doing. and you need to call commit() on the whole shebang, but that's after you actually give it some useful values.
[edit..] oh I see you're calling the same page twice, depending on if the user hits submit... this is an INCREDIBLY bad way to handle this sort of thing... As a test to see if you can put stuff in your database it's fine, but you really must never build anything real like this. Keep your presentation and business logic separate at all costs. I know that JSP (and PHP and all the other template engines) give you the ability to merge them, but just because you "CAN" do something, doesn't mean that you should.
[EDIT 2]
We can't see your database, but do you really have a table called username in which you have a column called username? Cause that's what you're talking to right now. update [TABLE NAME]
. I'm guessing this is failing cause you're trying to write to a non-existant table.
oh and commit is called after update.
Upvotes: 1
Reputation: 171
Better if you create two pages so that the query runs only once. First page includes the input fields like in your case name and balance. So after submitting the form forward it to the next page where you can put the code you write in the scriptlet.
Also write System.out.println(e.getmessage())
. So you can see the proper message.
And also try your query in Mysql browser to confirm that there is not spelling mistake in your query.
Upvotes: 1
Reputation: 42060
The first time you load this page attempts to update the following:
update username set balance='null' where username='null';
At least there is a user with this information and are of type char
, nothing will happen. On the other hand, using scriptlets
today is considered bad practice. I recommend that you be applying the MVC design pattern.
There is an excellent answer JSP using MVC and JDBC.
Upvotes: 1