当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Data is not being saved in ArrayList

问题描述:

I have two classes - one class as a bean for "Movies" and one class for handling the database.

The problem I have is when trying to receive the data from the database. As you can see in my code I am adding the resultset in a linkedlist, which is parameter "Movies" so I can easily get the specific object that I want.

The problem that I have is that when I try to print a specific object from the linkedlist, it is always the last value of the database and it is not printing out the all the "movienames". It seems like the set method in the while loop is being overwritten until the last value, and that is what is being saved. This is weird because after the setValue in the while loop, I immediately save it in the database.

Why is it not giving me all the values in the database and how can I fix this?

Thanks!

My bean:

import java.io.Serializable;

import javax.persistence.*;

/**

* Entity implementation class for Entity: Movies

*

*/

@Entity

public class Movies implements Serializable {

private static final long serialVersionUID = 1L;

public Movies() {

super();

}

@Id

@GeneratedValue(strategy = GenerationType.AUTO)

private int id;

public String getImagepath() {

return imagepath;

}

public void setImagepath(String imagepath) {

this.imagepath = imagepath;

}

private String moviename, movieinfo, mediapath, imagepath;

public String getMediapath() {

return mediapath;

}

public void setMediapath(String mediapath) {

this.mediapath = mediapath;

}

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getMoviename() {

return moviename;

}

public void setMoviename(String moviename) {

this.moviename = moviename;

}

public String getMovieinfo() {

return movieinfo;

}

public void setMovieinfo(String movieinfo) {

this.movieinfo = movieinfo;

}

}

My database class:

package JDBC;

import java.sql.*;

import java.util.ArrayList;

import com.commerce.models.Movies;

public class MovieService2 {

//

static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";

static Connection conn = null;

static Statement stmt = null;

public void insertData (String imagepath,String mediapath, String moviename) throws Exception{

conn = DriverManager.getConnection("jdbc:mysql://192.168.51.123:3306/movies","app","app");

stmt = conn.createStatement();

String sql;

sql = "INSERT INTO movies (id, imagepath, mediapath, moviename) VALUES (null, '"+imagepath+"', '"+mediapath+"','"+moviename+"')";

stmt.executeUpdate(sql);

}

public static ArrayList<Movies> getData () throws Exception{

System.out.println("Connecting to database...");

Movies m = new Movies ();

ArrayList <Movies> data = new ArrayList <Movies> ();

conn = DriverManager.getConnection("jdbc:mysql://192.168.51.123:3306/movies","app","app");

System.out.println("Creating statement...");

stmt = conn.createStatement();

String sql;

sql = "SELECT * from movies";

ResultSet rs = stmt.executeQuery(sql);

while (rs.next()){

m.setImagepath(rs.getString("imagepath"));

m.setMoviename(rs.getString("moviename"));

m.setMediapath(rs.getString("mediapath"));

data.add(m);

}

System.out.println (data.get(0).getMoviename());

return data;

}

public static void main (String [] args) throws Exception{

ArrayList <Movies> data = getData ();

for (int i = 0; i < data.size(); i++){

//System.out.println("hej: " + getData().get(0).getMoviename());

}

}

}

网友答案:

create Movies object inside the while loop:

  while (rs.next()){
        Movies m = new Movies ();
        m.setImagepath(rs.getString("imagepath"));
        m.setMoviename(rs.getString("moviename"));
        m.setMediapath(rs.getString("mediapath"));
        data.add(m);

      }

as a side node: Movie is a better naming not Movies.

Furthermore use PreparedStatment: sql = "INSERT INTO movies (id, imagepath, mediapath, moviename) VALUES (null, ?, ?,?)"; to prevent SQL Injection Attacks.

last but not least: "app" is a realy weak password :-)

分享给朋友:
您可能感兴趣的文章:
随机阅读: