1

我正在运行我的程序,但出现此错误:

'[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());
        }
    }
4

3 回答 3

5

你应该使用pstmt.setDouble(...);

pstmt.setDouble(18, Double.parseDouble(txtTotal.getText()));
于 2013-04-04T07:39:44.873 回答
1

它在错误中说,

不允许从数据类型 char 到数据类型 money 的隐式转换

pTotalDue 不是字符。您需要设置数字类型值。

使用 BigDecimal。使用任何原语迟早都会导致精度问题。

于 2013-04-04T07:39:31.423 回答
-1

您正在为此代码使用 JDBC-ODBC 桥驱动程序,要配置桥驱动程序,请确保您已正确配置系统 DSN

对于 Windows 它是

控制面板-->管理工具-->数据源(ODBC)-->驱动程序的系统DSN设置。

或对您的代码使用瘦驱动程序,

对于瘦驱动程序,请参阅以下链接

http://www.razorsql.com/articles/oracle_jdbc_connect.html

于 2013-04-04T07:45:38.433 回答