开发者

Arraylist / prepared statement

开发者 https://www.devze.com 2023-02-21 21:17 出处:网络
Hi Im trying to pass an arraylist to a prepared statement but am getting the follwing error. setString(int,java.lang.String) in java.sql.PreparedStatement cannot be applied to (int,java.lang.Object)

Hi Im trying to pass an arraylist to a prepared statement but am getting the follwing error.

setString(int,java.lang.String) in java.sql.PreparedStatement cannot be applied to (int,java.lang.Object) m_ps.setString(2, topics.get(1));

^ Here is the portion of the code

public ArrayList<String> passTopics(String userName, ArrayList topics){
            m_ps = null;
            String sql = null;

        try{
            sql = "INSERT INTO adviceGiverTopics"+ "(userName,topics,dateAdded) VALUES(?, ?, ?)";
            m_ps = m_conn.prepareStatement(sql);    

            m_ps.setString(1,userName);
            m_ps.setString(2, topics.get(1));           
            m_ps.setString(3, this.getDateTime());



            m_ps.ex开发者_StackOverflow社区ecute();
            m_ps.close();
        }
        catch(SQLException e){
            System.out.println(e.getMessage());
        }   

        return null;
    }


Here's how I'd write that code:

package persistence;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.List;

/**
 * B
 * @author Michael
 * @since 3/21/11
 */
public class B
{
    public static final String SQL = "INSERT INTO adviceGiverTopics" + "(userName,topics,dateAdded) VALUES(?, ?, ?)";

    private Connection connection;

    public int saveTopics(String userName, List<String> topics, Date insertDate)
    {
        if ((userName == null) || "".equals(userName.trim()))
            throw new IllegalArgumentException("user name cannot be null or blank");
        if ((topics == null) || (topics.size() < 2))
            throw new IllegalArgumentException("topics cannot be null and must have at least two elements");

        int numRowsAffected = 0;

        PreparedStatement ps = null;
        String sql = null;

        try
        {
            ps = connection.prepareStatement(sql);

            ps.setString(1, userName);
            ps.setString(2, topics.get(1));
            ps.setDate(3, new java.sql.Date(insertDate.getTime()));

            numRowsAffected = ps.executeUpdate();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        finally
        {
            close(ps);
        }

        return numRowsAffected;
    }

    private static void close(Statement st)
    {
        try
        {
            if (st != null)
            {
                st.close();
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

}


Since topics is a list of Object, you'd have to cast the Object to a String, or call toString():

m_ps.setString(2, (String) topics.get(1));        

or m_ps.setString(2, topics.get(1).toString());

An alternative would be to use generics, and declare topics as "ArrayList<String>" -- but I'm guessing that's not something you've learned about yet.

0

精彩评论

暂无评论...
验证码 换一张
取 消