Solved

Main method

Posted on 2004-04-06
20
182 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
Comment Utility
You need a main method to run an app:

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

Expert Comment

by:CEHJ
Comment Utility
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
Comment Utility
That mean your main method can be

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

Author Comment

by:johnbert_99
Comment Utility
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:mayankeagle
Comment Utility
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
Comment Utility
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:mayankeagle
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
Comment Utility
>> 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:mayankeagle
Comment Utility
>> 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
Comment Utility
>>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
Comment Utility
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:mayankeagle
Comment Utility
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
Comment Utility
OK ;-)
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
Accept CEHJ's comment as answer.
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
I agree with mayank
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
strCopies  challenge 17 73
json format text only 4 65
Image decoding from Camera 3 42
Java / Linux and Regular Expressions 11 43
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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

772 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

14 Experts available now in Live!

Get 1:1 Help Now