How do you connect a Java program to a database?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shalinisherlin
    New Member
    • Jan 2008
    • 1

    #1

    How do you connect a Java program to a database?

    how to connect java with database?
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Google for 'jdbc'.

    kind regards,

    Jos

    Comment

    • BigDaddyLH
      Recognized Expert Top Contributor
      • Dec 2007
      • 1216

      #3
      Please remember to provide a meaningful Title for any threads started (see the FAQ entry Use a Good Thread Title).

      This helps to ensure that other members, and also the general public, will have a better chance of finding answers to any similar questions.

      MODERATOR

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #4
        Here is Sun's JDBC tutorial:

        This JDBC Java tutorial describes how to use JDBC API to create, insert into, update, and query tables. You will also learn how to use simple and prepared statements, stored procedures and perform transactions

        Comment

        • 2008serela
          New Member
          • Jan 2008
          • 4

          #5
          Originally posted by BigDaddyLH
          Hi,

          First decide which database u want to connect it?

          Only the three steps:

          1.If it is in the Oracle then..

          String query="Select * from emp_table";
          class.forName(" sun.jdbc.odbc.J dbcOdbcDriver") ;
          Connection con=DriverManag er.getConnectio n("jdbc:odbc:<d atasourcename>" );
          PreparedStateme nt ps=con.prepareS tatement(query) ;
          ResultSet rs=ps.execute() ;

          2.If it is for MsAcess then

          the above steps are same only the driver and class.forName changes ..
          now i forget it later i will send it.

          Pls try the above coding and reply is must.If it is wrong Pls send the correct answer...

          bye takecare!

          by
          ela

          Comment

          • shaileshkumar
            New Member
            • Aug 2007
            • 36

            #6
            import java.sql package
            Load the driver
            Class.forName(" sun.jdbc.odbc.J dbcOdbcDriver") ;

            the above method throws ClassNotFoundEx ception

            establish the connection
            Connection con=DriverManag er.getConnectio n("URL","userid ","pwd")
            the method in the above will throw SQLException

            PreparedStateme nt ps=con.prepareS tatement("sql query");

            the method above throws SQLException

            AND

            after doing with the database operations

            close the connections
            con.close();
            this throws SQLException

            Comment

            • 2008serela
              New Member
              • Jan 2008
              • 4

              #7
              Originally posted by shaileshkumar
              import java.sql package
              Load the driver
              Class.forName(" sun.jdbc.odbc.J dbcOdbcDriver") ;

              the above method throws ClassNotFoundEx ception

              establish the connection
              Connection con=DriverManag er.getConnectio n("URL","userid ","pwd")
              the method in the above will throw SQLException

              PreparedStateme nt ps=con.prepareS tatement("sql query");

              the method above throws SQLException

              AND

              after doing with the database operations

              close the connections
              con.close();
              this throws SQLException

              Hi,

              Pls do the above code within the try block
              try{

              <code>
              }
              catch(SQLExcept ion exception){
              }


              try this.......

              Comment

              • ajos
                Contributor
                • Aug 2007
                • 283

                #8
                Originally posted by 2008serela
                Hi,

                First decide which database u want to connect it?

                Only the three steps:

                1.If it is in the Oracle then..

                String query="Select * from emp_table";
                class.forName(" sun.jdbc.odbc.J dbcOdbcDriver") ;
                Connection con=DriverManag er.getConnectio n("jdbc:odbc:<d atasourcename>" );
                PreparedStateme nt ps=con.prepareS tatement(query) ;
                ResultSet rs=ps.execute() ;

                2.If it is for MsAcess then

                the above steps are same only the driver and class.forName changes ..
                now i forget it later i will send it.

                Pls try the above coding and reply is must.If it is wrong Pls send the correct answer...

                bye takecare!

                by
                ela
                Yuo should add the jar file in the class path(in the lib folder of your application). The error you are getting states that.

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  As a side note: I see everone going Class.forName(" your.driver") but doesn't anyone
                  use the more versatile DataSource method for obtaining (pooled) connections?
                  It's been here since Java 1.4. Read all about it in its API documentation ...

                  kind regards,

                  Jos

                  Comment

                  Working...