0

I have been trying to insert data from an app to a database, but always get a syntax error relating to a UUID number that will be the customer's ID, and primary key for the table. The column is set as BIGINT, it always returns. Here is the code:

String a = txtNombre.getText();
String b = txtApellido.getText();
String c = txtLetras.getText();
String d = txtFin.getText();
String e = txtMensual.getText();
e = e.replaceAll("[^\\d.]","");
String f = txtfecha.getText(); 
String g = txtdeuda.getText();
g = g.replaceAll("[^\\d.]","");
int c1 = Integer.parseInt(c);
long result = UUID.nameUUIDFromBytes((a+b).getBytes()).getMostSignificantBits();
String x = "" + result + "";
String sbStr = x.substring(0, 6);
sbStr = sbStr.replaceAll("[^\\d.]","");
long csId = Long.parseLong(sbStr);
JOptionPane.showInternalMessageDialog(dskPane, "Cliente Ingresado con id#" + csId, "Cliente a Base de"
            + "Datos", 1, null);
connectMyDB con = new connectMyDB();
Statement state = null;

try {
    Connection charlie = con.connect();
    state = charlie.createStatement();
    String insert = "insert into APP.CLIENTES("+csId+",'"+a+"',"
                     + "'"+b+"',"+c1+","+d+","+e+",'"+f+"',"+g+")";
    state.executeUpdate(insert);
    System.out.println(insert);
    if(state!=null) { 
        state.close();
        charlie.close();
    }
} catch(SQLException ex) { 
    System.out.print(ex);
}

I always get sql.SQLSyntaxErrorException: Syntax error: Encountered "61297" at line 1, column 26. Is there a way to avoid that?

skomisa
  • 16,436
  • 7
  • 61
  • 102
Nemesis
  • 3
  • 3
  • Can you please do a `System.out.println( insert );` just after you initialize `insert` and post the output here? – mypetlion Mar 06 '19 at 19:50
  • I made the the System.out.print , the outcome is/////insert into APP.CLIENTES(406430,'AName ',' ALastaname',60, 5,1342.09,' 2019-10-10',80525.50) – Nemesis Mar 06 '19 at 20:39
  • And then did you get "sql.SQLSyntaxErrorException: Syntax error: Encountered "406430" at line 1, column 26"? – Meg Mar 06 '19 at 21:42
  • Always [search Stack Overflow](https://duckduckgo.com/?q=site%3Astackoverflow.com+JDBC+UUID&t=osx&ia=web) thoroughly before posting. Using UUID data type with JDBC has been covered many times already. – Basil Bourque Mar 07 '19 at 04:28
  • Generally better to be using a prepared statement with placeholders that manipulating strings into SQL. – Basil Bourque Mar 07 '19 at 04:30

2 Answers2

1

One of the correct way for insert SQL command is: insert into tables values (val1, val2, ...)

Also you have to use PreparedStatement to prevent SQL injections.

Alex
  • 11,451
  • 6
  • 37
  • 52
0

It is likely that the problem is that your csId, as a Long, is not represented internally in a way that makes it compatible with sql's bigInt. Even though they both stand for 'a really large integer', they are not the same thing 'under the hood'.

The usual solution is to keep the uuid as a string in your javascript, and it should be able to be inserted into a bigint field without issues as long as it contains only digits and the size is correct.

Meg
  • 938
  • 9
  • 20