Java Programming: Advanced topics - Sample code
- From: Clive_S <cliveswan@xxxxxxxxxxx>
- Date: Fri, 16 Nov 2007 04:04:45 -0800 (PST)
Hi,
I am trying to get a Java Bean sample code from Java Programming:
Advanced topics book working.
I have modified the code for SQL Server, it does not work??
The Java Virtual Machine throws a "could not find the main class"????
Could anyone point out what I am doing wrong.
I can get a db connection in a JSP page, am trying to create a
container that I can call the
variable from the JSP page.
Thanks for your help.
Clive
java.lang.NoClassDefFoundError: project1/DBdemo
Exception in thread main
package project1;
import java.sql.*;
/**
* Class to create connection to a database and
* and demonstrate basic SQL queries
*/
public class DBdemo {
/**
* the connection object
*/
static Connection connection = null;
// select DB2 type 2 driver
static final String dbDriver = "net.sourceforge.jtds.jdbc";
static final String dbUrl = "jdbc:jtds:sqlserver://localhost:
1032/";
// use the SKICLUB database
static final String dbName = "Calibration";
// set dbUser to any user on your Windows OS
static final String dbUser = "userid";
// set dbPassword to Windows password for dbUser
static final String dbPassword = "password";
/**
* method to open the connection
*/
public Connection getConnection() {
if ( connection != null )
return connection;
try {
connection = DriverManager.getConnection(
dbUrl, dbUser, dbPassword );
} catch( SQLException e ) {
System.err.println("Cannot connect to database: for SQl
server, " + "check that SQl is running and " + "the SKICLUB database
exists." );
}
return connection;
}
public ResultSet getAllMemberInfo() {
ResultSet rs = null;
try {
Statement statement =
getConnection().createStatement();
String sql = "Select * from Prop1_Wallfinish,
Users where Prop1_Wallfinish.SurveyID= Users.SurveyIDS";
System.out.println( sql );
statement.executeQuery( sql );
rs = statement.getResultSet();
} catch( SQLException e ) {
System.out.println(
"SQLException " + e.getMessage() );
} finally {
return rs;
}
}
public static void main(String[] args) {
DBdemo dbdemo = new DBdemo();
System.out.println("Getting Database driver" );
try {
Class.forName( dbDriver );
} catch( ClassNotFoundException e ) {
System.err.println("Cannot load database
driver: for SQL, " + "your classpath must include " + "SQLLIB\\JAVA12\
\DB2JAVA.ZIP." );
}
System.out.println(
"Getting Database connection" );
Connection connection = dbdemo.getConnection();
if ( connection == null )
System.exit(0);
System.out.println( "Database ready" );
try {
// demonstrate a SELECT Statement
dbdemo.getAllMemberInfo();
connection.close();
} catch (Exception e) {
System.err.println(
e.getClass().getName() + ": " +
e.getMessage() );
}
}// end public main
} // end DB
.
- Follow-Ups:
- Re: Java Programming: Advanced topics - Sample code
- From: Clive_S
- Re: Java Programming: Advanced topics - Sample code
- Prev by Date: Re: Beans with JSF
- Next by Date: Re: Java Programming: Advanced topics - Sample code
- Previous by thread: Beans with JSF
- Next by thread: Re: Java Programming: Advanced topics - Sample code
- Index(es):
Relevant Pages
|