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

Thread: I am trying to connect my java application with SQL Server 2008 but I get error msgs

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I am trying to connect my java application with SQL Server 2008 but I get error msgs

    //STEP 1. Import required packages

    import java.sql.*;


    public class JDBCCreateTable {

    // JDBC driver name and database URL
    // Database is called Student

    static final String JDBC_DRIVER = "com.SQLEXPRESS.jdbc.Driver";
    static final String DB_URL = "jdbc:SQLSERVER://SQLEXPRESS/Student";


    // Database credentials

    static final String USER = "sa";
    static final String PASS = "sasa";

    public static void main(String[] args) {
    Connection conn = null;
    Statement stmt = null;

    try{

    //STEP 2: Register JDBC driver

    Class.forName("com.SQLEXPRESS.jdbc.Driver");

    //STEP 3: Open a connection

    System.out.println("Connecting to database...");
    conn = DriverManager.getConnection(DB_URL,USER,PASS);


    //STEP 4: Execute a query

    System.out.println("Creating statement...");
    stmt = conn.createStatement();
    String sql;
    sql = "SELECT StudentID, StudentName, StudentAddress, PhoneNumber FROM StudentName";
    ResultSet rs = stmt.executeQuery(sql);

    //STEP 5: Extract data from result set

    while(rs.next()){

    //Retrieve by column name

    int id = rs.getInt("StudentID");
    int age = rs.getInt("StudentName");
    String first = rs.getString("StudentAddress");
    String last = rs.getString("PhoneNumber");

    //Display values

    System.out.print("StudentID: " + id);
    System.out.print("StudentName: " + age);
    System.out.print("StudentAddress: " + first);
    System.out.println("PhoneNumber: " + last);
    }


    //STEP 6: Clean-up environment

    rs.close();
    stmt.close();
    conn.close();
    }catch(SQLException se){

    //Handle errors for JDBC

    se.printStackTrace();
    }catch(Exception e){

    //Handle errors for Class.forName

    e.printStackTrace();
    }finally{

    //finally block used to close resources

    try{
    if(stmt!=null)
    stmt.close();

    }catch(SQLException se2){
    }

    // nothing we can do

    try{
    if(conn!=null)
    conn.close();
    }catch(SQLException se){
    se.printStackTrace();
    }
    //end finally try
    }

    //end try

    System.out.println("Goodbye!");
    }
    //end main

    }

    //end FirstExample
    Last edited by nadirkhan; November 19th, 2011 at 03:00 PM.


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

    Default Re: I am trying to connect my java application with SQL Server 2008 but I get error m

    Error message? Where? What?

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I am trying to connect my java application with SQL Server 2008 but I get error m

    Here are the error messages that I get when I compile this code.

    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at JDBCCreateTable.main(JDBCCreateTable.java:27)

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

    Default Re: I am trying to connect my java application with SQL Server 2008 but I get error m

    That is not the full exception...that being said I presume it is a ClassNotFound exception, which means I recommend reading the following:
    http://www.javaprogrammingforums.com...html#post20626

  5. #5
    Member
    Join Date
    Feb 2011
    Posts
    33
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: I am trying to connect my java application with SQL Server 2008 but I get error m

    There should be a jar that contains the Driver class needed to make the connection to the DB. That should fix the ClassNotFoundException.


    All intelligent thoughts have already been thought;
    what is necessary is only to try to think them again.



  6. #6
    Junior Member
    Join Date
    Jul 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I am trying to connect my java application with SQL Server 2008 but I get error m

    Now I get the following exception though I have downloaded the jar file from the microsoft site and saved it in the build path for eclipse.

    java.sql.SQLException: No suitable driver found for jdbc:microsoft:SQLSERVER://SQLEXPRESS:Student
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at JDBCCreateTable.main(JDBCCreateTable.java:32)

  7. #7

    Default Re: I am trying to connect my java application with SQL Server 2008 but I get error msgs

    The database URL is invalid so JDBC couldn't find an appropriate driver:

    jdbc:microsoft:SQLSERVER://SQLEXPRESS:Student

    It should be:

    jdbc:sqlserver://localhost\sqlexpress;databaseName=Student
    Last edited by copeg; December 28th, 2012 at 10:48 AM. Reason: link removed

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

    Default Re: I am trying to connect my java application with SQL Server 2008 but I get error msgs

    Quote Originally Posted by nambill View Post
    The database URL is invalid so JDBC couldn't find an appropriate driver:
    Please don't resurrect posts over a year old, especially with the intent of providing links back to (what I presume is) your webpage for self promotion

Similar Threads

  1. Java Code to connect remote server
    By idealguy in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: August 3rd, 2012, 04:59 AM
  2. MS SQL server 2005 to 2008 R2 migration
    By sandeep23k in forum JDBC & Databases
    Replies: 3
    Last Post: April 9th, 2011, 10:53 AM
  3. Replies: 6
    Last Post: August 18th, 2010, 05:41 PM
  4. Java Application Server
    By goutamdaphtari in forum Java Theory & Questions
    Replies: 0
    Last Post: July 14th, 2010, 11:53 PM
  5. java application connecting to a server
    By wildheart25c in forum Java Networking
    Replies: 2
    Last Post: September 17th, 2009, 07:22 AM