简体   繁体   中英

I am trying to store ArrayList object in database

I am trying to store ArrayList object in database but when i set the object to PreparedStatement im getting error, i want to user entered multiple values in single column in database

my java code:

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Uploads
 */

@WebServlet("/Rough")
public class Rough extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ArrayList<String> name=  new ArrayList<>();

        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/practise", "root", "root");
            PreparedStatement ps = con.prepareStatement("insert into imgtable(imgname,imgnnn,list) values(?,?,?)");

            name.add("abc");
            name.add("def");
            ps.setString(1, "JAVA");
            ps.setString(2, "Spring");
            ps.setString(3, name);
            int z = ps.executeUpdate();
            if(z!=0) {
                System.out.println("sucess");
            }
        } catch (Exception ex) {
            System.out.println(ex);
        }
        }


}

You should convert the array to string manually in the way which is useful to you, than you will be able to save it. The problem is that JDBC does not have corresponding type to map your object automatically, so it is up to you. The other option could be to create another table and set a foreign key. It depends on the real data which way you should follow.

you could iterate through your List and add its contents in the DB as a single String, if that's what you're looking for, eg

String list = "";
for (String s : name) {
  list += s + " ";
}
ps.setString(3, list);

You can store any object in the Database by converting the object into byte[] array. Your Column type should be "BLOB" type.

Sample code snippet

...........................................
ByteArrayOutputStream baos = null;
ObjectOutputStream oout = null;
try{
    baos = new ByteArrayOutputStream();
    oout = new ObjectOutputStream(baos);
    oout.writeObject(<<ARRAYLIST_OBJECT>>);
    oout.close();
    byte[] blobdata = baos.toByteArray();
    preparedStmt.setBytes(5, blobdata);
   .........................
   preparedStmt.executeUpdate();
 }catch(Exception e){
    e.printStackTrace();
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM