Results 1 to 6 of 6
  1. #1
    dilanka007 is offline Member
    Join Date
    Jan 2010
    Posts
    11
    Rep Power
    0

    Exclamation jdbc problem!!! pls help

    this is a code i am working for a coursework...
    the problem is in the isLogged method. the line starting from ResultSet res...
    when i put just ResultSet res = stmt.executeQuery("SELECT * FROM user ";); all works fine.. as i have records in this table..

    But when i do as i ve done there.. i think the result set isn't generating properly..
    have i done anything wrong while writing the query..
    i have 2 records containing the firs_name as ashanthi in my database..i am sure i have done something wrong while writing the query..


    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    package DAO;

    import java.sql.Statement;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.logging.Level;
    import java.util.logging.Logger;

    /**
    *
    * @author dilanka
    */
    public class UserDA {



    Connection conn = null;
    String userName ="root";
    String password = "dilan";
    String url = "jdbc:mysql://localhost:3306/bidbizz";
    public boolean resultSetReturned;

    public UserDA() throws SQLException{

    initialise();


    }


    public void initialise()throws SQLException{
    try {
    Class.forName("com.mysql.jdbc.Driver");
    conn = DriverManager.getConnection (url, userName, password);

    /* Statement stmt = conn.createStatement();
    resultSetReturned = stmt.execute("SELECT * FROM user;") ;*/

    }
    catch (ClassNotFoundException ex) {
    Logger.getLogger(UserDA.class.getName()).log(Level .SEVERE, null, ex);
    }
    }
    public boolean IsLogged(String username, String password) throws SQLException{

    int rowCount = 0;
    Statement stmt = conn.createStatement();
    String uname = "ashanthi";
    ResultSet res = stmt.executeQuery("SELECT * FROM user where first_name="+uname+";") ;
    //where username="+username+"
    while(res.next()){
    rowCount++;
    }



    return rowCount > 0;
    }

    }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by dilanka007 View Post
    this is a code i am working for a coursework...
    the problem is in the isLogged method. the line starting from ResultSet res...
    when i put just ResultSet res = stmt.executeQuery("SELECT * FROM user ";); all works fine.. as i have records in this table..

    But when i do as i ve done there.. i think the result set isn't generating properly..
    have i done anything wrong while writing the query..
    i have 2 records containing the firs_name as ashanthi in my database..i am sure i have done something wrong while writing the query..

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package DAO;
    
    import java.sql.Statement;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    /**
     *
     * @author dilanka
     */
    public class UserDA {
    
    
    
        Connection conn = null;
        String userName ="root";
        String password = "dilan";
        String url = "jdbc:mysql://localhost:3306/bidbizz";
        public boolean resultSetReturned;
        
           public UserDA() throws SQLException{
    
           initialise();
    
    
            }
           
           
           public void initialise()throws SQLException{
           try {
                Class.forName("com.mysql.jdbc.Driver");
                conn = DriverManager.getConnection (url, userName, password);
              
               /* Statement stmt = conn.createStatement();
                resultSetReturned = stmt.execute("SELECT * FROM user;") ;*/
    
           }
            catch (ClassNotFoundException ex) {
                Logger.getLogger(UserDA.class.getName()).log(Level.SEVERE, null, ex);
           }
        }
           public boolean IsLogged(String username, String password) throws SQLException{
    
               int rowCount = 0;
               Statement stmt = conn.createStatement();
               String uname = "ashanthi";
               ResultSet res = stmt.executeQuery("SELECT * FROM user where first_name="+uname+";") ;
               //where username="+username+" 
               while(res.next()){
                   rowCount++;
               }
    
    
    
               return rowCount > 0;
           }
    }
    Better use a PreparedStatement because a string value needs to be (single) quoted. You can either do that yourself (using a simple Statement) or have the PreparedStatement do the nitty gritty details.

    kind regards,

    Jos

  3. #3
    dilanka007 is offline Member
    Join Date
    Jan 2010
    Posts
    11
    Rep Power
    0

    Default could you please give an simple example

    Jos,

    Could you please give an simple example of how you write an prepared statement.

    i need the query to list the lines which have a same username as "ash"

    how would you write it using a prepared statement.. i magine the table name is user

    Thanks a lot

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by dilanka007 View Post
    Jos,

    Could you please give an simple example of how you write an prepared statement.

    i need the query to list the lines which have a same username as "ash"

    how would you write it using a prepared statement.. i magine the table name is user

    Thanks a lot
    Read the API documentation for the PreparedStatement interface; the sql query is something like "select * from user where name = ?" the questionmark value can be given with a setString(1, userName) method where userName = "ash".

    kind regards,

    Jos

  5. #5
    dilanka007 is offline Member
    Join Date
    Jan 2010
    Posts
    11
    Rep Power
    0

    Red face Thanks, but one more problem

    Well i figured out on prepared statement...

    but now when i write this code.. this works perfectly fine...
    but when i want to write as
    String query = "Select * from user where username = ? and password = ?";
    with pstmt.setString(2,password);

    it doesn't work for some reason..

    well s there anything i do wrong now?
    cos it works with one parameter not with 2?

    Regards
    Dilanka

    public boolean IsLogged(String username, String password) throws SQLException{

    int rowCount = 0;
    String query = "Select * from user where username = ?";

    PreparedStatement pstmt = conn.prepareStatement(query);
    pstmt.clearParameters() ;
    pstmt.setString(1, username) ;
    //pstmt.setString(2, password) ;

    //String uname = "ashanthi";
    ResultSet res = pstmt.executeQuery() ;
    //where username="+username+"
    while(res.next()){
    rowCount++;
    }



    return rowCount > 0;
    }

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by dilanka007 View Post
    it doesn't work for some reason..
    Is an Exception thrown? If not most likely zero rows are returned as a consequence that the password differs from the password in the database; else please define 'doesn't work'.

    kind regards,

    Jos

Similar Threads

  1. problem with jdbc in jsp
    By nishant.4545 in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 08-29-2009, 01:31 PM
  2. Problem in JDBC
    By Preethi in forum New To Java
    Replies: 11
    Last Post: 01-08-2009, 05:18 PM
  3. Jdbc problem
    By abhiN in forum JDBC
    Replies: 1
    Last Post: 03-21-2008, 05:10 AM
  4. JDBC problem
    By Swamipsn in forum New To Java
    Replies: 1
    Last Post: 08-13-2007, 06:05 PM
  5. JDBC problem
    By peiceonly in forum Threads and Synchronization
    Replies: 2
    Last Post: 08-03-2007, 01:42 PM

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
  •