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

Thread: Problems in connecting to a SQL Server 2008 database "Student"

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

    Default Problems in connecting to a SQL Server 2008 database "Student"

    //STEP 1. Import required packages

    import java.sql.*;


    public class JDBCCreateTable {

    // JDBC driver name and database URL

    static final String JDBC_DRIVER = "com.microsoft.jdbc.sqlserver.SQLServerDriver" ;
    static final String DB_URL = "jdbc:microsoft: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.microsoft.sqlserver.jdbc.SQLSer verDriver");

    //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

    }


    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)


  2. #2
    Junior Member
    Join Date
    Mar 2010
    Location
    Home-Ujjain /Job-Mumbai
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problems in connecting to a SQL Server 2008 database "Student"

    In the URL you are not providing the port number for MySQL Database server i.e 3306 is default, make sure your URL is correct
    Programmer

Similar Threads

  1. Problems Making a "POST" to a Socket
    By haroldjclements in forum Java Networking
    Replies: 0
    Last Post: October 11th, 2011, 05:28 AM
  2. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  3. Replies: 1
    Last Post: March 31st, 2010, 09:42 PM
  4. Replies: 1
    Last Post: January 15th, 2010, 01:32 AM
  5. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 08:35 AM