• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

PreparedStatement-Statement

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friends,
I am very confuse about the issue..that i am facing.
friends i am trying to use both Preparestatement & Statement in the same program.
Is it poss?
Please guide me..
thanks in advance
 
Ranch Hand
Posts: 1325
Android Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by ashirvad jain:
I am very confuse about the issue..that i am facing.



What's the problem? Are you getting a compiler error or any Exception. Provide some details so we can help.

Originally posted by ashirvad jain:
i am trying to use both Preparestatement & Statement in the same program.Is it poss?



Yeah it is

[ January 10, 2007: Message edited by: Saif uddin ]
[ January 10, 2007: Message edited by: Saif uddin ]
 
ashirvad jain
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<%@page import ="java.sql.*" %>
<%@page import="java.util.*" %>



<%!

Connection conn=null;
Statement stmt=null;
PreparedStatement ps=null;

ResultSet rs=null;

String connURL="jdbc:mysql://localhost:3306/customer";
int customer_id=0;
int amount=0;
int flag=1;
int tempAmount=0;
%>

<%


customer_id=Integer.parseInt(request.getParameter("custid"));
amount=Integer.parseInt(request.getParameter("amt"));
try
{

Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection(connURL,"root","root");

stmt=conn.createStatement();
rs=stmt.executeQuery("SELECT *FROM cust");
ps=conn.prepareStatement("UPDATE cust SET amount=? WHERE customerid=?");
out.println("connection made");


while(rs.next())
{
int custid=rs.getInt("customerid");

if(custid==customer_id)
{
flag=0;

out.println("statement execute");
tempAmount=rs.getInt("amount");
out.println("amount" +tempAmount);
tempAmount=tempAmount+amount;
out.println("amount" +tempAmount);
//ps.setInt(1,customer_id);
ps.setInt(4,21000);

out.println(" update is made");
ps.executeUpdate();
out.println("Even update is made");
}

}

rs.close();
conn.close();

out.println("connection closed");
}
catch(Exception e)
{
e.printStackTrace();
}

%>

hI I AM sending the code basically its in JSp ..but it simple coz i am using any scriptlet and in which i am doing all the logic work...

and even i sending the Url det i am using for this application..

http://localhost:8080/try.jsp?custid=2&amt=350

and there is no error only o/p til
connection made statement execute amount20000 amount20350

measn it accessing the data but cudnt update though PreparedStatement..

Hope this info help you to narrow your solution..

Again ,
Thanks in advance
 
Muhammad Saifuddin
Ranch Hand
Posts: 1325
Android Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


place holder (?) in PreparedStatement is defining location of your field in your set type methods you passing to value for Example..



hope it helps..
 
ashirvad jain
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi saif ..
Atually that was my mistake ,
Even later i figure it out and make it clear by..

ps.setInt(1,customer_id);
ps.setInt(4,tempAmount);

but stil the problem remain same..

And even i used the rs.updateInt(1,customer_id) rs.updateInt(4,tempAmount)
rs.updateRow();
by doing simple changes but stil the probles remain same.

And saif we are using mysql5.0.
And even we try without PreparedStatement and we got some sucess but not 100%.
Coz in that we observer we cant insert a single column ,but we have to insert the complete the row to get update..

Saif i am really appriciate your help ..
And need more help regrading the PreparedStatement and Statement in the same application...
 
ashirvad jain
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI saif ,

I am very glad by your response,
And i made some changes according to your advice ..and got 100%success.

Do you have any idea about the mysql 5.0
Actually we are getting insertion error in the same application..
Coz last time we changed it into

stmt2.executeUpdate("INSERT INTO cust(customername,customerid,customeraddress,amount)"+" VALUES ('"+name+"',"+id+",'"+add+"',"+tempAmount+")");

in this cases we are getting success cause we are specifying the completer schema,
But whenever we try for single column the error remain same..means no change,

this is the query we are specifying,

stmt2.executeUpdate("INSERT INTO cust(amount,tempAmount)"+" VALUES (amount,"+tempAmount+")");

So its creating issue..

We are solving the issue ...and hope you can help a bit if you know about it..

But no Issue..
We are very happy that you solve are first Prob..

Thanks .
 
Muhammad Saifuddin
Ranch Hand
Posts: 1325
Android Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by ashirvad jain:



try with this query..
 
Muhammad Saifuddin
Ranch Hand
Posts: 1325
Android Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check this link for more about Sql Insert Query..
SQL INSERT
[ January 10, 2007: Message edited by: Saif uddin ]
 
ashirvad jain
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi saif i went through diff tutorial..
But not finding anyway to update perticular column.But no issue cause we solved the issue by using your prev solution..
ANyways..great help from you,

Thanks alot again..
Take care
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic