找了半天也沒找到,究竟哪里出錯了。
package?dao;
import?java.sql.Connection;
import?java.sql.Date;
import?java.sql.PreparedStatement;
import?java.sql.ResultSet;
import?java.sql.SQLException;
import?java.sql.Statement;
import?java.util.ArrayList;
import?java.util.List;
import?com.imooc.DBUtil;
import?model.Goddess;
public?class?GoddessDao?{
public?void?addGoddess(Goddess?g)?throws?Exception{
Connection?conn=DBUtil.getConnection();
String?sql=""?+
"insert?into?imooc_goddess"?+
"(user_name,sex,age,birthday,email,mobile,"?+
"create_user,create_date,update_user,update_date,isdel)"?+
"values("?+
"?,?,?,?,?,?,?,current_date(),?,current_date(),?)";
PreparedStatement?ptmt=conn.prepareStatement(sql);
ptmt.setString(1,?g.getUser_name());
ptmt.setInt(2,?g.getSex());
ptmt.setInt(3,?g.getAge());
ptmt.setDate(4,?new?Date(g.getBirthday().getTime()));
ptmt.setString(5,?g.getEmail());
ptmt.setString(6,?g.getMobile());
ptmt.setString(7,?g.getCreate_user());
ptmt.setString(8,?g.getUpdate_user());
ptmt.setInt(9,?g.getIsdel());
ptmt.execute();
}
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;
}
}
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?email;
private String?mobile;
private String?create_user;
private String?update_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?getEmail()?{
return?email;
}
public?void?setEmail(String?email)?{
this.email?=?email;
}
public?String?getMobile()?{
return?mobile;
}
public?void?setMobile(String?mobile)?{
this.mobile?=?mobile;
}
public?String?getCreate_user()?{
return?create_user;
}
public?void?setCreate_user(String?create_user)?{
this.create_user?=?create_user;
}
public?String?getUpdate_user()?{
return?update_user;
}
public?void?setUpdate_user(String?update_user)?{
this.update_user?=?update_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?com.imooc.action;
import?java.util.Date;
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();
Goddess?gl=new?Goddess();
gl.setUser_name("小夏");
gl.setAge(22);
gl.setSex(1);
gl.setBirthday(new?Date());
gl.setEmail("xiaoxia@imooc.com");
gl.setCreate_user("ADMIN");
gl.setUpdate_user("ADMIN");
gl.setId(1);
g.addGoddess(gl);
// List<Goddess>?gs=g.query();
// for(Goddess?goddess:gs){
// System.out.println(goddess.getUser_name()+","+goddess.getAge());
// }
}
}
package?com.imooc;
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:///imooc";
private?static?final?String?USER="root";
private?static?final?String?PASSWORD="wo123wo";
private?static?Connection?conn=null;
static{
try?{
Class.forName("com.mysql.jdbc.Driver");
Connection?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?{
Class.forName("com.mysql.jdbc.Driver");
Connection?conn=DriverManager.getConnection(URL,?USER,?PASSWORD);
Statement?stmt=conn.createStatement();
ResultSet?rs=stmt.executeQuery("select?user_name,age?from?imooc_goddess");
while(rs.next()){
System.out.print(rs.getString("user_name")+","+rs.getInt("age"));
}
}
}
2017-11-06
在action里加? update_date并賦值? ?你的新建Date沒有賦值所以顯示空指針異常
2017-11-06
2017-02-08
DBUtil類中第17行“Connection?conn=DriverManager.getConnection(URL,?USER,?PASSWORD);”,去掉“Connection”,直接給你之前定義的靜態(tài)變量conn賦值