How to connect JAVA to SQL Server 2012?

13,556

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.

Share:
13,556
Shaktisinh Jadeja
Author by

Shaktisinh Jadeja

Updated on June 04, 2022

Comments

  • Shaktisinh Jadeja
    Shaktisinh Jadeja almost 2 years

    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)
    
  • Shriram
    Shriram about 10 years
    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
  • Shaktisinh Jadeja
    Shaktisinh Jadeja about 10 years
    I am using Eclipse but i am not able to found jar file.
  • Shriram
    Shriram about 10 years
    Download it from the below link and add it your classpath. microsoft.com/en-us/download/details.aspx?id=2505