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 4 of 4

Thread: Connecting to a database:

  1. #1
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Connecting to a database:

    I am connecting to my database FINALLY however now I am getting the following error:

    my output:
    Database Established…
    Error Catch:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM 'users' WHERE user_id='7'' at line 1

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;
     
     
    public class DBDemo 
    {
    	public static void main(String [] args){            
            try{
     
             Connection conn=null;//8889
             String url ="jdbc:mysql://localhost:3306/ch18";
             String user="root";
             String pass="root";
             conn = DriverManager.getConnection(url,user,pass);
             System.out.println("Database Established…");
     
                PreparedStatement ps = null;
                ps=conn.prepareStatement("SELECT FROM 'users' WHERE user_id='7'");
     
                ps.execute();
     
                System.out.println(ps);
     
                    conn.close();
     
            }catch(SQLException e){
                System.out.println("Error Catch:"+e);
            }
     
        }
    }
    DataBase.jpg

    Here is a picture of my MAMP mySQL database. This is my first time doing this and could use all the advice I can get.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 832 Times in 772 Posts
    Blog Entries
    5

    Default Re: Connecting to a database:

    Thread moved to the appropriate section

    The exception indicates an error in your syntax. Can you run that query directly in the database? I presume not, because usually a query needs to select something (for instance a single column, or wildcard ( * ) for everything)

  3. #3
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: Connecting to a database:

    Ok I fixed that now it just repeats the query back to me. I want it to execute it:

    Database Established…
    com.mysql.jdbc.JDBC4PreparedStatement@24599bcd: SELECT * FROM  `users` WHERE  `first_name` =  'Drew'

    public static void main(String [] args){            
            try{
     
             Connection conn=null;//8889
             String url ="jdbc:mysql://localhost:3306/ch18";
             String user="root";
             String pass="root";
             conn = DriverManager.getConnection(url,user,pass);
             System.out.println("Database Established…");
     
                PreparedStatement ps = null;
                ps=conn.prepareStatement("SELECT * FROM  `users` WHERE  `first_name` =  'Drew'");
     
                ps.execute();
     
                System.out.println(ps);
     
                    conn.close();
     
            }catch(SQLException e){
                System.out.println("Error Catch:"+e);
            }
    Last edited by jocdrew21; September 10th, 2014 at 01:25 PM.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 832 Times in 772 Posts
    Blog Entries
    5

    Default Re: Connecting to a database:

    You must retrieve a ResultSet to get any results from a query. See the API for the class and methods you are using for full descriptions
    PreparedStatement (Java Platform SE 7 )\
    As an aside, I would also recommend closing any open resources (eg: PreparedStatement, ResultSet, Connection), possibly best done in the finally clause of the try/catch

Similar Threads

  1. Connecting netbeans with sql database
    By babe20042004 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 15th, 2013, 03:57 PM
  2. Problem with connecting to DataBase
    By ocean1991 in forum JDBC & Databases
    Replies: 1
    Last Post: January 11th, 2013, 12:19 PM
  3. Error connecting to database
    By bczm8703 in forum JDBC & Databases
    Replies: 9
    Last Post: December 8th, 2011, 01:06 AM
  4. Looking at Exploring Database Connecting
    By aussiemcgr in forum JDBC & Databases
    Replies: 2
    Last Post: August 12th, 2010, 03:40 PM
  5. Connecting to a database
    By fwashington in forum JDBC & Databases
    Replies: 5
    Last Post: March 15th, 2010, 01:37 PM