Jump to content

Java Servlet database transaction problem

- - - - -

  • Please log in to reply
2 replies to this topic

#1
mutago

mutago

    Programmer

  • Members
  • PipPipPipPip
  • 102 posts
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){

                            

                            }


		}

       	}











#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 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
You are initializing Price inside a try/catch block. If an error occurs, Price will not be initialized.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
mutago

mutago

    Programmer

  • Members
  • PipPipPipPip
  • 102 posts
okay what do i do
thanks




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users