我正在运行我的程序,我有这个错误:
'[Microsoft][ODBC SQL Server Driver][SQL Server]Disallowed implicit conversion from data type char to data type money, table 'OJT.dbo.Patients', column 'pTotalDue'. Use the CONVERT function to run this query'
我该如何解决?有谁可以帮助我?
这是我的代码:
private void btnInsertActionPerformed(java.awt.event.ActionEvent evt) {
PreparedStatement pstmt = null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:OJT_dsn";
String user = "******";
String pass = "******";
String sql = "INSERT INTO dbo.Patients"
+ "(pIDNo,pLName,pFName,pMI,pSex,pStatus,pTelNo,pDocID,pAddr,pStreet,pBarangay,pCity,pProvince,pLnameKIN,pFNameKIN,pMIKIN,pRelationKIN,pTotalDue)"
+ "VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
Connection connection = DriverManager.getConnection(url, user, pass);
pstmt = connection.prepareStatement(sql);
pstmt.setString(1, txtPatientID.getText());
pstmt.setString(2, txtpLName.getText());
pstmt.setString(3, txtpFName.getText());
pstmt.setString(4, txtpMI.getText());
pstmt.setString(5, txtSex.getText());
pstmt.setString(6, txtStatus.getText());
pstmt.setString(7, txtpTel.getText());
pstmt.setString(8, txtpDoctor.getText());
pstmt.setString(9, txtStreetNo.getText());
pstmt.setString(10, txtStreetName.getText());
pstmt.setString(11, txtBarangay.getText());
pstmt.setString(12, txtCity.getText());
pstmt.setString(13, txtProvince.getText());
pstmt.setString(14, txtLnameKIN.getText());
pstmt.setString(15, txtFNameKIN.getText());
pstmt.setString(16, txtMIKIN.getText());
pstmt.setString(17, txtRelation.getText());
pstmt.setString(18, txtTotal.getText());
pstmt.executeUpdate();
JOptionPane.showMessageDialog(rootPane, "Patient Added!");
}catch (Exception ex) {
JOptionPane.showMessageDialog(this, ex.getMessage());
}
}
答案 0 :(得分:5)
您应该使用pstmt.setDouble(...);
pstmt.setDouble(18, Double.parseDouble(txtTotal.getText()));
答案 1 :(得分:1)
它在错误中说,
禁止从数据类型char到数据类型money的隐式转换
pTotalDue不是char。您需要设置数字类型值。
使用BigDecimal。使用任何原语迟早会导致精度问题。
答案 2 :(得分:-1)
您正在为此代码使用JDBC-ODBC Bridge驱动程序,以配置网桥驱动程序,确保您已正确配置系统DSN
对于Windows来说是
控制面板 - >管理工具 - >数据源(ODBC) - >系统DSN设置 驱动程序。
或在代码中使用瘦驱动程序,
对于瘦驱动程序,请参阅以下链接