Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Where

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Where

    Following code works until I try to add WHERE to the Query. I have no errors before run but get the errors below. I need the proper syntax or method to specify the row. HELP!!!

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
     
     
    public class TryThree {
     
    public static void main(String args[]) throws SQLException{
     
    Connection con = DriverManager.getConnection ("jdbc:mysql://67.20.123.248:3306/whitebu4_sliditerod" ,"whitebu4_admins" , "***");
    String dbClass = "com.mysql.jdbc.Driver";
    String Nome = null;
    String query = ("Select x,y,z FROM xyz WHERE pname='" + Nome + "'");
     
    Object prepareStatement = null;
     
    ((PreparedStatement) prepareStatement).setString(1, Nome);
     
    try {
     
    Class.forName("com.mysql.jdbc.Driver");
     
    Statement st = con.createStatement();
    ResultSet rs = st.executeQuery(query);
    while(rs.next()){
    float id  = rs.getFloat("x");
    float id1  = rs.getFloat("y");
    float id2  = rs.getFloat("z");
     
    System.out.print("ID: " + id);
    System.out.print("id1 " + id1);
    System.out.print("id2 " + id2);
     
    } //end while
     
    con.close();
    } //end try
     
    catch(ClassNotFoundException e) {
    e.printStackTrace();
    }
     
    catch(SQLException e) {
    e.printStackTrace();
    }
     
    }  //end main
     
    }  //end class
    Exception in thread "main" java.lang.NullPointerException
    at TryThree.main(TryThree.java:21)

  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Where

    It's because your String "Nome" is null.

    You need to give it a proper value.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Where

    That didnt work Ive tried it developed into more errors Im now trying this which makes more sense but still produces errors. Why cant I specify a row without hassle?

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
     
     
    public class TryThree {
     
    public static void main(String args[]) throws SQLException{
     
    Connection con = DriverManager.getConnection ("jdbc:mysql://67.20.123.248:3306/whitebu4_sliditerod" ,"whitebu4_admins" , "admins1234");
    String dbClass = "com.mysql.jdbc.Driver";
    Statement statement = con.createStatement();
    String query = "Select x,y,z FROM xyz";
    ResultSet result = statement.executeQuery(query);
     
    result.getFloat    ("Nome");
    result.getFloat   ("Nome");
    result.getFloat    ("Nome");
    result.getString    ("Nome");
     
     
     
    try {
     
    Class.forName("com.mysql.jdbc.Driver");
     
    Statement st = con.createStatement();
     
    while(result.next()){
     
     
    System.out.print(result);
    System.out.print(result);
    System.out.print(result);
     
    } //end while
     
    con.close();
    } //end try
     
    catch(ClassNotFoundException e) {
    e.printStackTrace();
    }
     
    catch(SQLException e) {
    e.printStackTrace();
    }
     
    }  //end main
     
    }  //end class

  4. #4
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Where

    You've converted your small problem into a heap of problems I imagine.
    Please be more direct with your questions and provide more information of what is going wrong.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  5. #5
    Junior Member
    Join Date
    Sep 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Where

    How can I specify a Row to select data from using a string for the choice??