Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By SurfMan

Thread: faild to connect the SQLDatabase

  1. #1
    hosseini8546 is offline Member
    Join Date
    May 2017
    Posts
    2
    Rep Power
    0

    Unhappy faild to connect the SQLDatabase

    hello friends!
    i have problem in my code to connect the database!
    my code is below:

    Java Code:
    import java.sql.Connection;
    import java.sql.Statement;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.ResultSetMetaData;
    import java.sql.SQLException;
    public class displayauyhors {
        public static void main(String[] args){
            final String database_url="jdbc:derby://localhost:1527/book";
            final String selectquerry="SELECT * FROM t1";
            try(
                    Connection con=DriverManager.getConnection(database_url,"sa","12345");
                    Statement statement=con.createStatement();
                    ResultSet resultset=statement.executeQuery(selectquerry))
            {
                ResultSetMetaData metadata=resultset.getMetaData();
                int numberofcolumns=metadata.getColumnCount();
                System.out.printf("auther table is:%n%n");
                for(int i=1;i<=numberofcolumns;i++){
                    System.out.printf("%-8s|t", metadata.getCatalogName(i));
                    
                }
                while(resultset.next()){
                    for(int i=1;i<=numberofcolumns;i++){
                        System.out.printf("%-8s\t", resultset.getObject(i));
                        System.out.println();
                    }
                }
                
            }
            catch(SQLException sqlException ){
            sqlException.printStackTrace();
            }
                    
        }
        
        
    }

    WHY can not connect my database!!! PLEASE PLEASE HELP ME !! PLEASE
    Last edited by hosseini8546; 05-01-2017 at 03:21 PM.

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,939
    Rep Power
    7

    Default Re: faild to connect the SQLDatabase

    The stacktrace will tell you all about it. Copy/paste that in here too. (Or actually read it and feel enlightened)
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  3. #3
    hosseini8546 is offline Member
    Join Date
    May 2017
    Posts
    2
    Rep Power
    0

    Default Re: faild to connect the SQLDatabase

    wow!! dear what do you mean !! would you please guide me clearly!!!!!
    i change it and i put it in code sample!!!
    Last edited by hosseini8546; 05-01-2017 at 03:23 PM.

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,939
    Rep Power
    7

    Default Re: faild to connect the SQLDatabase

    When something goes wrong in a Java program, an Exception is created containing details about where it went wrong, and usually, a message describing what went wrong. Your case is no different. Run your program and watch the output (console/stdout). If there is an SQLException, it should be printed on the output. You can read that exceptionj for yourself and see what goes wrong. You can also post it here, and let us do all the hard work. Just know that when you are programming Java, you will be reading a whole lot of stacktraces in your life, so it might be a Good Thing™ to learn how to read a stacktrace.
    jepoys1 likes this.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  5. #5
    caraldur is offline Member
    Join Date
    Feb 2015
    Posts
    27
    Rep Power
    0

    Default Re: faild to connect the SQLDatabase

    Hi hosseini8546,

    A few things jump out at me right away, but like Surfman said you should take a look at the stacktrace output to get an idea where the error is occuring.

    First: I noticed was on line 11 you have
    Java Code:
     try(
    when you should have
    Java Code:
     try{
    Second: thing is line 14 should end in a ;
    Third: You have too many braces remove line 15

    Once you do that run your program and check the output window for the stack trace error.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,532
    Rep Power
    24

    Default Re: faild to connect the SQLDatabase

    That try() is correct. If it wasn't it wouldn't have compiled, so couldn't have run.
    It's a "try with resources".
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    caraldur is offline Member
    Join Date
    Feb 2015
    Posts
    27
    Rep Power
    0

    Default Re: faild to connect the SQLDatabase

    Quote Originally Posted by Tolls View Post
    That try() is correct. If it wasn't it wouldn't have compiled, so couldn't have run.
    It's a "try with resources".
    Ahh, didn't know you could do that.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,532
    Rep Power
    24

    Default Re: faild to connect the SQLDatabase

    What'll happen is that those three things defined in the try brackets (conection, statement and resultset) will be auto-closed as if in a finally block.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    caraldur is offline Member
    Join Date
    Feb 2015
    Posts
    27
    Rep Power
    0

    Default Re: faild to connect the SQLDatabase

    Quote Originally Posted by Tolls View Post
    What'll happen is that those three things defined in the try brackets (conection, statement and resultset) will be auto-closed as if in a finally block.
    That's good to know, I'll have to give it a try. Thanks for the info.

  10. #10
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,939
    Rep Power
    7

    Default Re: faild to connect the SQLDatabase

    Anything that implements AutoClosable is eligable for use in a try-with-resources block.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  11. #11
    jepoys1 is offline Member
    Join Date
    Apr 2017
    Posts
    5
    Rep Power
    0

    Default Re: faild to connect the SQLDatabase

    I have a question, a general-question in programming with databases. Whenever you create sql statements or CRUD methods, do you always have to connect to the database or can I separate a method/interface whereas I connect to the database and create a separate method for CRUD?
    Last edited by jepoys1; 05-16-2017 at 05:03 PM.

Similar Threads

  1. Need Help With Connect 4!!!
    By AJ07 in forum New To Java
    Replies: 1
    Last Post: 12-17-2013, 10:21 AM
  2. Connect 4 AI help!
    By jenxin in forum New To Java
    Replies: 2
    Last Post: 10-01-2012, 02:35 AM
  3. faild to load the jni shared library
    By sadeem in forum Eclipse
    Replies: 0
    Last Post: 05-22-2011, 11:52 PM
  4. how to connect with my GUI
    By doha786 in forum New To Java
    Replies: 2
    Last Post: 04-01-2009, 05:59 PM
  5. how connect
    By herfnai in forum JDBC
    Replies: 1
    Last Post: 08-18-2008, 07:34 AM

Posting Permissions

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