我有两个类 - 一个类作为“电影”的bean,一个类用于处理数据库。
我遇到的问题是尝试从数据库接收数据时。正如您在我的代码中看到的,我将结果集添加到链表中,这是参数“Movies”,因此我可以轻松获取我想要的特定对象。
我遇到的问题是,当我尝试从链表中打印特定对象时,它始终是数据库的最后一个值,并且它不会打印出所有“movienames”。似乎while循环中的set方法被覆盖,直到最后一个值,这就是正在保存的内容。这很奇怪,因为在while循环中的setValue之后,我立即将它保存在数据库中。
为什么不给我数据库中的所有值以及如何解决这个问题? 谢谢!
我的豆子:
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;
}
}
我的数据库类:
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());
}
}
}
答案 0 :(得分:6)
在Movies
循环中创建while
对象:
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);
}
作为辅助节点:Movie
是一个更好的命名而不是Movies
。
此外,请使用PreparedStatment:sql = "INSERT INTO movies (id, imagepath, mediapath, moviename) VALUES (null, ?, ?,?)";
防止SQL Injection Attacks
。
最后但并非最不重要:"app"
是一个真正的弱密码: - )