Crystal Maiden
Crystal Maiden

Reputation: 364

Conversion of data type in JAVA

I'm running my program and I have this error :

'[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'

how can I fix it? can anyone help me?

Here is my codes :

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

Upvotes: 1

Views: 426

Answers (3)

Rakesh Chouhan
Rakesh Chouhan

Reputation: 1230

You are using JDBC-ODBC Bridge driver for this code, to configure the bridge driver make sure you have properly configured the System DSN

for Windows it is

Control Panel -->Administrative tools-->Data Sources(ODBC)-->System DSN setting for your Driver.

or use thin Driver to your code,

for Thin driver please refer the following link

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

Upvotes: -1

John Woo
John Woo

Reputation: 263733

You should be using pstmt.setDouble(...);

pstmt.setDouble(18, Double.parseDouble(txtTotal.getText()));

Upvotes: 5

Subir Kumar Sao
Subir Kumar Sao

Reputation: 8401

Its says in the Error,

Disallowed implicit conversion from data type char to data type money

pTotalDue is not char. You need to set number type value.

Use BigDecimal. Using any primitive will lead to precision problems sooner or later.

Upvotes: 1

Related Questions