我正在实施一个简单的教程
将java程序连接到java DB 使用NetBeans 7 IDE 。 这是教程的链接。
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 :(得分:2)
如果上面的代码是您正在使用的代码,那么您需要将整个代码放在课堂上。
//Package declarations (if any)
//Import packages
public class className {
public static void main(String[] args) {
//Other code
}
}
答案 1 :(得分:1)
请将课程添加到您的主要()。喜欢以下
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());
}
}
}
答案 2 :(得分: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
的文件中
答案 3 :(得分: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");
}
}