-2

我已经编写了 get 方法,但我想编写 set 方法,但它给了我错误提示 - PreparedStatement 类型中的方法 setString(int, String) 不适用于参数(String,String,String,String,字符串,)我的代码在 com.glomindz.mercuri.dao 包下面给出;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

 import com.glomindz.mercuri.pojo.User;
 import com.glomindz.mercuri.util.MySingleTon;

 public class UserServicesDAO {

private Connection connection;

public UserServicesDAO() {
    // connection = new MySingleTon().getConnection();
    connection = MySingleTon.getInstance().getConnection();

}

public List<User> get_all_data() {
    List<User> usersList = new ArrayList<User>();
    try {
        PreparedStatement stmt = connection.prepareStatement("INSERT INTO              spl_user_master(name,email,mobile,password,role,status,last_update)VALUES(?,?,?,?,?,?,?)");
         stmt.setString("Charlie Sheen","help@glomindz.com","9654087107","cbf51a71a11d7ec348b0d7e9b2f0055f","admin","3","null");

        boolean execute = stmt.execute();
        System.out.println(execute);
        ResultSet resultSet = stmt.getResultSet();
        System.out.println(resultSet.getMetaData());
        while (resultSet.next()) {
            User user = new User();
            user.setId(resultSet.getInt("id"));
            user.setName(resultSet.getString("name"));
            user.setEmail(resultSet.getString("email"));
            user.setMobile(resultSet.getString("mobile"));
            user.setPassword(resultSet.getString("password"));
            user.setRole(resultSet.getString("role"));
            user.setStatus(resultSet.getString("status"));
            user.setLast_udpate(resultSet.getString("last_update"));
            usersList.add(user);
        }
    } 

    catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return usersList;

}

public static void main(String[] args) {
    UserServicesDAO userdao = new UserServicesDAO();
    List<User> data = userdao.get_all_data();
    System.out.println(data);
    System.exit(0);
}

我的用户类是包 com.glomindz.mercuri.pojo;

     public class User {

private int  id;

     private String  Name;

private String  email;

private String  mobile;

private String  password;

private String  role;

private String  status;

private String  last_update;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
  return Name;
 }

 public void setName(String name) {
  Name = name;
 }

  public String getEmail() {
  return email;
 }

  public void setEmail(String email) {
  this.email = email;
  }

  public String getMobile() {
  return mobile;
  }

  public void setMobile(String mobile) {
  this.mobile = mobile;
  }

  public String getPassword() {
  return password;
   }

  public void setPassword(String password) {
  this.password = password;
 }

  public String getRole() {
  return role;
  }

   public void setRole(String role) {
  this.role = role;
   }

   public String getStatus() {
  return status;
 }

  public void setStatus(String status) {
      this.status = status;
  }
  public String getLast_update() {
  return last_update;
  }



  public void setLast_udpate(String last_update) {
// TODO Auto-generated method stub

   }




     @Override
   public String toString() {
   return "User [id=" + id + "Name=" + Name + ", email=" + email + ", mobile=" + mobile
          + ", password=" + password + ", role=" + role + ", status="
          + status + ", last_update=" + last_update + "]";
    }



    }
4

5 回答 5

2

PreparedStatement.setString()不是变量arity 方法;它不需要变量号。的论点。每个占位符()必须单独设置使用

stmt.setString(1, "Charlie Sheen");
stmt.setString(2, "help@glomindz.com");

等等。

使用 Spring 的JdbcTemplate

jdbcTemplate.update("INSERT INTO spl_user_master(name,email,mobile,password,role,status,last_update)VALUES(?,?,?,?,?,?,?)", new Object[] {
         "Charlie Sheen","help@glomindz.com","9654087107","cbf51a71a11d7ec348b0d7e9b2f0055f","admin","3","null"});
于 2013-07-18T06:15:47.833 回答
1
stmt.setString("Charlie Sheen","help@glomindz.com","9654087107","cbf51a71a11d7ec348b0d7e9b2f0055f","admin","3","null");

这行不通。您必须单独设置,例如:

stmt.setString(1, "Charlie Sheen);

其中 1 是变量的位置,“Charlie Sheen”是值。您需要为?PreparedStatement 中的每个占位符执行此操作。

于 2013-07-18T06:18:46.097 回答
0

正如其他人已经发布的那样,您将错误的参数传递给该setString()方法。?请记住,您必须分别为查询中定义的每个占位符设置值。

PreparedStatement stmt = connection.prepareStatement("INSERT INTO
     spl_user_master(name,email,mobile,password,role,status,last_update)
     VALUES(?,?,?,?,?,?,?)");
stmt.setString(1,"Charlie Sheen");
stmt.setString(2,"help@glomindz.com");

     ... so on ...

如果相应的列定义为,请记住setString()用于设置SQL VARCHAR or LONGVARCHAR值来代替占位符 SQL VARCHAR or LONGVARCHAR。对于其他数据类型,API 中有各种setXXX()方法。

于 2013-07-18T06:19:48.353 回答
0

这是问题所在,您不能使用 setString 一次设置所有参数。您需要使用 setString 一个一个地设置每个参数。:

stmt.setString("Charlie Sheen","help@glomindz.com","9654087107","cbf51a71a11d7ec348b0d7e9b2f0055f","admin","3","null");

从javadocs:

设置字符串

void setString(int parameterIndex, String x) 抛出 SQLException

将指定参数设置为给定的 Java 字符串值。驱动程序在将其发送到数据库时将其转换为 SQL VARCHAR 或 LONGVARCHAR 值(取决于参数的大小相对于驱动程序对 VARCHAR 值的限制)。

参数:parameterIndex - 第一个参数是1,第二个是2,... x - 参数值

抛出: SQLException - 如果 parameterIndex 不对应 SQL 语句中的参数标记;如果发生数据库访问错误或在关闭的 PreparedStatement 上调用此方法

于 2013-07-18T06:15:58.027 回答
0

Oracle 教程所示,该setString方法接受一个整数,表示参数的编号和一个字符串,表示值。因此,在您的情况下,您需要执行以下操作:

PreparedStatement stmt = connection.prepareStatement("INSERT INTO spl_user_master(name,email,mobile,password,role,status,last_update)VALUES(?,?,?,?,?,?,?)");
stmt.setString(1, "Charlie Sheen");
stmt.setString(2, "help@glomindz.com");
...
于 2013-07-18T06:16:19.753 回答