Results 1 to 13 of 13

Thread: new to JDBC

  1. #1
    hussainzim is offline Member
    Join Date
    May 2008
    Posts
    14
    Rep Power
    0

    Default new to JDBC

    im new to Java and i want conenct a sql server 2005 database to a java application..can i knw da procedure and whr to download sqlserver driver.and how to set the classpath and a simple sample code to connect to a database.thank u in adavance

    Hussain

  2. #2
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Hello,

    what have you done and what errors are your getting.


    Try this code.

    Java Code:
    try{
            Class.forName(driver);
            conn = DriverManager.getConnection(url,uid,pass);
            stmt = conn.createStatement();
            }
            catch(ClassNotFoundException ce)
            {
                System.out.println("ERROR:"+ce.getMessage());
            }
            catch(SQLException se)
            {
                System.out.println("ERROR:"+se.getMessage());
            }   
            }
    sanjeev,संजीव

  3. #3
    hussainzim is offline Member
    Join Date
    May 2008
    Posts
    14
    Rep Power
    0

    Default

    "java.lang.ClassNotFoundException" thats the error im getting..it something to do with da sql driver i guess.

  4. #4
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    9

    Default

    "java.lang.ClassNotFoundException" thats the error im getting..it something to do with da sql driver i guess.
    Yes your right this error is because the SQL JDBC driver cannot be found.

    You can download the driver here:

    Microsoft SQL Server 2005 JDBC Driver

    Do a search on these forums before you post again because you'll find these questions have been asked and answered 100 times before.

    :D
    Did this post help you? Please me! :cool:

  5. #5
    hussainzim is offline Member
    Join Date
    May 2008
    Posts
    14
    Rep Power
    0

    Default

    i download the driver and then set the classpath..stil im getting the same error :(

  6. #6
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    9

    Default

    What JDK are you using? What editor...?

    Use this code. Create a class called connectDS

    Java Code:
    import java.sql.*;
    import com.microsoft.sqlserver.jdbc.*;
    
    public class connectDS {
    
       public static void main(String[] args) {
    
          // Declare the JDBC objects.
          Connection con = null;
    
          try {
             // Establish the connection. 
             SQLServerDataSource ds = new SQLServerDataSource();
             ds.setUser("UserName");
             ds.setPassword("*****");
             ds.setServerName("localhost");
             ds.setPortNumber(1433); 
             ds.setDatabaseName("AdventureWorks");
             con = ds.getConnection();
    
          }
          catch (Exception e) {
             e.printStackTrace();
          }
          finally {
             if (con != null) try { con.close(); } catch(Exception e) {}
             System.exit(1);
          }
       }
    }
    Last edited by DonCash; 05-01-2008 at 12:05 PM.
    Did this post help you? Please me! :cool:

  7. #7
    hussainzim is offline Member
    Join Date
    May 2008
    Posts
    14
    Rep Power
    0

    Default

    im using eclipse..

    btw da code u gave me had error
    "com.microsoft cannot be resolved to a type"

  8. #8
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    9

    Default

    Have you imported the JDBC jar into your Eclipse project?

    Try the new code example above. Sorry the original one I did was from memory. This new one is taken from the SQLServer JDBC help and I cut it down a bit.
    Last edited by DonCash; 05-01-2008 at 12:09 PM.
    Did this post help you? Please me! :cool:

  9. #9
    hussainzim is offline Member
    Join Date
    May 2008
    Posts
    14
    Rep Power
    0

    Default

    i extracted my SqlServer2005 dirver at C:\Program Files\
    and then set the class path.thats wht i did..i didnt import a jdbc jar file.how to import the JDBC jar???

    got an error from the code u gave
    "The import com.microsoft cannot be resolved"

  10. #10
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    9

    Default

    OK this is because you haven't imported the JDBC driver into Eclipse.

    You need to locate the .jar file that is extracted once you download the exe from that Microsoft Download site.

    Go to Eclipse: In the package navigation window on the left hand side right click your current Project.

    Click 'Properties' then on the left hand side click 'Java Build Path' then click 'Add External JARs..' on the right.

    Navigate to the JDBC .jar file and double click it. This will import the jar into your project.

    Click OK.

    Now try to run your code...
    Did this post help you? Please me! :cool:

  11. #11
    hussainzim is offline Member
    Join Date
    May 2008
    Posts
    14
    Rep Power
    0

    Default

    got an error

    "con cannot be resolved"

  12. #12
    hussainzim is offline Member
    Join Date
    May 2008
    Posts
    14
    Rep Power
    0

    Default

    sorry the error was
    con = ds.getConnection();
    "Type mismatch: cannot convert from Connection to Connection"

  13. #13
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    9

    Default

    Hey hussainzim, you do have the ability to edit your previous posts so it would be best to edit rather than post a new one.

    I can't actually test this code because I dont have a SQL database to connect to.

    All I can suggest is looking at the Help that comes with the JDBC and try some of their examples.

    Try:

    Java Code:
    import java.sql.*;
    
    public class connectURL {
    
       public static void main(String[] args) {
    
          // Create a variable for the connection string.
          String connectionUrl = "jdbc:sqlserver://localhost:1433;" +
             "databaseName=AdventureWorks;user=UserName;password=*****";
    
          // Declare the JDBC objects.
          Connection con = null;
          Statement stmt = null;
          ResultSet rs = null;
    
          try {
             // Establish the connection.
             Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
             con = DriverManager.getConnection(connectionUrl);
    
             // Create and execute an SQL statement that returns some data.
             String SQL = "SELECT TOP 10 * FROM Person.Contact";
             stmt = con.createStatement();
             rs = stmt.executeQuery(SQL);
    
             // Iterate through the data in the result set and display it.
             while (rs.next()) {
                System.out.println(rs.getString(4) + " " + rs.getString(6));
             }
          }
    
          // Handle any errors that may have occurred.
          catch (Exception e) {
             e.printStackTrace();
          }
          finally {
             if (rs != null) try { rs.close(); } catch(Exception e) {}
             if (stmt != null) try { stmt.close(); } catch(Exception e) {}
             if (con != null) try { con.close(); } catch(Exception e) {}
          }
       }
    }
    Did this post help you? Please me! :cool:

Similar Threads

  1. Replies: 0
    Last Post: 04-01-2008, 11:17 AM
  2. Jdbc
    By siwa in forum JDBC
    Replies: 4
    Last Post: 11-09-2007, 08:31 AM
  3. Replies: 0
    Last Post: 09-28-2007, 01:56 PM
  4. Ha-jdbc 2.0
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 07-17-2007, 06:40 PM
  5. Help with JDBC
    By Eric in forum JDBC
    Replies: 2
    Last Post: 06-28-2007, 07:00 PM

Posting Permissions

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