2

Hey friends I am totally new in JAVA and i want to now how to connect JDBC with SQL Server 2012. I go through so much material but i didn't get it. So can you give me a sample demo code...

I have one more problem that i installed SQL Server 2012 but i don't know what is username, password and server name. SO what can i do for it??

When i code it gives error...

Code:

import java.sql.*;
public class Conection
{
    public static void main(String a[]) throws ClassNotFoundException, SQLException
    {
        try
        {
            String url = "jdbc:sqlserver://localhost:1433//SQLEXPRESS;databaseName=mydb";   
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Connection conn = DriverManager.getConnection(url);
            System.out.println("connection created");
            Statement st=conn.createStatement();
            String sql="select * from mydb";
            ResultSet rs=st.executeQuery(sql);
            while(rs.next())
            {
                System.out.println("Name: "+rs.getString(1));
                //System.out.println("Address : "+rs.getString(2));
            }
            if(st!=null)
            st.close();
            if(conn!=null)
                conn.close();
        }
        catch(SQLException sqle)
        {
            System.out.println("Sql exception "+sqle);
        }
    }
}

Error

Exception in thread "main" java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    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 Conection.main(Conection.java:11)
3
  • First add the sql server driver jar in your class path. Commented Mar 7, 2014 at 6:08
  • how can i add sql server driver in class path? Commented Mar 7, 2014 at 6:15
  • Which IDE are you using? Commented Mar 7, 2014 at 6:16

1 Answer 1

4

It seems your sql driver classes is not in classpath. if your are using an IDE add it to classpath else add it manually before compiling your class.

Sign up to request clarification or add additional context in comments.

3 Comments

Which IDE r u using? if you are using eclipse right click on the properties click add build path. In libraries click external libraries and locate your jar. If your are not using IDE please check the below link. stackoverflow.com/questions/17549100/java-classpath-in-unix
I am using Eclipse but i am not able to found jar file.
Download it from the below link and add it your classpath. microsoft.com/en-us/download/details.aspx?id=2505

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.