Solved

Main method

Posted on 2004-04-06
20
186 Views
Last Modified: 2010-03-31
heres a simple code to connect to db.. im getting the error : java.lang.NoSuchMethodError: main
Exception in thread "main"

import java.sql.*;
public class Trry
{
public static Connection getConnection ()
throws ClassNotFoundException, SQLException
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

  String protocol="jdbc:odbc:";
  String url=protocol+"TryJDBC";

  Connection conn = DriverManager.getConnection(url,"","");
  Statement stmt = conn.createStatement();

  String update =
      "CREATE TABLE StudentdTable "+
         "(id CHAR(6) NOT NULL, "+
         " name VARCHAR(20) )";
  int a = stmt.executeUpdate(update);

  String query =
      "SELECT * FROM StudentdTable ";
 
  ResultSet rs = stmt.executeQuery(query);

  ResultSetMetaData rsmd = rs.getMetaData();

  int cols = rsmd.getColumnCount();

  while(rs.next())
  {
   for(int i=1; i<=cols; i++)
   {
    System.out.println(rs.getString(i));
   } // for

   System.out.println();

  } // while

  rs.close();
  stmt.close();
  conn.close();
  return conn ;
}
}
0
Comment
Question by:johnbert_99
  • 8
  • 5
  • 3
  • +1
20 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 25 total points
ID: 10764969
You need a main method to run an app:

public static void main(String[] args) {
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10764983
Also that function you're calling need not return anything:

public static void doSql ()
throws ClassNotFoundException, SQLException
{
// etc but NOT return conn ;

}

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10764986
That mean your main method can be

public static void main(String[] args) {
      doSql();
}
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:johnbert_99
ID: 10765191
im sorry but could u please giv an example with my code,  as im not sure wether im supposed to have a method for connection and seperate for sql statements etc...
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10765233
Make:

>> public static Connection getConnection ()

as: public static void doTask ()

Remove: return con ;

and in the main () method, call the doTask () method:

public static void main ( String args[] )
  throws Exception
{
  doTask () ;

}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10765241
I've given you the very few changes you need (hardly any) already. You can keep it simple and just have the one method for now
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10765257
If you want to have separate methods for getting Connection and doing the SQL tasks (which is better), then you should have something like:

class Trry
{
  public static Connection getConnection ()
  ..
  .. // all the code for getting the connection and returning it

  public static CachedRowSet executeQuery ( String sQuery )
  {
    // call getConnection (), execute the Query, get the result and close the Connection

  }

  // and so on

}
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10765260
This is what CEHJ means:

import java.sql.*;
public class Trry
{
public void doSql() throws ClassNotFoundException, SQLException {
  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

  String protocol="jdbc:odbc:";
  String url=protocol+"TryJDBC";

  Connection conn = DriverManager.getConnection(url,"","");
  Statement stmt = conn.createStatement();

  String update =
      "CREATE TABLE StudentdTable "+
         "(id CHAR(6) NOT NULL, "+
         " name VARCHAR(20) )";
  int a = stmt.executeUpdate(update);

  String query =
      "SELECT * FROM StudentdTable ";
 
  ResultSet rs = stmt.executeQuery(query);

  ResultSetMetaData rsmd = rs.getMetaData();

  int cols = rsmd.getColumnCount();

  while(rs.next())
  {
   for(int i=1; i<=cols; i++)
   {
    System.out.println(rs.getString(i));
   } // for

   System.out.println();

  } // while

  rs.close();
  stmt.close();
  conn.close();
  }

  public static void main(String[] args) {
     doSql();
  }
 
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10765278
mayankeagle, i don't think that advances at all on what i've already said. Just make the changes i suggested. You can catch the exceptions in main

public static void main(String[] args) {
     try {
          doSql();
     }
     catch(Exception e) {
          e.printStackTrace();
     }
}
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10765288
>> im getting the error : java.lang.NoSuchMethodError: main
If you run a program/class java is always looking for a method called main().
In your case there wasn't, hence the error.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10765298
>> i don't think that advances at all on what i've already said

No, but the questioner is a beginner (which we know from his previous question that was answered just a few minutes back), that's why I provided some steps:

>> Make
>> Remove
>> throws Exception (in main ())

Later, we can take the class with multiple methods (one for getting connection, one for executing the query, etc) when he understands the basics.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10765314
>>No, but the questioner is a beginner

I'm aware of that but suggesting essentially the same as i've already done but changing the names is just going to confuse the issue
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10765330
johnbert_99, the upshot is, apart from the major omission (the main method) very little needs to be changed and your code can be as per zzynx's synthesis, but without the omission of catching the exception in main , the code for which i've provided - so now you've got it all
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10765334
Well, actually I just wanted to add the 'throws Exception' otherwise he would have reported the compilation error to us. And it would have taken more comments to fix it. Just added the first two points (I agree, which you had already said) to make my comment complete.

Cheers~!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10765395
OK ;-)
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10958297
Accept CEHJ's comment as answer.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10958359
I agree with mayank
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now