Can some one take a look at this and tell me why I am getting this error. I am trying to pull a table from a Mysql database and print it to a text file. I gives me the error listed above.
package db;
import java.io.*;
import java.sql.*;
import java.util.*;
public class TableToTextFile {
public static void main(String[] args) {
List<int[]> data = new ArrayList();
try {
Connection con = null;
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test", "root", "root");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("Select * from employee");
while (rs.next()) {
String id = rs.getString("emp_id");
String name = rs.getString("emp_name");
String address = rs.getString("emp_address");
String contactNo = rs.getString("contactNo");
data.add(id + " " + name + " " + address + " " + contactNo);
}
writeToFile(data, "Employee.txt");
rs.close();
st.close();
} catch (Exception e) {
System.out.println(e);
}
}
private static void writeToFile(java.util.List list, String path) {
BufferedWriter out = null;
try {
File file = new File(path);
out = new BufferedWriter(new FileWriter(file, true));
for (String s : list) {
out.write(s);
out.newLine();
}
out.close();
} catch (IOException e) {
}
}
}
Possibly because your list has been declared to accept integer arrays and you're passing in a string.
List<int[]> data = new ArrayList();
Change it to accept strings instead.
List<String> data = new ArrayList<>();
A much better and more object-oriented design would be to create a class called Employee
and use it instead.
public class Employee {
private String id;
private String name;
...
}
List<Employee> data = new ArrayList<>();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.