gpt4 book ai didi

Java DAO 对象 SQLException

转载 作者:行者123 更新时间:2023-11-29 22:22:45 25 4
gpt4 key购买 nike

Java 和 MySQL 新手。

我使用 DAO 对象来查询表,通过 Eclipse 运行。 MySQL 通过 Workbench 编辑。表存在并出现以下异常:

SELECT movie_name, release_dd, release_mm, release_yyyy, duration, language, director, genre, actor_1, actor_2 FROM movie_details_table WHERE movie_name = 'Piku'

java.sql.SQLException: Before start of result set
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:998)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:937)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:872)
at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:787)
at com.mysql.jdbc.ResultSetImpl.getStringInternal(ResultSetImpl.java:5244)
at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5167)
at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5206)
at com.library.model.MovieDAO.getMovieDetails(MovieDAO.java:41)
at com.library.model.MovieDetTest.main(MovieDetTest.java:18)

MovieDAO 类:

package com.library.model;


import java.util.*;
import java.sql.*;
import java.io.*;
import com.library.model.beans.*;

public class MovieDAO {

private static final String DB_URL =
"jdbc:mysql://localhost/planner";

// Database credentials
private static final String USER = "Sudipto";
private static final String PASS = "sudi85";

public MovieDetails getMovieDetails(String inputMov) throws
SQLException {

MovieDetails movieDetails = new MovieDetails();

try {

//Open a connection
Connection conn = DriverManager.getConnection
(DB_URL,USER,PASS);

//Create and execute query
String queryString = "SELECT movie_name, release_dd, release_mm, release_yyyy, duration, language, director, genre, actor_1, actor_2 FROM movie_details_table WHERE movie_name = '" + inputMov + "'";

System.out.println(queryString);

PreparedStatement statement = conn.prepareStatement
(queryString);



ResultSet rsMovieDetails = statement.executeQuery();

movieDetails.setMovieName(rsMovieDetails.getString
("movie_name"));
movieDetails.setReleaseDate
(rsMovieDetails.getInt ("release_dd"), rsMovieDetails.getInt ("release_mm"), rsMovieDetails.getInt ("release_yyyy"));
movieDetails.setDuration(rsMovieDetails.getInt
("duration"));
movieDetails.setLanguage(rsMovieDetails.getString
("language"));
movieDetails.setDirector(rsMovieDetails.getString
("director"));
movieDetails.setGenre(rsMovieDetails.getString
("genre"));
movieDetails.setActor1(rsMovieDetails.getString
("actor_1"));
movieDetails.setActor2(rsMovieDetails.getString
("actor_2"));

}

catch (SQLException e) {
e.printStackTrace();
}

return movieDetails;

}

}

MySQL工作台中有以下错误日志:

2015-05-31T15:04:36, 27, Note, Aborted connection 27 to db: 'planner' user: 'Sudipto' host: 'localhost' (Got an error reading communication packets)

任何人都可以建议我如何以及需要解决什么问题吗?

最佳答案

使用 rsMovieDetails.next() 检索详细信息。就像 rs.next() 用于 https://docs.oracle.com/javase/tutorial/jdbc/basics/retrieving.html 中一样。
rs.next() 将光标移动到数据库结果集的下一行,如果有行则返回 true,否则返回 false。如果存在行,那么您应该检索数据

关于Java DAO 对象 SQLException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30559609/

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