0

I am Trying To Insert Finger Print Data From My Form to the DataBase but i Got the Following Exception,

com.microsoft.sqlserver.jdbc.SQLServerException: String or binary data would be truncated.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:216)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1515)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:404)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:350)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1715)

I know i have implemented the Serialization MeChanism in a Wrong Fashion , But i Don't Know How to do that ,

Here is the Code What i Did,

public void setPerson(){
     tfcNIC.setText(tfcnic1.getText()+tfcnic2.getText()+tfcnic3.getText());
     Person p = new Person();
     p.setName(tfname.getText());
     p.setFname(tfFname.getText());
     p.setAddr(tfaddr.getText());
     p.setCnic(tfcNIC.getText());
     p.setfPrint(((MainForm)getOwner()).getTemplate().serialize());   <--- My Stupid Serialization Step,

In Person Class

public void setfPrint(byte[] fp){
    fPrint = fp;          <----- Setter method for Finger Print Of Person
}

Inserting Data Into DataBase

public void setPersonStatement(String nm,String fn,String cn,String add, byte[] fpt) {
    String Sql = "INSERT INTO PERSON (NAME, FNAME, CNIC, ADDR, FPT) VALUES ( ?,?,?,?,?)";
    try {
        if(con==null){
            System.out.println("Connection error");
        }
        else {
            System.out.println("Connection ok");
        }


        pst2=con.prepareStatement(Sql);

    System.out.println(nm);
    pst2.setString(1, nm);
    pst2.setString(2, fn);
    pst2.setString(3, cn);
    pst2.setString(4, add);
    pst2.setBytes(5, fpt);

    pst2.executeUpdate();

    } catch (SQLException e) {
        // TODO Auto-generated catch block
        System.out.println("SQL Error");
        e.printStackTrace();
    }
    }

the Data Type For Finger Print in DataBase is VarBinary, of size 1024, I am new at the Serialization Concept, so If Anyone can tell Me What to Do,

i am Using Digital Persona URU 4000 b and there One touch SDk For Java API

Adnan Ahmad Khan
  • 634
  • 2
  • 7
  • 25
  • 2
    I don't think this has anything to do with serialization. The error coming back from the database is quite clear. One of the values you are trying to insert is bigger than the size of its corresponding column. – Amir Keibi Jan 29 '14 at 06:07
  • All Other Fields Have Constraints in GUI, That User Can Not Enter data greater then the Size Specified, the Only Dynamic is the byte[], and I have setted VarBinary of size 1024 for that, is that not enough one for that? – Adnan Ahmad Khan Jan 29 '14 at 06:28
  • This is the System.out.println(fpt); output to cmd [B@98adae2 – Adnan Ahmad Khan Jan 29 '14 at 06:30
  • And When I Commented out that Finger Print Data from InsertQuery, Everything Worked Fine – Adnan Ahmad Khan Jan 29 '14 at 06:43
  • 2
    The "println" calls "toString" on the fpt array which inherit its implementation from Object. Instead try "Arrays.toString(fpt)" to print the actual value of the byte array. Or better yet debug the code and find out what the real size of fpt is. – Amir Keibi Jan 29 '14 at 07:06
  • Increased the Size to 2000 and everything worked Fine, i Can Now Enroll Fingers in Database. – Adnan Ahmad Khan Jan 29 '14 at 08:04

1 Answers1

0

As Amir Keibi Said, That was not the problem with the Serialization,The Actual Problem was with the Size of FPT Column in DataBase, I increased that From 1024 to 2000 and Everything Worked Fine. Now I Can Save Finger Prints In DataBase.

Adnan Ahmad Khan
  • 634
  • 2
  • 7
  • 25