Solved

Main method

Posted on 2004-04-06
20
189 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

729 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