两个类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();
}
}
}
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();
}
}
}
如
import qzjz.ReadDBConfig;
import qzjz.java,ReadDBConfig;注意指的是class文件,而不是java文件
是的,都在同一个文件夹下frilly(秋◆水)
试过了,还是报一样的错误。dyw31415926(守护)
引用了也是一样的错误。好象在同一个包内不需要引用的吧。
CustomSQL.java:28:找不到符号
符号:类 ReadDBConfig
位置:类 qzjz.CustomSQL
ReadDBConfig readdbconfig = new ReadDBConfig();
1
password = readdbconfig.password();
应该是
password = readdbconfig.getPassword();
2
getResultSet方法没有返回结果
你说删除掉ReadDBConfig.java 我试了下,也不行。而且包名我也不能去掉,因为有JSP页面需要引用该包的。youthon(可乐可口) ,
这两个地方我改掉了,但还是提示上述错误,请问你有编译通过了吗?sunnykun(阿包),shuai45()
可以举例说明吗?我一直用EDITPLUS,没有用过IDE。
你在command窗口下编译是会有这样的问题
把这两个文件放在qzjz文件夹下,编译是到qzjz的上层目录,输入
javac qzjz/ReadDBConfig.java进行编译,应该没问题