句子在mysql的java [英] Sentence in mysql for java

查看:89
本文介绍了句子在mysql的java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想这错误是在sentenciaSQL,但我不知道如何解决它=

  public long guardar (PedidoDTO obj)throws SQLException {

PedidoDTO dto = new PedidoDTO();
String url =jdbc:mysql://127.0.0.1:3306 / capacitacion?user = capacitacion& password = password;

try {
Connection con = DriverManager.getConnection(url);
String sentenciaSQL =INSERT INTO capacitacion.pedido(id,fecha,total,folio)VALUES(NULL,'+ obj.getId()+,+ obj.getFecha()+,+ obj .getTotal()+,+ obj.getFolio()+');
PreparedStatement s = con.prepareStatement(sentenciaSQL);
s.executeUpdate();
for(PedidoDetalleDTO detalle:obj.getPedidoDetalle()){
dto.getId();
dto.getFecha();
dto.getTotal();
dto.getFolio();
} con.close();
} catch(SQLException e){
System.out.println(e);
}
return dto.getId();
}


解决方案

您所做的错误是,sentenciaSQL中的第一个和最后一个变量每个都有单独的,未关闭的标记(在ID和作品集上):

  VALUES(NULL,'+ obj.getId()

p>

  obj.getFolio()+'); 

由于您已经在使用预编译语句,最好的方法是将您的值作为参数添加,如下所示:

  String sentenciaSQL =INSERT INTO capacitacion.pedido(id,fecha,total,folio)VALUES(?,?,?,?); 
PreparedStatement s = con.prepareStatement(sentenciaSQL);
s.setInt(1,obj.getId());
s.setDate(2,obj.getFecha());
s.setInt (3,obj.getTotal());
s.setString(4,obj.getFolio()。toString());
s.execute();



此外,如果您不关心已更新的行数,可以使用execute()而不是executeUpdate()。任一个都很好。


I guess that the error is in sentenciaSQL, but i don't know how to fix it =

public long guardar(PedidoDTO obj) throws SQLException {

PedidoDTO dto = new PedidoDTO();
String url="jdbc:mysql://127.0.0.1:3306/capacitacion?user=capacitacion&password=password";

try {
    Connection con = DriverManager.getConnection(url);
    String sentenciaSQL = "INSERT INTO capacitacion.pedido (id, fecha, total, folio) VALUES(NULL,'" +  obj.getId() + "," +  obj.getFecha() + ","+ obj.getTotal() + "," + obj.getFolio() + "')";
        PreparedStatement s =  con.prepareStatement(sentenciaSQL);
        s.executeUpdate();
        for(PedidoDetalleDTO detalle:obj.getPedidoDetalle()){
            dto.getId();
            dto.getFecha();
            dto.getTotal();
            dto.getFolio();
        }con.close();
} catch (SQLException e) {
    System.out.println(e);
}
return dto.getId();
}

解决方案

Look closely. The error you've made is that your first and last variables in sentenciaSQL each have lone, unclosed tick marks (on ID and folio):

VALUES(NULL,'" +  obj.getId()

and...

obj.getFolio() + "')";

Since you're already using a prepared statement, the best way to go about this, is to add your values as parameters, like this:

String sentenciaSQL = "INSERT INTO capacitacion.pedido (id, fecha, total, folio) VALUES(?,?,?,?)";
PreparedStatement s =  con.prepareStatement(sentenciaSQL);
s.setInt(1,obj.getId());
s.setDate(2,obj.getFecha());
s.setInt(3,obj.getTotal());
s.setString(4,obj.getFolio().toString());
s.execute();

Note, that I have made assumptions that your Id and Total are integers. You will want to substitute with the actual data types.

Also, if you don't care about how many rows have been updated, you can use execute() instead of executeUpdate(). Either one is fine.

这篇关于句子在mysql的java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆