473,386 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

What is the use of prepareStatement?

Hi,
i have written jsp for retrieving the responseText for Javascript but it throw error as

ERROR
Expand|Select|Wrap|Line Numbers
  1. Type mismatch: cannot convert from String to int
  2. while(rs.next())
  3.  {
  4.    ProjectCod = ""+ rs.getInt(1);
  5.         }
  6.    out.print(ProjectCod);
  7.  
  8.  
my Code comes below

Expand|Select|Wrap|Line Numbers
  1. stateId = Integer.parseInt(request.getParameter("dil_ProjectCode"));
  2.  
  3.        stmt = con.prepareStatement("select projectcode from jerald where projectcode ="+stateId+"");
  4.  
  5.     rs = stmt.executeQuery();
  6.     int ProjectCod ;
  7.     while(rs.next())
  8.     {
  9.  
  10.         ProjectCod = ""+ rs.getInt(1);
  11.         }
  12.     out.print(ProjectCod);
  13. }
  14.  
  15.  
Jan 21 '11 #1
10 2497
nathj
938 Expert 512MB
Not sure I know what the question is here. If it is about prepareStatement this is used to do just that it prepares a SQL statement for use with your database connection. Basically, reading the code you see that you have a connection, you know the SQL you want to run so you prepare the SQL with the connection giving you a Statement object that contains a query which you can execute to return a resultSet.

If you have a question about converting String to int then you need to use the parseInt function something like:
Expand|Select|Wrap|Line Numbers
  1. String i = "12"
  2. String output = "the integer is " + i; // using a string as a string
  3. System.out.println(output);
  4.  
  5. int i2 = 13;
  6. // convert i to int to do some maths
  7. Integer result = Integer.parseInt(i) + i2;
  8. // output as a string
  9. system.out.println("Result: " + result.toString());
  10.  
This should work for you. If I have misunderstood the question then please post back and I'll happily try again.

Thanks
nathj
Jan 21 '11 #2
Hi Nathj thanks for ur reply..,

now i'm getting no error,but it display a blank window
it is not entering into while(rs.next){ please help me

JavaScript Code
Expand|Select|Wrap|Line Numbers
  1. function loadContent(formj)
  2. {
  3.  
  4.  xmlhttp=GetXmlHttpObject();
  5.  
  6.   if (xmlhttp==null)
  7.   {
  8.    alert ("Your browser does not support Ajax HTTP");
  9.    return;
  10.   }
  11.  
  12.     var stateValue = formj.dil_ProjectCode.value;
  13.     alert(stateValue);
  14.     var url="Code.jsp";
  15.     url=url+"?dil_ProjectCode="+stateValue;
  16.     alert(url);
  17.     xmlhttp.onreadystatechange=getOutput;
  18.     xmlhttp.open("GET",url,true);
  19.     xmlhttp.send(null);
  20. }
  21.  
  22.  
  23. function getOutput()
  24. {
  25.   if (xmlhttp.readyState==4)
  26.   {
  27.  
  28.      var result = xmlhttp.responseText;
  29.     alert(result);
  30.   }
  31. }
  32. function GetXmlHttpObject()
  33. {
  34.     if (window.XMLHttpRequest)
  35.     {
  36.        return new XMLHttpRequest();
  37.     }
  38.     if (window.ActiveXObject)
  39.     {
  40.       return new ActiveXObject("Microsoft.XMLHTTP");
  41.     }
  42.  return null;
  43. }
  44.  
  45.  
Form

Expand|Select|Wrap|Line Numbers
  1. <input type="text" name="dil_ProjectCode" id="dil_ProjectCode" >
  2.  
Code.jsp
Expand|Select|Wrap|Line Numbers
  1. stateId = request.getParameter("dil_ProjectCode");
  2.  
  3.     String query = "select projectcode from jerald where projectcode ="+stateId+"";
  4.     stmt = con.prepareStatement(query);
  5.  
  6.     rs = stmt.executeQuery();
  7.  
  8.     while(rs.next())
  9.     {
  10.  
  11.     String ProjectCod = ""+ rs.getString(1);
  12.         }
  13.     out.print(ProjectCod);
  14. }
  15.  
Please Help me out..,
Jan 22 '11 #3
nathj
938 Expert 512MB
Hi,

Try something like this:
Expand|Select|Wrap|Line Numbers
  1. PreparedStatement statement = new PreparedStatement("select projectcode from jerald where projectcode = ?");
  2. statement.setString(1, stateId);
  3.  
  4. ResultSet rs = statement.executeQuery();
  5.  
This should give you what you are after.

Thanks
nathj
Jan 24 '11 #4
Hi Nathj Thanks for ur reply.I'm newly joined in the Company.
i need complete this form as soon as possible.
i stick with this error for 3 day's.
all the steps are completed in the Form. this one ERROR ONLY.., please help me Out..

No Boss ur Code is not working..
But now i Found out the error.,
Same code only..
in Code.jsp here i'm not getting any value i don't Y..,
But this is the ERROR
CODE.jsp
Expand|Select|Wrap|Line Numbers
  1.  
  2. stateId = request.getParameter("dil_ProjectCode");
  3.  
  4.  
Jan 24 '11 #5
nathj
938 Expert 512MB
Hi,

I'm sorry to say I'm not sure I follow what the problem is here.

Can you let me know what you are expecting, what you are actually getting and what line you think or know is causing the problem.

Cheers
nathj
Jan 24 '11 #6
Hi nathj,
Sorry 4 disturbing u again..,

i need to get input from the user(text box field value) and check whether the input is given by the user is in the database or not.

if the user entered the input in the database i need to give the alert msg as "value is already exist's" else {
it needs to save into database..(this is i want),
any IDEA..,



Existing post say's i need to get matched data from the database in alert msg.
Next STep i need to give condition to check and giving alert msg.

But the response it self is not coming.So if the response is come means my work is finished.

once again thank's nathj..,
Help me oUt..,
Jan 25 '11 #7
nathj
938 Expert 512MB
Hi,

Don't worry about disturbing me - I make time each day to help other out.

Now it seems to me that you want to have an onChange() event that calls some AJAX. This would then pass the value of the textbox to some Java function that checks the database for the value. If the value is found you return a message that the Javascript function writes out next to the text box . If the value is not found you write a different message out to explain that all is well.

I would check out some tutorials on AJAX and JSP. Take a look at this Google Search. Getting an understanding of what is going on will give you a good foundation for future projects.

Cheers
nathj
Jan 25 '11 #8
hi nathj,

Now i change my idea in coding.
Form Code
Expand|Select|Wrap|Line Numbers
  1. <form name="form1" action="http://localhost:8085/NewForm/dil_ProjectMaster.jsp" method="POST" onsubmit="return checkform(this) && Dat(this);">
  2.  
<javascript>
Expand|Select|Wrap|Line Numbers
  1. <%
  2. if(request.getAttribute("jerald")!=null)
  3. {%>
  4. alert("Project Code is Already is in the Database");
  5.  
  6. <%}
  7.  

server Code:
Expand|Select|Wrap|Line Numbers
  1. Connection con = null;
  2. PreparedStatement stmt=null;
  3. PreparedStatement stmtRead=null;
  4. //Statement stmt=null;
  5. ResultSet rs1=null;
  6. int updateQuery = 0;
  7.  
  8.  
  9.  
  10. Connection con = null;
  11. PreparedStatement stmt=null;
  12. PreparedStatement stmtRead=null;
  13. //Statement stmt=null;
  14. ResultSet rs1=null;
  15. int updateQuery = 0;
  16.  
  17.  
  18.  
  19.  
  20. try{
  21.   DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
  22.  
  23. con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","distil","distil");
  24.  
  25.  
  26.       String queryRead = "select projectcode from DIL_PROJECTMASTER where projectcode ='"+proCode+"'";
  27.  
  28.     stmtRead = con.prepareStatement(queryRead);
  29.  
  30.     rs1 = stmtRead.executeQuery();
  31.  
  32.  
  33.  
  34.     if(rs1.next())
  35.     {
  36.  
  37.         request.setAttribute("jerald","Record is there");
  38.  
  39.         %><jsp:include page="/Form1.jsp"/><%}
  40.     else
  41.     {
  42.  
  43.  
  44.  
  45. stmt = con.prepareStatement("INSERT INTO DIL_PROJECTMASTER(PROJECTCODE,PROJECTNAME,WORKLEVEL1,WORKLEVEL2,WORKLEVEL3,NOTES,PROJECTSTDATE,PROJECTENDATE) values(?,?,?,?,?,?,?,?)");
  46.  
  47.  
  48.             stmt.setInt(1,proCode);
  49.  
  50.             stmt.setString(2,proName);
  51.  
  52.             stmt.setString(3,option1);
  53.  
  54.             stmt.setString(4,option2);
  55.  
  56.             stmt.setString(5,option3);
  57.  
  58.             stmt.setString(6,notes);
  59.  
  60.             stmt.setDate(7,new java.sql.Date(result1.getTime()));
  61.  
  62.             stmt.setDate(8,new java.sql.Date(result2.getTime()));
  63.  
  64. updateQuery=stmt.executeUpdate();
  65.  
  66.     stmt.close();
  67. //con.close();
  68.  
  69.  
  70.  
  71. if( updateQuery != 0)
  72.     {
  73.  
  74.  
  75. %>
  76.     <jsp:forward page="/TimeForm.jsp"/>
  77. <%
  78.  
  79. }
  80. }
  81. }
  82. catch (SQLException ex){
  83. System.err.println(ex.getMessage());
  84. }
  85. catch (Exception e){
  86. e.printStackTrace();
  87. }
  88. finally{
  89.     con.close();
  90. }
  91. %>
  92.  
the alert msg is coming in server page but i want that alert msg to come in form.
wat i need to do..,
Jan 31 '11 #9
nathj
938 Expert 512MB
Hi,

I noticed on you form you have the onsubmit returning the results of boolean addition:
Expand|Select|Wrap|Line Numbers
  1. checkform(this) && Dat(this)
  2.  
I assume the javascript you posted relates to checkform what does dat() do?

Also, on the form you need to have an element - prehaps a div or a span that the warning or success messgae can be posted into. Then with the onchange event of the textbox you can call the AJAX to find if the data is valid. The server side codes returns either true or false. The Javascript then uses
Expand|Select|Wrap|Line Numbers
  1. // snippet
  2. var target = document.getElementById(outputMsg);
  3. if(valid == true)
  4. {
  5. target.innerHTML='<p class="success">valid</p>';
  6. }
  7. else
  8. {
  9. target.innerHTML='<p class="fail">invalid</p>';
  10. }
  11.  
That little snippet would go in the Javascript that is called by the onchange event of the text box. Alternativley you could have a simple button next to the text box that the user click to check the name.

Cheers
Nathan
Feb 1 '11 #10
Hi Nathan,
Thanks i completed that form.i used setAttribute and getAttribute to display the alert msg in client side itself.once again thanks to u for helping me.
change's of the code are following

Server Side Code
Expand|Select|Wrap|Line Numbers
  1. try{
  2.   DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
  3.  
  4. con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","username","password");
  5.  
  6.  
  7.     String queryRead = "select projectcode from DIL_PROJECTMASTER where projectcode ='"+proCode+"'";
  8.  
  9.     stmtRead = con.prepareStatement(queryRead);
  10.     rs1 = stmtRead.executeQuery();
  11.  
  12.     if(rs1.next())
  13.     {
  14.     request.setAttribute("jerald",proCode);
  15.     String strViewPage="Form1.jsp";
  16.     RequestDispatcher dispatcher =  request.getRequestDispatcher(strViewPage);
  17.  if (dispatcher != null){
  18.  dispatcher.forward(request, response);
  19. }
  20. %><jsp:include page="/Form1.jsp"/><%}
  21. else
  22. {
  23.  
  24. stmt = con.prepareStatement("INSERT INTO DIL_PROJECTMASTER(PROJECTCODE,PROJECTNAME,WORKLEVEL1,WORKLEVEL2,WORKLEVEL3,NOTES,PROJECTSTDATE,PROJECTENDATE)  values(?,?,?,?,?,?,?,?)");
  25.     stmt.setInt(1,proCode);
  26.         stmt.setString(2,proName);
  27.         stmt.setString(3,option1);
  28.     stmt.setString(4,option2);
  29.     stmt.setString(5,option3);
  30.     stmt.setString(6,notes);
  31.     stmt.setDate(7,new java.sql.Date(result1.getTime()));
  32. stmt.setDate(8,new java.sql.Date(result2.getTime()));
  33. updateQuery=stmt.executeUpdate();
  34. stmt.close();
  35.  
  36. if( updateQuery != 0)
  37. {
  38. %>
  39.  <jsp:forward page="/TimeForm.jsp"/>
  40. <%
  41.  
  42. }
  43. }
  44. }
  45.  

this code i putted inside javascript(Form.jsp)
Expand|Select|Wrap|Line Numbers
  1. <%
  2. if(request.getAttribute("jerald")!=null)
  3. {%>
  4. alert("Project Code is Already is in the Database");
  5.  
  6. <%}
  7. %>
  8.  
this is i changed in my program..,
Once again thanks for helping me THANKS..,

they gave me another Login form(admin side,projectuser side) Exercise. i want create it and join with exist's form.

i want same help in tat process also please...,
Bye Take Care..
Feb 1 '11 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: thecrow | last post by:
Alright, what the hell is going on here? In the following code, I expect the printed result to be: DEBUG: frank's last name is burns. Instead, what I get is: DEBUG: frank's last name is...
220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
92
by: Reed L. O'Brien | last post by:
I see rotor was removed for 2.4 and the docs say use an AES module provided separately... Is there a standard module that works alike or an AES module that works alike but with better encryption?...
137
by: Philippe C. Martin | last post by:
I apologize in advance for launching this post but I might get enlightment somehow (PS: I am _very_ agnostic ;-). - 1) I do not consider my intelligence/education above average - 2) I am very...
12
by: Dario | last post by:
The following simple program behaves differently in Windows and Linux . #include <stdexcept> #include <iostream> #include <string> using namespace std; class LogicError : public logic_error {...
2
by: CM | last post by:
Hi, I found this post in a forum. What do you think about it? :) Christophe ---------------------------------------------------------------------------- ----
125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
6
by: Zhang Weiwu | last post by:
Hello. I am working with a php software project, in it (www.egroupware.org) Chinese simplified locate is "zh" while Traditional Chinese "tw". I wish to send correct language attribute in http...
47
by: Neal | last post by:
Patrick Griffiths weighs in on the CSS vs table layout debate in his blog entry "Tables my ass" - http://www.htmldog.com/ptg/archives/000049.php . A quite good article.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.