为什么同一个包下的类不能引用,编译错误。
两个类ReadDBConfig.java 和 CustomSQL.java,ReadDBConfig.java 编译通过,在JSP中引用也可以用,但CustomSQL.java就是编译出两个错误。
package qzjz;
import java.sql.*;
import java.io.*;
import java.util.Properties;
public class ReadDBConfig {
//数据库驱动
private String driver;
//数据库URL
private String url;
//数据库用户名
private String user;
//数据库用户密码
private String password;
private Properties props = new Properties();
public ReadDBConfig() {
try{
//读取配置文件
InputStream in = new FileInputStream( "dbConfig.properties ");
props.load(in);
//读取属性文件的值
driver = props.getProperty( "driver ");
url = props.getProperty( "url ");
user = props.getProperty( "user ");
password = props.getProperty( "password ");
//关闭输入流
in.close();
if(driver == null||null == url||null == user||null == password)
System.out.println( "读取属性文件的资料不全。 ");
} catch(Exception e) {
System.out.println( "读取数据库配置失败 ");
e.printStackTrace();
}
}
//属性操作
public void setDriver(String driver) {
this.driver = driver;
}
public void setURL(String url) {
this.url = url;
}
public void setUser(String user) {
this.user = user;
}
public void setPassword(String password) {
this.password = password;
}
public String getDriver() {
return driver;
}
public String getURL() {
return url;
}
public String getUser() {
return user;
}
public String getPassword() {
return password;
}
//写入数据库配置文件
public void writeDBConfig() {
props.setProperty( "driver ",driver);
props.setProperty( "url ",url);
props.setProperty( "user ",user);
props.setProperty( "password ",password);
try {
OutputStream ops = new FileOutputStream( "dbConfig.properties ");
props.store(ops,null);
ops.close();
} catch(IOException ioe) {
System.out.println( "保存数据库配置失败! ");
ioe.printStackTrace();
}
}
}
package qzjz;
import java.sql.*;
public class CustomSQL {
private String sqlyuju;
private String driver;
private String url;
private String user;
private String password;
private Connection con;
private Statement smt;
private ResultSet rst;
private int i;
public void CustomSQL() {
ReadDBConfig readdbconfig = new ReadDBConfig();//就是这句编译出错
driver = readdbconfig.getDriver();
url = readdbconfig.getURL();
user = readdbconfig.getUser();
password = readdbconfig.password();
}
public void setSQLyuju(String sqlyuju) {
this.sqlyuju = sqlyuju;
}
public String getSQLyuju() {
return sqlyuju;
}
public ResultSet getResultSet() {
try {
Class.forName(driver);
} catch(Exception e) {
System.out.println( "无法加载驱动程序! " + driver);
e.printStackTrace();
}
/********数据库查询*********/
try {
con = DriverManager.getConnection(url,user,password);
smt = con.createStatement();
rst = smt.executeQuery(sqlyuju);
} catch(SQLException ex) {
System.out.println( "数据库连接查询错误 " + ex);
ex.printStackTrace();
}
}
public int getUpdate() {
try {
Class.forName(driver);
} catch(Exception e) {
System.out.println( "无法加载驱动程序! " + driver);
e.printStackTrace();
}
/********数据库操作*********/
try {
con = DriverManager.getConnection(url,user,password);
smt = con.createStatement();
i = smt.executeUpdate(sqlyuju);
} catch(SQLException ex) {
System.out.println( "数据库连接查询错误! " + ex);
ex.printStackTrace();
}
return i;
}
public void closeAll() {
try {
con.close();
smt.close();
rst.close();
} catch(SQLException ex) {
System.out.println( "数据库连接无法关闭 " + ex);
ex.printStackTrace();
}
}
}
[解决办法]
我在eclipse下做的,编译通过
你在command窗口下编译是会有这样的问题
把这两个文件放在qzjz文件夹下,编译是到qzjz的上层目录,输入
javac qzjz/ReadDBConfig.java进行编译,应该没问题
