Java program to connect to Sql server and run sample request from Eclipse

package sqlselection; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class Sqlselection { public static void main(String[] args) { try { Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); String userName = "sa"; String password = "password"; String url = "jdbc:microsoft:sqlserver://localhost:1433"+";databaseName=AdventureWorks2008R2"; Connection con = DriverManager.getConnection(url, userName, password); Statement s1 = con.createStatement(); ResultSet rs = s1.executeQuery("SELECT TOP 1 * FROM HumanResources.Employee"); String[] result = new String[20]; if(rs!=null){ while (rs.next()){ for(int i = 0; i <result.length ;i++) { for(int j = 0; j <result.length;j++) { result[j]=rs.getString(i); System.out.println(result[j]); } } } } //String result = new result[20]; } catch (Exception e) { e.printStackTrace(); } } } enter code here 

The Above is my sample program for connecting to an Sql server to run a query to select a sample from eclipse.

I get the following error.

 java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.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 sqlselection.Sqlselection.main(Sqlselection.java:13) 

I added sqljdbc.jar to the library, sqljdbc4.jar. Help fix this.

+8
java eclipse jdbc jdbc-odbc
source share
8 answers

The problem is Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); this line. Invalid class name

This is sqlserver.jdbc not jdbc.sqlserver

+6
source share

you forgot to add sqlserver.jar to your eclipse external library follow the process of adding jar files

  • Right click on your project.
  • click buildpath
  • click on bullet route setup
  • click add external jar and then specify the path to jar
+5
source share

Refer to the link below.

There are two important changes you must make:

driver name as "com.microsoft.sqlserver.jdbc.SQLServerDriver"

& in the URL "jdbc: sqlserver: // localhost: 1433" + "; databaseName = AdventureWorks2008R2"

http://www.programcreek.com/2010/05/java-code-for-connecting-ms-sql-server-by-using-sql-server-authentication/

+3
source share

Add sqlserver.jar Here is the link

As the name suggests, a ClassNotFoundException in Java is a subclass of java.lang.Exception and occurs when the Java Virtual Machine tries to load a specific class, rather than finding the requested class in the classpath.

Another important point in this exception is that it is a noted exception , and you need to explicitly provide exception handling when using methods that can throw a ClassNotFoundException in java, either using a try-catch block or using the throws clause.

Oracle Docs

 public class ClassNotFoundException extends ReflectiveOperationException 

Thrown when an application tries to load into a class via its string name using:

  • The forName method in the Class class.
  • The findSystemClass method in the ClassLoader class.
  • The loadClass method in the ClassLoader class.

but a class definition with the specified name was not found.

+1
source share
+1
source share

Right-click your project ---> Build Path ----> configure Path Bar ----> Libraries Tab ---> Add External Banks ---> (Go to the location where you saved sql jar) ---> ok

+1
source share

download Microsoft JDBC Driver 4.0 for SQL Server, which supports:

  SQL Server versions: 2005, 2008, 2008 R2, and 2012. JDK version: 5.0 and 6.0. 

Run the downloaded sqljdbc __ program. exe. It will extract files to the specified directory (the default is Microsoft JDBC Driver 4.0 for SQL Server). You will find two jar files sqljdbc.jar (for JDBC 3.0) and sqljdbc4.jar (for JDBC 4.0), as well as some .dll files and HTML help files.

Place the sqljdbc4.jar file under your application class path if you are using JDK 4.0 or the sqljdbc4.1.jar file if you are using JDK 6.0 or later.

+1
source share

Just modify the query as follows:

 SELECT TOP 1 * FROM [HumanResources].[Employee] 

where Employee is the name of your table, and HumanResources is your schema name, if I'm not mistaken.

Hope your problem is resolved. :)

-2
source share

All Articles