File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SQL Server 2000 Database Connection using JDBC

 
Rajesh Rami Reddy
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Friends,

I had wrote a programme for connecting the Sql Server 2000 DataBase using JDBC.Programe code is

import java.sql.*;

public class createTable
{
public static void main (String[] args)
{
DB db = new DB();
Connection conn=db.dbConnect(
"jdbc:sqlserver://localhost:1433/tempdb","sa","manager");

db.createTables(conn);
}
}

class DB
{
public DB() {}

public Connection dbConnect(String db_connect_string,
String db_userid, String db_password )
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection(
db_connect_string, db_userid, db_password);
System.out.println ("connected");
return conn;

}
catch (Exception e)
{
e.printStackTrace();
return null ;
}
}

public void createTables(Connection conn)
{
String query;
Statement stmt;

try
{
query="create table cust_profile " +
"(name varchar(32), " +
"address1 varchar(50), " +
"city varchar(50), " +
"state varchar(50), " +
"country varchar(50))";
stmt = conn.createStatement();
stmt.executeUpdate( query);
stmt.close();
conn.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
};


When i was run this programme it was displayed as

java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
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.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at DB.dbConnect(createTable.java:24)
at createTable.main(createTable.java:8)
java.lang.NullPointerException
at DB.createTables(createTable.java:51)
at createTable.main(createTable.java:11)


i had set the CLASSPATH as "C:\Program Files\Microsoft SQL Server 2000 Driver for JDBC\lib\mssqlserver.jar;C:\Program Files\Microsoft SQL Server 2000 Driver for JDBC\lib\msbase.jar;C:\Program Files\Microsoft SQL Server 2000 Driver for JDBC\lib\msutil.jar;C:\Program Files\Java\jdk1.5.0\jre\lib\sqlserver.jar"



Could you know any one where i did a mistake. Please inform me at rajeshlab@yahoo.com
 
Jan Cumps
Bartender
Posts: 2577
11
C++ Linux Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try to find the one driver jar that you need, and add that to your classpath. It seems like you have added different (versions of?) sqlserver jars.

Try if putting the driver in a folder without spaces helps.

These people had a similar issue: Upgrading to SQL Server 2005 JDBC driver (SQLJDBC.jar) causes connection failure for exising SQLService
You will notice that the path is different in the SQL 2005 driver to that provided in the SQL 2000 driver.

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); // 2005 version
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); // 2000 version


Regards, Jan
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic