Juneava
2016-11-08 21:37
package com.imooc.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.db.DBUtil;
import com.imooc.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(),?)";// 用预编译符占位,后面两个日期直接用sql中的日期函数
PreparedStatement ptmt = conn.prepareStatement(sql);// 预编译sql语句
// 给预编译符赋值
ptmt.setString(1, g.getUser_name());// 把数据集成的传递过来,不要写死,方便前台操作
ptmt.setInt(2, g.getSex());
ptmt.setInt(3, g.getAge());
// ptmt.setDate(4, g.getBirthday());
//类型不符,这里的getBirthday传回java.util.birthday类型
ptmt.setDate(4, new Date(g.getBirthday().getTime()));// 此处date为sql类型
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 Exception {
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 com.imooc.action;
import java.util.Date;
import java.util.List;
import com.imooc.dao.GoddessDao;
import com.imooc.model.Goddess;
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()); }
*/
Goddess g1=new Goddess();
g1.setUser_name("小夏");
g1.setAge(22);
g1.setSex(1);
g1.setBirthday(new Date());//此处Date为util类型
g1.setEmail("xiaoxia@imooc.com");
g1.setMobile("18766888866");
g1.setCreate_user("ADMIN");
g1.setUpdate_user("ADMIN");
g1.setIsdel(1);
g.addGoddess(g1);
}
}Exception in thread "main" com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Unknown column 'update_user' in 'field list'
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3283)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1332)
at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:882)
at com.imooc.dao.GoddessDao.addGoddess(GoddessDao.java:35)
at com.imooc.action.GoddessAction.main(GoddessAction.java:30)
好吧,暂时没看出来
搞定了,数据表里面update_user写错了。。。
JDBC之 “ 对岸的女孩看过来”
99334 学习 · 916 问题
相似问题