java.sql.SQLException: Column index out of range.
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1094)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:997)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:983)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:928)
at com.mysql.jdbc.ResultSetMetaData.getField(ResultSetMetaData.java:407)
at com.mysql.jdbc.ResultSetMetaData.getColumnName(ResultSetMetaData.java:258)
at a.MyFrame.createTablePanel(MyFrame.java:58)
at a.MyFrame.<init>(MyFrame.java:24)
at a.MyFrame.main(MyFrame.java:19)
这是我在MyFrame类中得到的堆栈跟踪。这些是代码:
MyFrame.createTablePanel().
public JPanel createTablePanel () {
DefaultTableModel tableModel = new DefaultTableModel();
DB dB = new DB();
dB.connectDB();
ResultSet rs = dB.getData();
try {
ResultSetMetaData rsMD = rs.getMetaData();
String[] arr = new String[rsMD.getColumnCount()];
for (int i = 0; i <= rsMD.getColumnCount(); i++) {
arr[i] = rsMD.getColumnName(i+1);
}
tableModel.setColumnIdentifiers(arr);
while (rs.next()) {
for (int i = 0; i <= rsMD.getColumnCount(); i++) {
arr[i] = rs.getString(i+1);
}
tableModel.addRow(arr);
}
} catch (SQLException e) {
e.printStackTrace();
}
JPanel tablePanel = new JPanel(new BorderLayout(5,5));
JTable table = new JTable(tableModel);
tablePanel.add(new JScrollPane(table), BorderLayout.CENTER);
return tablePanel;
}
它使用DB类与数据库通信:
DB Class.
public class DB {
private final String driver = "com.mysql.jdbc.Driver";
private final String url = "jdbc:mysql://localhost:3306/test";
private final String user = "root";
private final String pass = "khjthat";
private final String table = "ungvien";
Statement stmt;
PreparedStatement pstmt;
ResultSet rs;
ResultSetMetaData rsMD;
String str; //Query String.
Connection connection;
public DB() {
}
public void connectDB () {
try {
Class.forName(driver);
System.out.println("Driver Loaded!");
connection = DriverManager.getConnection(url,user,pass);
System.out.println("Connected!");
} catch (ClassNotFoundException e) {
System.out.println("Class Not Found!");
} catch (SQLException e) {
System.out.println("Connection Error!");
}
}
public void showData(ResultSet rs) {
try {
rsMD = rs.getMetaData();
for (int i = 0; i < rsMD.getColumnCount(); i++) {
System.out.printf("t%-10s ", rsMD.getColumnName(i+1));
}
System.out.println();
while (rs.next()) {
System.out.printf("t%-10s t%-10s t%-10s t%-10s n", rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4));
}
} catch (SQLException e) {
}
}
public ResultSet getData() {
str = "select * from " + table + ";";
try {
stmt = connection.createStatement();
rs = stmt.executeQuery(str);
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
public ResultSet getDataTen(String ten) {
str = "select ten,tuoi,truong,kn from " + table + " where ten = ? " ;
try {
pstmt = connection.prepareStatement(str);
pstmt.setString(1, ten);
rs = pstmt.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
public void deleteTen (String ten) {
str = "delete from " + table + " where ten = ? ";
try {
pstmt = connection.prepareStatement(str);
pstmt.setString(1, ten);
if (pstmt.executeUpdate() > 0) {
System.out.println("Delete Successful!");
} else {
System.out.println("Delete Error!");
}
} catch (SQLException e) {
System.out.println("Delete Error!");
e.printStackTrace();
}
}
public void insert (UngVien ungVien) {
str = "insert into " + table + " values (?,?,?,?) ";
try {
pstmt = connection.prepareStatement(str);
pstmt.setString(1, ungVien.getTen());
pstmt.setInt(2, ungVien.getTuoi());
pstmt.setString(3, ungVien.getTruong());
pstmt.setInt(4, ungVien.getKn());
if (pstmt.executeUpdate() > 0) {
System.out.println("Insert Successful!");
} else {
System.out.println("Insert Error!");
}
} catch (SQLException e) {
System.out.println("Insert Error!");
e.printStackTrace();
}
}
public void updateTen (String ten, UngVien ungVien) {
str = "update " + table + " set tuoi =?, truong =?, kn =? where ten = ?";
try {
pstmt = connection.prepareStatement(str);
pstmt.setInt(1, ungVien.getTuoi());
pstmt.setString(2, ungVien.getTruong());
pstmt.setInt(3, ungVien.getKn());
pstmt.setString(4, ten);
if (pstmt.executeUpdate() > 0) {
System.out.println("Update Successful!");
} else {
System.out.println("Update Error!");
}
} catch (SQLException e) {
System.out.println("Update Error!");
e.printStackTrace();
}
}
public static void main (String[] args) {
DB dB = new DB();
dB.connectDB();
dB.showData(dB.getData());
}
}
我测试了DB类,一切正常。
我试着调试这个整个下午,但我不能理解如果它就在那里:(
我是JAVA的新手,我希望这个问题没有打扰到你们。
请给我一些技巧。
正如user3145373在评论中提到的
从for (int i = 0; i <= rsMD.getColumnCount(); i++)
删除equal(=)号,按
for (int i = 0; i < rsMD.getColumnCount(); i++)
进一步解释上述答案
假设有人让你给我10个硬币,并将第一枚硬币的计数设为0,那么在这种情况下,你会一直给他,直到数到9为止。
同样的情况,你已经初始化了i = 0
,所以你需要迭代它,直到计数达到totalCount-1
。
或者改成
for (int i = 1; i <= rsMD.getColumnCount(); i++)