1

I am implementing a simple tutorial that

connects a java program to a java DB using the NetBeans 7 IDE . Here is the link to the tutorials.

http://www.homeandlearn.co.uk/java/connect_to_a_database_using_java_code.html

But when I run the project, NetBeans gives me the following error:

run:
java.lang.VerifyError: Constructor must call super() or this() before return in method   database_console.DBConnect.<init>()V at offset 0
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2442)
    at java.lang.Class.getMethod0(Class.java:2685)
    at java.lang.Class.getMethod(Class.java:1620)
    at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:492)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:484)
Exception in thread "main" Java Result: 1
BUILD SUCCESSFUL (total time: 12 seconds)

Below is the code for the project

    package database_console;

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;

  public static void main(String[] args) {           
            try {
          String host = "jdbc:derby://localhost:1527/Employees";
          String uName="faisal";
          String uPass="password";
          Connection con=DriverManager.getConnection(host, uName, uPass);        
            }
           catch ( SQLException err ) {
    System.out.println( err.getMessage( ) );
    }
        }
    }

any available suggestion to solve this problem

faisal abdulai
  • 3,739
  • 8
  • 44
  • 66

4 Answers4

2

If your above code is what you are using, then you need to put your entire code in class.

//Package declarations (if any)
//Import packages
public class className {

    public static void main(String[] args) {
        //Other code
    }
}
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Pradeep Simha
  • 17,683
  • 18
  • 56
  • 107
1

Please add class to your main(). Like following

package database_console;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBConnect 
{

    public static void main(String[] args) 
    {
        try 
        {
            String host = "jdbc:derby://localhost:1527/Employees";
            String uName = "faisal";
            String uPass = "password";
            Connection con = DriverManager.getConnection(host, uName, uPass);
        } 
        catch (SQLException err) 
        {
            System.out.println(err.getMessage());
        }
    }
}
codeMan
  • 5,730
  • 3
  • 27
  • 51
0

You did not follow the tutorial you linked correctly.

Your code should look like this:

package database_console;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBConnect {  // <--- This is missing from your code

    public static void main(String[] args) {
        try {
            String host = "jdbc:derby://localhost:1527/Employees";
            String uName = "faisal";
            String uPass = "password";
            Connection con = DriverManager.getConnection(host, uName, uPass);
        } catch (SQLException err) {
            System.out.println(err.getMessage());
        }
    }  // <--- This is also missing from your code
}

And it should be in a file named DBConnect.java in a package called database_console

user000001
  • 32,226
  • 12
  • 81
  • 108
  • You should point that the code comes from the [tutorial](http://www.homeandlearn.co.uk/java/connect_to_a_database_using_java_code.html) not from the top of your head. – Luiggi Mendoza Feb 24 '13 at 15:09
  • Yes exactly. OP sais it in the question – user000001 Feb 24 '13 at 15:09
  • after trying your suggstion , now the NetBeans IDE underlines he class declaration with the following message duplicate class database_console.DBConnect – faisal abdulai Feb 24 '13 at 15:19
  • @faisalabdulai Delete the entire contents of the file `DBConnect.java` and paste the above code. It should give no compiler errors. You have something else in there that is not showing. – user000001 Feb 24 '13 at 15:21
  • @faisalabdulai Also clean the project and build again – user000001 Feb 24 '13 at 15:25
0

this class is used to create DB connection

import java.sql.Connection;
import java.sql.*;

public class GetDBConnection 
{
    public static Connection getConnection(String[] args) 
    {
        String host = "jdbc:derby://localhost:1527/Employees";
        String uName="faisal";
        String uPass="password";
        Connection con=null;
        try 
        {
              con=DriverManager.getConnection(host, uName, uPass);        
        }
        catch ( SQLException err ) 
        {
        System.out.println( err.getMessage( ) );
        }
        return con;
    }

}

this class is used to get coonection and use it

public class UseDBConnection 
{
    public static void main(String[] args) 
    {
        GetDBConnection object = new GetDBConnection();
        System.out.println("now you can use this DB like you want it and can create as many connection as you want");
    }
}