Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Java Servlet database transaction problem

servlet

  • Please log in to reply
2 replies to this topic

#1 mutago

mutago

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 478 posts

Posted 23 March 2011 - 01:51 PM

Good day everybody, This code was aimed in performing price transaction but when i run it it display an error shown below


C:\Tomcat 6.0\webapps\cyberworld\WEB-INF\classes>javac check.java
check.java:61: variable Price might not have been initialized
                                Price=Price-int3;
                                      ^
check.java:64: variable int1 might not have been initialized
                                "price='" +Price+"' WHERE password="+int1;
                                                                     ^
check.java:104: variable Price1 might not have been initialized
                                Price1=Price1+int3;
                                       ^
check.java:107: variable int2 might not have been initialized
                                "price='" +Price1+"' WHERE password="+int2;
                                                                      ^
4 errors

C:\Tomcat 6.0\webapps\cyberworld\WEB-INF\classes>

below is the main code thanks



import java.io.*;
import java.lang.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;


public class Check extends HttpServlet{
  public void doPost(HttpServletRequest request, HttpServletResponse response)
                                 throws ServletException, IOException{
    response.setContentType("text/html");
    PrintWriter pw = response.getWriter();

 int int1,int2,int3,Price,Price1,Password;
 String str1,str2,str3;

String url = "jdbc:mysql://localhost:3306/test";
 String user = "root";
 String passer ="root"; 



                     	try{
                        	Class.forName("com.mysql.jdbc.Driver");
                            }catch(ClassNotFoundException a){
                            	
                            }
                      

                        try{      

 
                                str3 = request.getParameter("str3");
                        	 int1=Integer.parseInt(str3);
                        	String run = "SELECT * FROM try " +"WHERE password = " +int1;
                        	System.out.println(run + "SQL string executed");
                        	Connection connection = DriverManager.getConnection(url,user,passer);
  	                     	Statement stmt = connection.createStatement();
                        	ResultSet result=stmt.executeQuery(run);
                                while(result.next()){
                        	String name = result.getString(1);
                        	String surname = result.getString(2);
                        	 int price = result.getInt(3);
                        	 int password = result.getInt(4);
                          
                        	Price=price;
                              }
                         	connection.close();
                            }
                            catch(SQLException a){
                            	
                            }

                        	  // price sent
                                str2 = request.getParameter("str2");
                        	int3=Integer.parseInt(str2);
                               

                        	Price=Price-int3;
                                //--------------Updating values of the database-------------
                        	String run =  "UPDATE try SET " +
                        	"price='" +Price+"' WHERE password="+int1;
                        try{
                        	Connection connection = DriverManager.getConnection(url,user,passer);
	      	            	Statement stmt = connection.createStatement();
      		            	stmt.executeUpdate(run);
                        	System.out.println("Operation completed...");
      		            	connection.close();
                            }catch(SQLException a){
                         
                            }
                        //---Select operation------
                        try{
                        	Class.forName("com.mysql.jdbc.Driver");
                            }catch(ClassNotFoundException a){
                            	
                            }
                        try{

                                str1 = request.getParameter("str1");
                        
                        	int2=Integer.parseInt(str1);
        
                        	String run2 = "SELECT * FROM try " +"WHERE password = " +int2;
                        	System.out.println(run2 + "SQL string executed");
                        	Connection connection = DriverManager.getConnection(url,"user","passer");
  	                     	Statement stmt = connection.createStatement();
                        	ResultSet result=stmt.executeQuery(run2);
                              while(result.next()){
                        	String name = result.getString(1);
                        	String surname = result.getString(2);
                        	 int price = result.getInt(3);
                        	 int password = result.getInt(4);

                        	Price1=price;
                              }
                        	connection.close();
                            }
                            catch(SQLException a){
                            	
                            }
                        	Price1=Price1+int3;
                                //---------Updating the values --
                        	String run1 =  "UPDATE try SET " +
                        	"price='" +Price1+"' WHERE password="+int2;
                        try{
                        	Connection connection = DriverManager.getConnection(url,user,passer);
	      	            	Statement stmt = connection.createStatement();
      		            	stmt.executeUpdate(run1);
                        	System.out.println("Operation completed...");
      		             	connection.close();
                            }catch(SQLException a){
                            
                            }

		}
       	}









  • 0

#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 23 March 2011 - 02:47 PM

You are initializing Price inside a try/catch block. If an error occurs, Price will not be initialized.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 mutago

mutago

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 478 posts

Posted 23 March 2011 - 08:12 PM

okay what do i do
thanks
  • 0





Also tagged with one or more of these keywords: servlet

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download