[英]java.sql.SQLException: Column 'Max(category_id' not found
Here is my code.这是我的代码。 It gives me an exception error says "java.sql.SQLException: Column 'Max(category_id' not found.". Please help. Thanks in advance.它给了我一个异常错误,提示“java.sql.SQLException: Column 'Max(category_id' not found.”。请帮忙。提前致谢。
enter code here在此处输入代码
public class Category extends javax.swing.JFrame {公共类类别扩展 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);
}
}
The column has a default name but it isn't the same as the function, the easiest option would be to change all该列有一个默认名称,但它与函数不同,最简单的选择是更改所有
rs.getString("Max(category_id)");
to到
rs.getString(1);
Alternatively, name the column in your query.或者,为查询中的列命名。 Like,喜欢,
ResultSet rs = s.executeQuery("SELECT Max(category_id) AS FRED from category");
then use然后使用
rs.getString("FRED");
for example.例如。 Finally, you should be using getInt
or getLong
if the column is of those types (which I suspect because you are taking the MAX).最后,如果列属于这些类型(我怀疑是因为您使用的是 MAX),则您应该使用getInt
或getLong
。
I think in the line我认为在行
if(rs.getString("Max(category_id") == null) {
CategoryIDField.setText("C0001")
the quote should be after the round bracket.引号应该在圆括号之后。
使用 alisa select Max(category_id) as xxx from category
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.