greddin
asked on
Eclipse: Could not find the main class. Program will exit.
Hi, I am a newbie using Eclipse. I created a Java application project and then added a new class to it named Connect. I then copied and pasted the following code below into it, that I got from Microsoft (http://support.microsoft.com/kb/313100).
It compiles ok but when I try to run it I get the following error:
Could not find the main class. Program will exit.
import java.*;
public class Connect{
private java.sql.Connection con = null;
private final String url = "jdbc:microsoft:sqlserver: //";
private final String serverName= "localhost";
private final String portNumber = "1433";
private final String databaseName= "pubs";
private final String userName = "user";
private final String password = "password";
// Informs the driver to use server a side-cursor,
// which permits more than one active statement
// on a connection.
private final String selectMethod = "cursor";
// Constructor
public Connect(){}
private String getConnectionUrl(){
return url+serverName+":"+portNum ber+";data baseName=" +databaseN ame+";sele ctMethod=" +selectMet hod+";";
}
private java.sql.Connection getConnection(){
try{
Class.forName("com.microso ft.jdbc.sq lserver.SQ LServerDri ver");
con = java.sql.DriverManager.get Connection (getConnec tionUrl(), userName,p assword);
if(con!=null) System.out.println("Connec tion Successful!");
}catch(Exception e){
e.printStackTrace();
System.out.println("Error Trace in getConnection() : " + e.getMessage());
}
return con;
}
/*
Display the driver properties, database details
*/
public void displayDbProperties(){
java.sql.DatabaseMetaData dm = null;
java.sql.ResultSet rs = null;
try{
con= this.getConnection();
if(con!=null){
dm = con.getMetaData();
System.out.println("Driver Information");
System.out.println("\tDriv er Name: "+ dm.getDriverName());
System.out.println("\tDriv er Version: "+ dm.getDriverVersion ());
System.out.println("\nData base Information ");
System.out.println("\tData base Name: "+ dm.getDatabaseProductName( ));
System.out.println("\tData base Version: "+ dm.getDatabaseProductVersi on());
System.out.println("Avalil able Catalogs ");
rs = dm.getCatalogs();
while(rs.next()){
System.out.println("\tcata log: "+ rs.getString(1));
}
rs.close();
rs = null;
closeConnection();
}else System.out.println("Error: No active Connection");
}catch(Exception e){
e.printStackTrace();
}
dm=null;
}
private void closeConnection(){
try{
if(con!=null)
con.close();
con=null;
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception
{
Connect myDbTest = new Connect();
myDbTest.displayDbProperti es();
}
}
Many thanks.
It compiles ok but when I try to run it I get the following error:
Could not find the main class. Program will exit.
import java.*;
public class Connect{
private java.sql.Connection con = null;
private final String url = "jdbc:microsoft:sqlserver:
private final String serverName= "localhost";
private final String portNumber = "1433";
private final String databaseName= "pubs";
private final String userName = "user";
private final String password = "password";
// Informs the driver to use server a side-cursor,
// which permits more than one active statement
// on a connection.
private final String selectMethod = "cursor";
// Constructor
public Connect(){}
private String getConnectionUrl(){
return url+serverName+":"+portNum
}
private java.sql.Connection getConnection(){
try{
Class.forName("com.microso
con = java.sql.DriverManager.get
if(con!=null) System.out.println("Connec
}catch(Exception e){
e.printStackTrace();
System.out.println("Error Trace in getConnection() : " + e.getMessage());
}
return con;
}
/*
Display the driver properties, database details
*/
public void displayDbProperties(){
java.sql.DatabaseMetaData dm = null;
java.sql.ResultSet rs = null;
try{
con= this.getConnection();
if(con!=null){
dm = con.getMetaData();
System.out.println("Driver
System.out.println("\tDriv
System.out.println("\tDriv
System.out.println("\nData
System.out.println("\tData
System.out.println("\tData
System.out.println("Avalil
rs = dm.getCatalogs();
while(rs.next()){
System.out.println("\tcata
}
rs.close();
rs = null;
closeConnection();
}else System.out.println("Error:
}catch(Exception e){
e.printStackTrace();
}
dm=null;
}
private void closeConnection(){
try{
if(con!=null)
con.close();
con=null;
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception
{
Connect myDbTest = new Connect();
myDbTest.displayDbProperti
}
}
Many thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Your .project and .classpath files look OK, but they show that you don't appear to have the Microsoft SQL Server Driver in your classpath?
do you see any errors on the project?
ASKER
In order to use SQL Server connectivity, do I need to actally download and install a JDBC driver for SQL 2000?
yes, you can download it from http://jtds.sourceforge.net/
The specific driver that is being referenced by the example code is here:
http://www.microsoft.com/downloads/details.aspx?familyid=9f1874b6-f8e1-4bd6-947c-0fc5bf05bf71&displaylang=en
http://www.microsoft.com/downloads/details.aspx?familyid=9f1874b6-f8e1-4bd6-947c-0fc5bf05bf71&displaylang=en
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sorry, I forgot to also recommend some points for keyurkarnik. Moderator please correct and do a four way split:
Split: Ajay-Singh {http:#18033729} & sciuriware {http:#18034773} & mukundha_expert {http:#18034884} & keyurkarnik {http:#18084117}
Split: Ajay-Singh {http:#18033729} & sciuriware {http:#18034773} & mukundha_expert {http:#18034884} & keyurkarnik {http:#18084117}
ASKER
--- .project file contents ---
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>JavaDatabase</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.co
</natures>
</projectDescription>
--- .classpath file contents ---
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path=""/>
<classpathentry kind="con" path="org.eclipse.jdt.laun
<classpathentry kind="output" path=""/>
</classpath>