[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Main method

Posted on 2004-04-06
20
Medium Priority
?
192 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
17 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month17 days, 18 hours left to enroll

830 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