at com.imooc.dao.GoddessDao.addGoddess(GoddessDao.java:25) at com.imooc.action.GoddessAction.main(GoddessAction.java:28)

来源:2-4 JDBC实战---搭建模型层 Ⅱ

慕粉15797834603

2018-05-08 15:37

GodessAction.java文件

package com.imooc.action;


import java.sql.Date;

import java.sql.SQLException;

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();

Goddess g1=new Goddess();

g1.setUser_name("小夏");

g1.setAge(22);

g1.setSex(1);

g1.setBirthday(new java.util.Date());

g1.setEmail("xiaoxian@imooc.com");

g1.setMobile("18766888866");

g1.setCreate_user("Admin");

g1.setUpdate_user("Admin");

g1.setIsdel(1);

g.addGoddess(g1);

}

}

GoddessDao.java文件

package com.imooc.dao;


import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.ArrayList;

import java.util.Date;

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)"+

"valurs(" +

"?,?,?,?,?,?,?,?,?,?,?)";

PreparedStatement ptmt=conn.prepareStatement(sql);

ptmt.setString(1, g.getUser_name());

ptmt.setInt(2, g.getSex());

ptmt.setInt(3, g.getAge());

    ptmt.setDate(4, (java.sql.Date) 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();

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;

}

}


写回答 关注

1回答

  • sky_i
    2018-05-09 12:31:56

    你的DAO文件里,String sql语句的中,有11个占位符:“?”。但是后续的ptmt只赋予了9个值。


    解决方法:将第8、10个“?” 替换为:

    current_date()


JDBC之 “ 对岸的女孩看过来”

一起领略JDBC的奥秘,为进一步学习集成框架打下良好的基础

99327 学习 · 856 问题

查看课程

相似问题