Results 1 to 4 of 4
Thread: MS SQL server+ JDBC Connection
- 07-09-2012, 10:55 AM #1
Member
- Join Date
- Jul 2012
- Posts
- 5
- Rep Power
- 0
MS SQL server+ JDBC Connection
hi,
I am new to java i need to connect MS SQL server+ JDBC Connection
I am getting the below error when i try to connect the code in Eclipse
import java.sql.*;
public class SQLServerTest {
public static void main(String[] args) {
// Create a variable for the connection string.
String connectionUrl="jdbc:sqlserver://localhost:1433;DatabaseName=Master";
// Declare the JDBC objects.
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
// Establish the connection.
Class.forName("com.microsoft.sqlserver.jdbc.SQLSer verDriver");
con = DriverManager.getConnection(connectionUrl);
// Create and execute an SQL statement that returns some data.
String SQL = "Select * from sys.sysobjects where xtype='u'";
stmt = con.createStatement();
rs = stmt.executeQuery(SQL);
// Iterate through the data in the result set and display it.
while (rs.next()) {
System.out.println(rs.getString(4) + " " + rs.getString(6));
}
}
// Handle any errors that may have occurred.
catch (Exception e) {
e.printStackTrace();
}
finally {
if (rs != null) try { rs.close(); } catch(Exception e) {}
if (stmt != null) try { stmt.close(); } catch(Exception e) {}
if (con != null) try { con.close(); } catch(Exception e) {}
}
}
}
ERROR:
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 SQLServerTest.main(SQLServerTest.java:20)
What would be the reason.
Thanks
- 07-09-2012, 11:32 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: MS SQL server+ JDBC Connection
The jar file containing the driver needs to be on the runtime classpath.
How that is setup in your IDE will probably be in the documentation for Eclipse.Please do not ask for code as refusal often offends.
- 07-09-2012, 12:36 PM #3
Member
- Join Date
- Jul 2012
- Posts
- 5
- Rep Power
- 0
Re: MS SQL server+ JDBC Connection
- 07-09-2012, 01:31 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
JDBC connection
By Dineshmtech in forum JDBCReplies: 2Last Post: 01-07-2012, 08:20 AM -
JDBC Connection
By Riyaz.hk77 in forum New To JavaReplies: 2Last Post: 11-06-2010, 05:53 PM -
Issue with JDBC Connection to SQL Server 2005 database when default schema is changed
By NicoleVT2000 in forum New To JavaReplies: 0Last Post: 12-01-2009, 10:01 PM -
JDBC Connection...
By onlysumitg in forum JDBCReplies: 3Last Post: 08-21-2009, 11:34 AM -
SQL Server 2005 Jdbc connection
By mgt83 in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 07-27-2007, 04:52 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks