我在将信息从一个班级转移到另一个班级时遇到了问题。例如,我的第二个类文件将读取csv文件并将数据提取到数组nextLine []中。然后将数据传输到另一个阵列d。然后该函数调用readDateBase,它将接受数组输入,d并执行set string函数。但是,我遇到了一个错误,导致我无法将数据发送到函数readDataBase(d);我不确定为什么......有人可以帮助我吗?非常感谢!
public void readDataBase(String d1, String d2, String d3, String d4 ) throws Exception {
try {
// This will load the MySQL driver, each DB has its own driver
Class.forName("com.mysql.jdbc.Driver");
// Setup the connection with the DB
connect = DriverManager
.getConnection("jdbc:mysql://localhost/MAXdb?"
+ "user=root&password=");
// Statements allow to issue SQL queries to the database
statement = connect.createStatement();
// Result set get the result of the SQL query
resultSet = statement
.executeQuery("select * from MAXdb.emcsg");
writeResultSet(resultSet);
// PreparedStatements can use variables and are more efficient
preparedStatement = connect
.prepareStatement("insert into MAXdb.emcsg values (default,?, ?, ? , ?, ?)");
// Parameters start with 1
preparedStatement.setString(1, d1);
preparedStatement.setString(2, d2);
preparedStatement.setString(3, d3);
preparedStatement.setString(4, d4);
preparedStatement.executeUpdate();
preparedStatement = connect
.prepareStatement("SELECT Date, Time, Demand, Supply, Price from MAXdb.emcsg");
resultSet = preparedStatement.executeQuery();
writeResultSet(resultSet);
}
我的第二堂课:
public void Csvreader() throws IOException {
try {
// TODO code application logic here
CSVReader reader = new CSVReader(new FileReader("D:/chart.csv"));
String nextLine[];
Mysql sen = new Mysql();
while ((nextLine = reader.readNext()) != null) {
sen.readDataBase(nextLine[0], nextLine[1], nextLine[2], nextLine[3]);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Opencsv.class.getName()).log(Level.SEVERE, null, ex);
}
}
答案 0 :(得分:1)
您的readDatabase()方法接受String[]
作为参数,并且您将String
传递给它。
如果使用Eclipse之类的IDE,IDE将抛出错误消息并突出显示该调用,并说它无效。 (另外,请尝试阅读错误消息!如果您无法理解它,也请发布错误消息。)
小心你的术语。您的错误不,您没有发现异常,而是您的逻辑总是提升它。抓住它不会使你的程序运行,它只会导致它无声地失败。