list 方法调用问题
本人是新手:写了以下代码,但是老是编译不通过。本人用的是jdk1.7的
代码如下:
package com.amaker.test;
import java.sql.Connection;
import java.sql.ResultSet;
import
java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.amaker.util.DbUtil;
public class ResultSetTest {
public static void main(String[] args){
List list = new listStu(); //
问题就是无法识别listStu()
//List<Object> Mylist = new listStu();
System.out.println(list);
}
//查询数据list做成
static List<Stu> listStu() {
DbUtil util = new DbUtil();
Connection conn = util.openConnection();
try {
Statement stmt = conn.createStatement();
String sql = "select id,name,age from StuTbl ";
ResultSet rs = stmt.executeQuery(sql);
//List Mylist = new ArrayList();
List<Stu> Mylist = new ArrayList<Stu>();
while (rs.next()){
int id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
//用定义的list 赋值
Stu s = new Stu();
s.setId(id);
s.setName(name);
s.setAge(age);
Mylist.add(s);
}
return Mylist;
} catch (
SQLException e) {
e.printStackTrace();
} finally {
util.closeConnection(conn);
}
return null;
}
static void testResultSet(){
DbUtil util = new DbUtil();
Connection conn = util.openConnection();
try {
Statement stmt = conn.createStatement();
String sql = "select id,name,age from StuTbl ";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()){
int id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
util.closeConnection(conn);
}
}
}
class Stu {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
------解决方案--------------------List list = new listStu(); //问题就是无法识别listStu()
改成
List list = listStu(); //问题就是无法识别listStu()
------解决方案--------------------List<Stu> list =ResultSetTest.listStu();