gpt4 book ai didi

java - 对我的代码的批评/建议

转载 作者:行者123 更新时间:2023-11-30 06:36:42 26 4
gpt4 key购买 nike

<分区>

在我继续之前,最好知道我的程序到目前为止是否存在任何重大设计缺陷。在我继续之前有什么值得改变的吗?

模型

package model;

import java.sql.*;
import java.util.*;

public class MovieDatabase {
@SuppressWarnings({ "rawtypes", "unchecked" })
public List queryMovies() throws SQLException {
Connection connection = null;
java.sql.Statement statement = null;
ResultSet rs = null;
List results = new ArrayList();

try {
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password");
statement = connection.createStatement();
String query = "SELECT * FROM movie";
rs = statement.executeQuery(query);

while(rs.next()) {
MovieBean bean = new MovieBean();
bean.setMovieId(rs.getInt(1));
bean.setTitle(rs.getString(2));
bean.setYear(rs.getInt(3));
bean.setRating(rs.getInt(4));
results.add(bean);
}
} catch(SQLException e) {

}
return results;
}
}

小服务程序

public class Service extends HttpServlet {

@SuppressWarnings("rawtypes")
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("Movies!");
MovieDatabase movies = new MovieDatabase();

try {
List results = movies.queryMovies();
Iterator it = results.iterator();

while(it.hasNext()) {
MovieBean movie = new MovieBean();
movie = (MovieBean)it.next();
out.println(movie.getYear());
}
}
catch(SQLException e) {

}

}
}

bean

package model;

@SuppressWarnings("serial")
public class MovieBean implements java.io.Serializable {

protected int movieid;
protected int rating;
protected int year;
protected String title;

public MovieBean() {

}

public void setMovieId(int movieidVal) {
movieid = movieidVal;
}

public void setRating(int ratingVal) {
rating = ratingVal;
}

public void setYear(int yearVal) {
year = yearVal;
}

public void setTitle(String titleVal) {
title = titleVal;
}

public int getMovieId() {
return movieid;
}

public int getRating() {
return rating;
}

public int getYear() {
return year;
}

public String getTitle() {
return title;
}

}

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com