Results 1 to 6 of 6
  1. #1
    JDev_Beginner is offline Member
    Join Date
    Jul 2012
    Posts
    2
    Rep Power
    0

    Default SQL Query String 'Select'

    Dear professionals,

    I am currently developing an application which need to retrieve some data from the MySQL database using JAVA. But there is a little problem.

    My Java Code:
    Java Code:
    package SearchLicense.servlets;
    
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import javax.sql.*;
    import java.sql.*;
    public class Search extends HttpServlet{ 
     public void doPost(HttpServletRequest request, HttpServletResponse response)
       throws ServletException,IOException{
      response.setContentType("text/html");
      PrintWriter out = response.getWriter();
      System.out.println("MySQL Connect Example.");
      Connection conn = null;
      String url = "jdbc:mysql://localhost:3306/";
      String dbName = "lic";
      String driver = "com.mysql.jdbc.Driver";
      String userName = "root"; 
      String password = "root";
     String licenseename="";
     String businessname="";
     String licenseno="";
     String strQuery= ""; 
      Statement st=null;
      ResultSet rs=null;
      HttpSession session = request.getSession(true);
      try {
      Class.forName(driver).newInstance();
      conn = DriverManager.getConnection(url+dbName,licenseeName,businessName,licenseNo);
      if(request.getParameter("licenseename")!=null &&
         request.getParameter("licenseename")!="" && request.getParameter("businessname")!=null &&
         request.getParameter("businessname")!=""&& request.getParameter("licenseno")!=null &&
         request.getParameter("licenseeno")!="")
      {
      licenseename = request.getParameter("licenseename").toString();
      businessname = request.getParameter("businessname").toString();
      licenseno=request.getParameter("licenseno").toString();
      
     strQuery="select * from t15_00034, t15_00033, t01_15050, t15_00030, t01_15010 where
      t15_00010.LICENSEE_NAME='"+licenseename+"' and t15_00020.BUSINESS_NAME='"+businessname+"'and T15_00030.LIC_NAME='"+licenseno+"'";
     System.out.println(strQuery);
      st = conn.createStatement();
      rs = st.executeQuery(strQuery);
      int count=0;
      while(rs.next())
      {
      session.setAttribute("licenseename",rs.getString(2));
      count++;
      }
      if(count>0)
      {
      response.sendRedirect("Welcome.jsp");
      }
      else
      {
     response.sendRedirect("search.jsp");
      }
      }
      else
      {
     response.sendRedirect("search.jsp");
      }
      System.out.println("Connected to the database"); 
      conn.close();
      System.out.println("Disconnected from database");
      } catch (Exception e) {
      e.printStackTrace();
      }
      }
    }
    This line strQuery="select * from t15_00034, t15_00033, t01_15050, t15_00030, t01_15010 where
    t15_00010.LICENSEE_NAME='"+licenseename+"' and t15_00020.BUSINESS_NAME='"+businessname+"'and T15_00030.LIC_NAME='"+licenseno+"'";
    at the code above give an error of "String literal is not properly closed by a double-quote". How can I solve it?
    Please guide. Thanks in advance.

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    8

    Default Re: SQL Query String 'Select'

    You cannot have a multiple lines of string in Java. If you want to then you have to concatenate those strings.

    Java Code:
    strQuery="select * from t15_00034, t15_00033, t01_15050, t15_00030, t01_15010 where " +
      "t15_00010.LICENSEE_NAME='"+licenseename+"' and t15_00020.BUSINESS_NAME='"+businessname+"'and T15_00030.LIC_NAME='"+licenseno+"'";
    The other thing is that the DriverManager.getConnection() method can accept three argument, the jdcb url, username and password. In your code you passes four arguments. This will not work. And you also pass the wrong information. You also need to know that Java is case sensitive; licenseename != licenseeName.
    Last edited by wsaryada; 07-25-2012 at 09:46 AM.

  3. #3
    JDev_Beginner is offline Member
    Join Date
    Jul 2012
    Posts
    2
    Rep Power
    0

    Default Re: SQL Query String 'Select'

    Quote Originally Posted by wsaryada View Post
    You cannot have a multiple lines of string in Java. If you want to then you have to concatenate those strings.

    Java Code:
    strQuery="select * from t15_00034, t15_00033, t01_15050, t15_00030, t01_15010 where " +
      "t15_00010.LICENSEE_NAME='"+licenseename+"' and t15_00020.BUSINESS_NAME='"+businessname+"'and T15_00030.LIC_NAME='"+licenseno+"'";
    Thank you.

    The other thing is that the DriverManager.getConnection() method can accept three argument, the jdcb url, username and password.
    The username and password mentioned is it the username and the password of the database?

  4. #4
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    8

    Default Re: SQL Query String 'Select'

    Yes, it should be the username and password of the database. I saw in the code that you have the userName and password variables. These information that should be use when obtaining a connection from the DriverManager.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default Re: SQL Query String 'Select'

    You wouldn't have had this problem if you had used a PreparedStatement rather than concatenating your query together.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Fast VPN ?

    Quote Originally Posted by LuisRodrig View Post
    Sorry if i have posted in a wrong area.
    Yes, you have. This isn't a spam forum.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

Similar Threads

  1. Multiple SELECT Query issue
    By WishBone101 in forum New To Java
    Replies: 2
    Last Post: 06-19-2012, 10:41 AM
  2. Problem executing SELECT query
    By mike_ledis in forum JDBC
    Replies: 2
    Last Post: 04-26-2011, 05:30 AM
  3. SELECT FROM WHERE query
    By herfnai in forum JDBC
    Replies: 3
    Last Post: 11-24-2008, 11:04 AM
  4. problem in select query
    By herfnai in forum JDBC
    Replies: 0
    Last Post: 08-17-2008, 11:47 AM
  5. Using a variable in a SELECT FROM WHERE query
    By cplmckenzie in forum JDBC
    Replies: 12
    Last Post: 04-23-2008, 03:24 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •