?
Solved

Main method

Posted on 2004-04-06
20
Medium Priority
?
190 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 100 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month14 days, 4 hours left to enroll

800 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