Results 1 to 7 of 7
  1. #1
    Join Date
    Jan 2010
    Posts
    80
    Rep Power
    0

    Default Connect Java to SQL

    Hi

    I'm working these days on how to connect java applet with SQL database.

    I have searched many times and finally found this code

    Java Code:
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    public class Main {
      public static void main(String[] args) throws Exception {
        if (conn != null) {
          System.out.println("Connection attempt was successful!");
    
          try {
            conn.close();
          } catch (SQLException ex) {
            ex.printStackTrace();
            System.out.println("Problem closing the connection");
          }
        }
      }
    
      static Connection conn;
    
      static Statement st;
    
      static {
        try {
          // Step 1: Load the JDBC driver.
          Class.forName("org.hsqldb.jdbcDriver");
          System.out.println("Driver Loaded.");
          // Step 2: Establish the connection to the database.
          String url = "jdbc:hsqldb:data/tutorial";
    
          conn = DriverManager.getConnection(url, "sa", "");
          System.out.println("Got Connection.");
    
          st = conn.createStatement();
        } catch (Exception e) {
          System.err.println("Got an exception! ");
          e.printStackTrace();
          System.exit(0);
        }
      }
    }
    After I read about the connection , I could not understand this line

    Java Code:
    // Step 1: Load the JDBC driver.
          Class.forName("org.hsqldb.jdbcDriver");
    Also this line

    Java Code:
     // Step 2: Establish the connection to the database.
          String url = "jdbc:hsqldb:data/tutorial";
    Can someone explain that to me ?

    I'll be thankful to you guys.

    :)

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    They are explained in the Tutorial. Go through that from front to back.

  3. #3
    baloda is offline Member
    Join Date
    Nov 2010
    Location
    New Delhi
    Posts
    50
    Rep Power
    0

    Default

    Class.forName("org.hsqldb.jdbcDriver");

    loads the database drivers that will provide the necessary things i.e. connectivity to a specific data base u r using for ur application.

    String url = "jdbc:hsqldb:data/tutorial";

    this is the url for the data base server.

    if ur database is on the same machine then u can change it
    (MS-Access database)

    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    con = DriverManager.getConnection("jdbc:odbc:FMSDB",null ,null);
    //FMSDB is database name and first null is username and second is password

    for more details u can visit

    http://java-sql.blogspot.com/2011/03...ns-injava.html
    Last edited by baloda; 03-28-2011 at 12:24 PM.

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by baloda View Post
    if ur database is on the same machine then u can change it
    (MS-Access database)

    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    con = DriverManager.getConnection("jdbc:odbc:FMSDB",null ,null);
    //FMSDB is database name and first null is username and second is password

    for more details u can visit

    SQL where clause with Date in Java
    The question there is, why would you want to?

  5. #5
    baloda is offline Member
    Join Date
    Nov 2010
    Location
    New Delhi
    Posts
    50
    Rep Power
    0

    Default

    Quote Originally Posted by masijade View Post
    The question there is, why would you want to?
    i think that will be easy to understand for a starter.

    http://java-sql.blogspot.com/2011/03...ns-injava.html
    Last edited by baloda; 03-28-2011 at 12:25 PM.

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by baloda View Post
    i think that will be easy to understand for a starter.
    Not likely.

  7. #7
    baloda is offline Member
    Join Date
    Nov 2010
    Location
    New Delhi
    Posts
    50
    Rep Power
    0

    Default

    Quote Originally Posted by masijade View Post
    Not likely.
    May be, u r right. but at that time i thought like that.


    http://java-sql.blogspot.com/2011/03...ns-injava.html
    Last edited by baloda; 03-28-2011 at 12:26 PM.

Similar Threads

  1. connect to oracle from java
    By ahmadian in forum Advanced Java
    Replies: 7
    Last Post: 05-15-2013, 05:15 PM
  2. how to connect java to mysql?
    By ranvir in forum JDBC
    Replies: 2
    Last Post: 12-11-2010, 09:34 AM
  3. how to connect 2 java files
    By McArthurG in forum New To Java
    Replies: 3
    Last Post: 07-07-2010, 03:34 PM
  4. connect to web server from mobile using java app.
    By jankrishnan in forum Advanced Java
    Replies: 1
    Last Post: 07-15-2008, 05:12 PM
  5. Making connect 4 in java
    By zoe in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 07:10 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
  •