我正在尝试计算一段时间以将其添加到工资中,加班时间存储在表出勤中,而工资存储在其他表中 包含每小时总数我想从 2 个表中获取结果,在 java 中显示它们的另一个存储以备将来参考
int Eid = Integer.parseInt(jTextField2.getText());
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/hr","root","adam");
String query="SELECT * FROM sallary where Eid ="+Eid +";"
java.sql.PreparedStatement preparedStatement = null;
preparedStatement = con.prepareStatement(query);
String select="select 83 *((time_to_sec(sum(overtime)) /60)/60)from att where Eid="+Eid +";";
java.sql.PreparedStatement preparedStatement2 = null;
preparedStatement2 = con.prepareStatement(select);
int eid;
double basic ,total,hourly;
ResultSet rs2 = preparedStatement2.executeQuery();
ResultSet rs = preparedStatement.executeQuery();
double overtime=rs2.getDouble(1);
// I tried this on MySQL command line it worked, however on java its error:
// the right syntax to use near 'sum(overtime)' at line 2
eid =rs.getInt("Eid");
basic =rs.getDouble("basic");
total=rs.getDouble("total");
hourly=rs.getDouble("hourly");
Object[] row = {eid,basic,total,hourly,overtime};
model = (DefaultTableModel) st.getModel();
model.addRow(row);
} catch (ClassNotFoundException | SQLException ex) {
Logger.getLogger(payroll.class.getName()).log(Level.SEVERE, null, ex);
}
您收到的实际错误消息类似于"请参阅您的MySQL版本的手册,了解在第2行的'sum(加班('附近使用的正确语法。此错误消息会准确告诉您需要执行的操作。
查看手册:
https://dev.mysql.com/doc/refman/5.7/en/select.html
您可以看到 SELECT 语句语法末尾不包含分号。
分号是语句分隔符,在控制台上给出多个语句时很有用。JDBC 查询一次是一个语句,因此分号没有意义,并且是语法错误。
顺便说一下,您应该省略语句Class.forName("com.mysql.jdbc.Driver");
.根本不需要它。它在早期版本中是必需的,但在过去 15 年左右的时间里是不必要的。