[英]java.sql.SQLException: Column 'Max(category_id' not found
这是我的代码。 它给了我一个异常错误,提示“java.sql.SQLException: Column 'Max(category_id' not found.”。请帮忙。提前致谢。
在此处输入代码
公共类类别扩展 javax.swing.JFrame {
/**
* Creates new form Category
*/
public Category() {
initComponents();
DisplayTable();
autoID();
}
//Display Table
private void DisplayTable() {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/inventory?useTimezone=true&serverTimezone=UTC", "root", "ichigo197328");
String sql = "SELECT * FROM category";
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
jTable1.setModel(DbUtils.resultSetToTableModel(rs));
}
catch(Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
public void autoID() {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/inventory?useTimezone=true&serverTimezone=UTC", "root", "ichigo197328");
Statement s = conn.createStatement();
ResultSet rs = s.executeQuery("SELECT Max(category_id) from category");
rs.next();
rs.getString("Max(category_id)");
if(rs.getString("Max(category_id") == null) {
CategoryIDField.setText("C0001");
}
else {
Long id = Long.parseLong(rs.getString("Max(category_id").substring(2, rs.getString("Max(category_id").length()));
id++;
CategoryIDField.setText("C0" + String.format("%03d", id ));
}
}
catch(ClassNotFoundException e) {
Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, e);
}
catch(SQLException e) {
Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, e);
}
}
该列有一个默认名称,但它与函数不同,最简单的选择是更改所有
rs.getString("Max(category_id)");
到
rs.getString(1);
或者,为查询中的列命名。 喜欢,
ResultSet rs = s.executeQuery("SELECT Max(category_id) AS FRED from category");
然后使用
rs.getString("FRED");
例如。 最后,如果列属于这些类型(我怀疑是因为您使用的是 MAX),则您应该使用getInt
或getLong
。
我认为在行
if(rs.getString("Max(category_id") == null) {
CategoryIDField.setText("C0001")
引号应该在圆括号之后。
使用 alisa select Max(category_id) as xxx from category
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.