What is the correct way to make a classpath?

T

Tom

Hi everyone,

I have a question on the correct way to make a classpath. What i am
trying to do is get my Java program to connect to my database. The
database i am using is Microsoft SQL server 2000. I downloaded and
installed the Microsoft SQL Server 2000 Driver for JDBC. I had my
classpath set up as:

C:\Microsoft SQL Server 2000 Driver for
JDBC\lib\mssqlserver.jar;C:\Microsoft SQL Server 2000 Driver for
JDBC\lib\msutil.jar;C:\Microsoft SQL Server 2000 Driver for
JDBC\lib\msbase.jar

The way i set my classpath up was in my enviormental variables and the
variable is named CLASSPATH.(Im using WIN XP PROF, not sure if this
matters or not)

I added a bit of code into my program to see which jar files it is
using:
System.out.println("My classpath is " +
System.getProperty("java.class.path") );

And none of the above jar files are being used.
Causing me to have a driver not found error.
Iv included the code below if anyone thinks possibly the error is in
the code. Its a program to be used at school for class.(im a newby
student)
If anyone could offer me any help it would be greatly appreciated.
Thanks,
Tom


package thomas;

import java.awt.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;

public class displayAuthors extends JFrame {


public displayAuthors()
{
super( "Authors Table of Books Database" );


try {


Class.forName( "com.microsoft.jdbc.sqlserver.SQLServerDriver"
);

Properties p = new Properties();
p.put("selectMethod","cursor");
p.put("user","sa");
p.put("password","tom");

Connection connection = DriverManager.getConnection(
"jdbc:microsoft:sqlserver://TOMLAP2:1433",p );


Statement statement = connection.createStatement();


ResultSet resultSet =
statement.executeQuery( "SELECT * FROM authors" );


StringBuffer results = new StringBuffer();
ResultSetMetaData metaData = resultSet.getMetaData();
int numberOfColumns = metaData.getColumnCount();

for ( int i = 1; i <= numberOfColumns; i++ ) {
results.append( metaData.getColumnName( i )
+ "\t" );
}

results.append( "\n" );

while ( resultSet.next() ) {

for ( int i = 1; i <= numberOfColumns; i++ ) {
results.append( resultSet.getObject( i )
+ "\t" );
}

results.append( "\n" );
}


statement.close();
connection.close();


JTextArea textArea = new JTextArea(
results.toString() );
Container container = getContentPane();

container.add( new JScrollPane( textArea ) );

setSize( 300, 100 );
setVisible( true );
}

catch ( SQLException sqlException ) {
System.out.println("My classpath is " +
System.getProperty("java.class.path") );
JOptionPane.showMessageDialog( null,
sqlException.getMessage(), "Database Error",
JOptionPane.ERROR_MESSAGE );

System.exit( 1 );
}


catch ( ClassNotFoundException classNotFound ) {
System.out.println("My classpath is " +
System.getProperty("java.class.path") );
JOptionPane.showMessageDialog( null,
classNotFound.getMessage(), "Driver Not Found",
JOptionPane.ERROR_MESSAGE );

System.exit( 1 );
}
}


public static void main( String args[] )
{
displayAuthors window = new displayAuthors();

window.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top