2013-02-24 77 views
0

我實現一個簡單的教程,連接到Java DB與Java程序

使用NetBeans IDE 7 一個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()); 
    } 
     } 
    } 

任何可用的建議,以解決這一問題的代碼

+0

那是你真正的代碼(不多也減)? – 2013-02-24 14:58:56

+0

請發佈您的整個代碼(是的,不要介意它是否是代碼牆)。 – 2013-02-24 15:04:58

+0

是的,這是整個代碼@ Luiggi門多薩 – 2013-02-24 15:07:05

回答

0

您沒有按照正確鏈接的tutorial

您的代碼應該是這樣的:

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

+0

您應該指出,代碼來自[教程](http://www.homeandlearn.co.uk/java/connect_to_a_database_using_java_code.html),不是來自您的頭頂。 – 2013-02-24 15:09:24

+0

是的。 OP在嘗試使用提示後出現在 – user000001 2013-02-24 15:09:45

+0

這個問題中,現在NetBeans IDE強調了他使用以下消息重複類聲明瞭類聲明database_console.DBConnect – 2013-02-24 15:19:39

2

如果你的上面的代碼是你正在使用的,那麼你需要把你的整個代碼類。

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

    public static void main(String[] args) { 
     //Other code 
    } 
} 
+0

,但認爲上述代碼中的public clas className是類聲明 – 2013-02-24 15:03:41

+0

@faisalabdulai,在您的代碼中沒有公共類或非公共類。 – 2013-02-24 15:17:21

0

請添加類到您的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()); 
     } 
    } 
} 
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; 
    } 

} 

這個類是用來獲取coonection並用它

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