新手求教,空指针异常
package model;import java.util.Date;
public class Goddess {
private Integer id;
private String user_name;
private Integer sex;
private Integer age;
private Date birthday;
private String mobile;
private String update_user;
private String create_user;
private Date create_date;
private Date update_date;
private Integer isdel;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getUpdate_user() {
return update_user;
}
public void setUpdate_user(String update_user) {
this.update_user = update_user;
}
public String getCreate_user() {
return create_user;
}
public void setCreate_user(String create_user) {
this.create_user = create_user;
}
public Date getCreate_date() {
return create_date;
}
public void setCreate_date(Date create_date) {
this.create_date = create_date;
}
public Date getUpdate_date() {
return update_date;
}
public void setUpdate_date(Date update_date) {
this.update_date = update_date;
}
public Integer getIsdel() {
return isdel;
}
public void setIsdel(Integer isdel) {
this.isdel = isdel;
}
}
package db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DButil {
private static final String URL="jdbc:mysql://localhost:3306/imooc?useUnicode=true&characterEncoding=gbk";
private static final String USER="root";
private static final String PASSWORD= "940616";
private static Connection conn=null;
static {
// 1.加载驱动
try {
Class.forName("com.mysql.jdbc.Driver");
//获得数据库的连接(创建对象)
conn=DriverManager.getConnection(URL, USER, PASSWORD);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getConnection(){
return conn;
}
public static void main(String[] args) throws Exception {
// 1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
//获得数据库的连接(创建对象)
conn=DriverManager.getConnection(URL, USER, PASSWORD);
//3.通过数据库的连接操作,实现增删改查
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select user_name,age from imooc_goddess");
System.out.println(rs.getString("user_name")+","+rs.getInt("age"));
}
}
package action;
import java.util.List;
import model.Goddess;
import dao.GoddessDao;
public class GoddessAction {
public static void main(String[] args) throws Exception {
GoddessDao g=new GoddessDao();
List<Goddess> gs=g.query();
for(Goddess goddess :gs){
System.out.println(goddess.getUser_name()+","+goddess.getAge());
}
}
}
package dao;
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 db.DButil;
import model.Goddess;
public class GoddessDao {
public void addGoddess() {
DButil.getConnection();
}
public void updateGoddess() {
}
public void delGoddess() {
}
public List<Goddess> query() throws SQLException {
Connection conn = DButil.getConnection();
Statement stmt=conn.createStatement();
ResultSet rs = stmt.executeQuery("select user_name,age from imooc_goddess");
List<Goddess> gs=new ArrayList<Goddess>();
Goddess g = null;
while (rs.next()) {
g = new Goddess();
g.setUser_name(rs.getString("user_name"));
g.setAge(rs.getInt("age"));
gs.add(g);
}
return gs;
}
public Goddess get() {
return null;
}
}