1

我正在实施一个简单的教程

使用 NetBeans 7 IDE 将 java 程序连接到 java DB。这是教程的链接。

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

但是当我运行该项目时,NetBeans 给了我以下错误:

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)

下面是项目的代码

    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( ) );
    }
        }
    }

解决此问题的任何可用建议

4

4 回答 4

2

如果您使用的是上面的代码,那么您需要将整个代码放在类中。

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

    public static void main(String[] args) {
        //Other code
    }
}
于 2013-02-24T14:59:28.520 回答
1

请将类添加到您的 main() 中。喜欢以下

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());
        }
    }
}
于 2013-02-24T15:09:31.043 回答
0

您没有按照正确链接的教程进行操作。

您的代码应如下所示:

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
}

它应该在一个名为DBConnect.java的包中的文件中database_console

于 2013-02-24T15:07:58.533 回答
0

此类用于创建数据库连接

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;
    }

}

此类用于获取连接并使用它

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");
    }
}
于 2013-02-24T15:13:21.430 回答