gpt4 book ai didi

包含 2 个类的 Java 编译

转载 作者:太空宇宙 更新时间:2023-11-04 08:49:35 24 4
gpt4 key购买 nike

这是一些非常简单的 java OOP,但我有一段时间没有这样做了...从另一个 java 类引用一个 java 类时,我收到“符号未找到”错误

第 1 类:

package toaV2;

import java.sql.Connection;

public class vehicle_model
{
public db_model DB;
public Connection conn;

public static void main(String[] args) {
vehicle_model v = new vehicle_model("system");
}

public vehicle_model(String sys) {
DB = new db_model(sys);
conn = DB.connect();

if(conn != null) {
System.err.println("Got a connection.");
}
else {
System.err.println("Couldn't get a connection...");
}
}
}

第 2 类:

package toaV2;

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

public class db_model
{
private static String driver = "com.mysql.jdbc.Driver";

private static String dbUser = "user";
private static String dbPass = "pass";
private static String dbUrl = "jdbc:mysql://url";

private static String system;
public static Connection conn;

public db_model(String sys)
{
system = sys;
}

public static Connection connect()
{
conn = null;
try
{
String dbName = system.toUpperCase();
String dbHost = dbUrl + dbName;

Class.forName(driver).newInstance();
conn = DriverManager.getConnection(dbUrl, dbUser, dbPass);
}
catch(Exception e)
{
System.err.println("Exception: " + e.getMessage());
}

return conn;
}
}

以及我在编译时遇到的错误:

$ javac vehicle_model.java vehicle_model.java:10: cannot find symbolsymbol  : class db_modellocation: class toaV2.vehicle_model    public db_model DB;               ^vehicle_model.java:24: cannot find symbolsymbol  : class db_modellocation: class toaV2.vehicle_model        DB = new db_model(system);                         ^2 errors

最佳答案

编译时需要提供其他java文件的类路径。即 javac -classpath path/to/class2vehicle_model.java

关于包含 2 个类的 Java 编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3653835/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com