Results 1 to 2 of 2
  1. #1
    jprog is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default JavaBean - invalid method declaration and return type required

    Hello, guys

    i need some help. I wrote a java bean for database connection

    Java Code:
    import java.io.*;
    import java.sql.*;
    
    public class DBconnection {
    
    	String driverName = "org.gjt.mm.mysql.Driver";
        String serverName = "localhost";
    	String database = "data";
    	String dbURL = "jdbc:mysql://" + serverName +  "/" + database; // JDBC url
    	String username = "root";
    	String password = "root";
    
        private Connection dbConnection;
    	Statement statement;
        ResultSet resultSet;
    
        public DBconnection() {
        	super();
        }
    
        public boolean connect() throws ClassNotFoundException, SQLException {
        	Class.forName(driverName);
    		dbConnection = DriverManager.getConnection(dbURL);
    		return true;
        }
    
        public void close() throws SQLException {
        	dbConnection.close();
        }
    
        public execSQL(String sql) throws SQLException {
    
        	statement = dbConnection.createStatement();
        	resultSet = statement.executeQuery(sql);
        	return (resultSet == null) ? null : resultSet;
        }
    }
    and i get the following error:

    DBconnection.java:31: invalid method declaration; return type required
    public execSQL(String sql) throws SQLException {

  2. #2
    shnplr is offline Member
    Join Date
    May 2011
    Posts
    6
    Rep Power
    0

    Default

    Hi,

    your missing the return type in your execSQL method declaration.
    since you've got ResultSet as a member attribute you probably want:

    Java Code:
    public void execSQL(String sql) throws SQLException {
        	statement = dbConnection.createStatement();
        	resultSet = statement.executeQuery(sql);
    //    	return (resultSet == null) ? null : resultSet;
        }
    also don't forget you also need to close the ResultSet and Statement

    Java Code:
    public void close() throws SQLException {
      try {
            resultSet.close();
      } catch (Exception e) {}
      try {
            statement.close();
      } catch (Exception e) {}
      try {
        	dbConnection.close();
      } catch (Exception e) {}
    }

Similar Threads

  1. invalid method declaration; return type required
    By XaviannNJ in forum JCreator
    Replies: 7
    Last Post: 11-25-2010, 02:59 PM
  2. Replies: 3
    Last Post: 06-05-2010, 09:05 PM
  3. invalid method declaration error
    By bsarules in forum Java Applets
    Replies: 7
    Last Post: 05-25-2010, 07:06 PM
  4. BubbleSorting Problem: invalid method declaration
    By SwEeTAcTioN in forum New To Java
    Replies: 6
    Last Post: 12-02-2009, 04:47 AM
  5. Error: invalid method declaration
    By silvia in forum New To Java
    Replies: 1
    Last Post: 07-27-2007, 01:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •