Java 数据库中的选择列表为空
我想从数据库中选择一个列表,但它返回空值 dao方法:Java 数据库中的选择列表为空,java,jakarta-ee,Java,Jakarta Ee,我想从数据库中选择一个列表,但它返回空值 dao方法: public static List<Matiere> getAll() throws ClassNotFoundException, SQLException { Matiere M = null; Connection cnx = Connect.getConnection(); String req = "select* from matiere";
public static List<Matiere> getAll() throws ClassNotFoundException, SQLException {
Matiere M = null;
Connection cnx = Connect.getConnection();
String req = "select* from matiere";
PreparedStatement st = cnx.prepareStatement(req);
ResultSet rs = st.executeQuery();
rs.next();
System.out.println("cc " + M);
return (List<Matiere>) M;
}
publicstaticlist getAll()抛出ClassNotFoundException、SQLException{
M=null;
Connection cnx=Connect.getConnection();
String req=“从matiere选择*”;
编制报表st=cnx.编制报表(req);
结果集rs=st.executeQuery();
rs.next();
系统输出打印项次(“cc”+M);
返回(列表)M;
}
循环jsp
<table border=1 width=80% align=center>
<tr><th>ID</th><th>Libellé</th></tr>
<%
List<Matiere>lg=AdminDAO.getAll();
for(Matiere m:lg){ %>
<tr>
<td> <%=m.getId() %> </td>
<td><%=m.getLibelle() %> </td>
</tr>
<% } %>
</table>
伊德利贝勒
如何解决此问题问题在于您没有将结果集填充到
列表中
。为了从列表中获得结果,首先需要填充它
例如,请检查以下代码
public static List<Matiere> getAll()
{
List<Matiere> matiere = new ArrayList<>();
try
{
String sql = "SELECT * FROM matiere";
Connection con = Connect.getConnection();
PreparedStatement statement = con.prepareStatement(SQL);
ResultSet rs = statement.executeQuery(SQL);
while(rs.next()) {
Matiere m = new Matiere();
//Here add your database fields
m.setId(rs.getString("id"));
m.setLibelle(rs.getString("libelle"));
matiere.add(m);
}
}
catch(SQLException e) {
// Log any exceptions ...
}
finally() {
// Always close your JDBC resources
try {
if (statement != null) {
statement.close();
statement=null;
}
}
catch(SQLException e) {
// Couldn't close statement
}
}
return matiere;
}
公共静态列表getAll()
{
List matiere=new ArrayList();
尝试
{
String sql=“选择*来自matiere”;
Connection con=Connect.getConnection();
PreparedStatement=con.prepareStatement(SQL);
ResultSet rs=语句.executeQuery(SQL);
while(rs.next()){
Matiere m=新Matiere();
//在这里添加数据库字段
m、 setId(rs.getString(“id”);
m、 setLibelle(rs.getString(“libelle”));
添加(m);
}
}
捕获(SQLE异常){
//记录任何异常。。。
}
最后(){
//始终关闭JDBC资源
试一试{
if(语句!=null){
语句。close();
语句=null;
}
}
捕获(SQLE异常){
//无法关闭语句
}
}
返回马蒂埃;
}
问题是您没有将结果集填充到列表中。为了从列表中获得结果,首先需要填充它
例如,请检查以下代码
public static List<Matiere> getAll()
{
List<Matiere> matiere = new ArrayList<>();
try
{
String sql = "SELECT * FROM matiere";
Connection con = Connect.getConnection();
PreparedStatement statement = con.prepareStatement(SQL);
ResultSet rs = statement.executeQuery(SQL);
while(rs.next()) {
Matiere m = new Matiere();
//Here add your database fields
m.setId(rs.getString("id"));
m.setLibelle(rs.getString("libelle"));
matiere.add(m);
}
}
catch(SQLException e) {
// Log any exceptions ...
}
finally() {
// Always close your JDBC resources
try {
if (statement != null) {
statement.close();
statement=null;
}
}
catch(SQLException e) {
// Couldn't close statement
}
}
return matiere;
}
公共静态列表getAll()
{
List matiere=new ArrayList();
尝试
{
String sql=“选择*来自matiere”;
Connection con=Connect.getConnection();
PreparedStatement=con.prepareStatement(SQL);
ResultSet rs=语句.executeQuery(SQL);
while(rs.next()){
Matiere m=新Matiere();
//在这里添加数据库字段
m、 setId(rs.getString(“id”);
m、 setLibelle(rs.getString(“libelle”));
添加(m);
}
}
捕获(SQLE异常){
//记录任何异常。。。
}
最后(){
//始终关闭JDBC资源
试一试{
if(语句!=null){
语句。close();
语句=null;
}
}
捕获(SQLE异常){
//无法关闭语句
}
}
返回马蒂埃;
}
请注意,select*
不是有效的SQL语法。哪种语法@Arnaud有效?在select
和*
之间需要一个空格。从db接收值并存储到列表中的部分在哪里。即使有空格,错误仍然存在@Arnaud请注意,select*
不是有效的SQL语法。哪种语法在@Arnaud有效?在select
和*
之间需要一个空格。从db接收值并存储到列表中的部分在哪里。即使有空格,错误仍然存在@阿诺这是我想说的这是我想说的