Home > Database > Mysql Tutorial > body text

使用DBUtils连接Sqlserver插入失败的问题

WBOY
Release: 2016-06-07 15:46:00
Original
1184 people have browsed it

欢迎进入Windows社区论坛,与300万技术人员互动交流 >>进入 使用DBUtils连接Sqlserver插入失败的问题 帅宏军 一、问题描述: 使用DBUtils对数据库Sqlserver进行插入操作时,失败,提示参数"?"不可识别。代码如下 [java] view plaincopyprint? public void in

欢迎进入Windows社区论坛,与300万技术人员互动交流 >>进入

  使用DBUtils连接Sqlserver插入失败的问题

  帅宏军

  一、问题描述:

  使用DBUtils对数据库Sqlserver进行插入操作时,失败,提示参数"?"不可识别。代码如下

  [java] view plaincopyprint?

  public void insert(Customer customer) {

  String sql = "insert into customer values(?,?,?,?,?,?,?,?,?)";

  Object[] args = { CustomerUtils.getID(), customer.getName(),

  customer.getGender(), customer.getBirthday(),

  customer.getCellphone(), customer.getEmail(),

  customer.getPreference(), customer.getType(),

  customer.getDescription() };

  // 在执行这段代码时就出错了

  QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());

  try {

  queryRunner.update(sql, args);

  } catch (SQLException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  }

  public void insert(Customer customer) {

  String sql = "insert into customer values(?,?,?,?,?,?,?,?,?)";

  Object[] args = { CustomerUtils.getID(), customer.getName(),

  customer.getGender(), customer.getBirthday(),

  customer.getCellphone(), customer.getEmail(),

  customer.getPreference(), customer.getType(),

  customer.getDescription() };

  // 在执行这段代码时就出错了

  QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());

  try {

  queryRunner.update(sql, args);

  } catch (SQLException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  }

  }

  问题原因发现是sqlserver的jdbc驱动在判断占位符类型的时候有问题;具体的就是获得ParameterMetaData的时候出了问题,这个时候可以用一个knowParaType的参数避免这个问题,就是在创建 QueryRunner的时候,提供一个true参数。

  二、解决方法

  附上代码

  [java] view plaincopyprint?

  public void insert(Customer customer) {

  String sql = "insert into customer values(?,?,?,?,?,?,?,?,?)";

  Object[] args = { CustomerUtils.getID(), customer.getName(),

  customer.getGender(), customer.getBirthday(),

  customer.getCellphone(), customer.getEmail(),

  customer.getPreference(), customer.getType(),

  customer.getDescription() };

  //关键是这句加一个参数true

  QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource(),

  true);

  try {

  queryRunner.update(sql, args);

  } catch (SQLException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  }

  }

  public void insert(Customer customer) {

  String sql = "insert into customer values(?,?,?,?,?,?,?,?,?)";

  Object[] args = { CustomerUtils.getID(), customer.getName(),

  customer.getGender(), customer.getBirthday(),

  customer.getCellphone(), customer.getEmail(),

  customer.getPreference(), customer.getType(),

  customer.getDescription() };

  //关键是这句加一个参数true

  QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource(),

  true);

  try {

  queryRunner.update(sql, args);

  } catch (SQLException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  }

  }

使用DBUtils连接Sqlserver插入失败的问题

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template