Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

java.lang.NoSuchMethodError

Status
Not open for further replies.

gargon1

IS-IT--Management
Jan 24, 2003
15
0
0
VI
Hello,

I have just started Java programing and am trying to create a java bean that connect to a MySQL database. The program compiles fine with no error but when I attempt to run the program I am getting the following error:

Exception in thread "main" java.lang.NoSuchMethodError: main

I have verified connectivity to the database using another Java program.
Am using jdk1.3.0_02 and Windows 2000 professional. I downloaded an example I wound on the Internet and adapted it to connect to the MySQL batabase with the same results.
Please help.

/**
* A test class using a simple database query.
*/

/* Standard java imports. */
import java.lang.*;
import java.sql.*;

public class SimpleDatabaseBean
{
String firstNme = "";
String lastName = "";

/**
* Run the query.
*/
public void runQuery()
throws Exception
{

/* Use JDBC to connect to the SAMPLE database.*/
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url = "jdbc:mysql:time_clock";
String dbUser = "somename";
String dbPass = "apassword";
Connection dbConn = DriverManager.getConnection(url,dbUser,dbPass);

/* If the connection fails, throw an Exception.*/
if(dbConn == null)
{
throw new Exception("The database connection failed.");
}

/* Build a SQL SELECT statement.*/
String sqlSelect = "SELECT FIRSTNME, LASTNAME FROM EMPLOYEE WHERE EMPNO = '000020'";

/* Run the SELECT statement.*/
Statement statement = dbConn.createStatement();
ResultSet result = statement.executeQuery(sqlSelect);

/* Get the result row.*/
while(result.next())
{
firstNme = result.getString("FIRSTNME");
lastName = result.getString("LASTNAME");
}

/* Close the connection.*/
dbConn.close();
}

/**
* Return the firstNme.
*/
public String getFirstNme()
{
return firstNme;
 
This is because there is no
Code:
main
in your code...Try the following:

Code:
/**
* A test class using a simple database query.
*/

/* Standard java imports. */
import java.lang.*;
import java.sql.*;

public class SimpleDatabaseBean
{
	String firstNme = "";
 	String lastName = "";
	public SimpleDatabaseBean()
	{}



 /**
  * Run the query.
  */
 public void runQuery()
   throws Exception
 {

   /* Use JDBC to connect to the SAMPLE database.*/
   Class.forName("com.mysql.jdbc.Driver").newInstance();
   String url = "jdbc:mysql:time_clock";
   String dbUser = "somename";
   String dbPass = "apassword";
   Connection dbConn = DriverManager.getConnection(url,dbUser,dbPass);

   /* If the connection fails, throw an Exception.*/
   if(dbConn == null)
   {
     throw new Exception("The database connection failed.");
   }

   /* Build a SQL SELECT statement.*/
   String sqlSelect = "SELECT FIRSTNME, LASTNAME FROM EMPLOYEE WHERE EMPNO = '000020'";

   /* Run the SELECT statement.*/
   Statement statement = dbConn.createStatement();
   ResultSet result = statement.executeQuery(sqlSelect);

   /* Get the result row.*/
   while(result.next())
   {
     firstNme = result.getString("FIRSTNME");
     lastName = result.getString("LASTNAME");
   }

   /* Close the connection.*/
   dbConn.close();
 }

 /**
  * Return the firstNme.
  */
 public String getFirstNme()
 {
   return firstNme;
}

public static void main (String[] args)
{
	SimpleDatabaseBean sdb = new SimpleDatabaseBean();
	try
	{
		sdb.runQuery();
		System.out.println(sdb.getFirstNme());
	}
	catch (Exception e)
	{
		e.printStackTrace();
	}
}
}
 
check out this thread:-

thread269-575107

~za~
You can't bring back a dead thread!
 
Thanks guys. That did the trick.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top