老师好,如下4段代码,数据表用的课程中的employee,我添加员工信息为什么一直报错
package com.imooc.jdbc.sample; import com.imooc.jdbc.sample.command.Command; import com.imooc.jdbc.sample.command.InsertCommand; import com.imooc.jdbc.sample.command.PstmtQueryCommand; import com.imooc.jdbc.sample.command.QueryCommand; import java.util.Scanner; //按照部门查询员工信息 public class HumanResourceApplication { public static void main(String[] args) { System.out.println("1-查询部门员工"); System.out.println("2-办理员工入职"); System.out.println("3-调整薪资"); System.out.println("4-员工离职"); System.out.println("5-分页查询员工数据"); System.out.println("请选择功能:"); System.out.println("请输入要查询的选项"); Scanner in= new Scanner(System.in); Integer num=in.nextInt(); Command command=null; switch (num){ case 1:// command=new PstmtQueryCommand() ; command.execude(); break; case 2: command=new InsertCommand(); command.execude(); break; } } } package com.imooc.jdbc.sample.command; public interface Command { public void execude(); } package com.imooc.jdbc.sample.command; import com.imooc.jdbc.common.DBUtils; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.Scanner; public class InsertCommand implements Command{ public void execude(){ Scanner in=new Scanner(System.in); System.out.println("请输入员工编号");int eno =in.nextInt(); System.out.println("请输入员工姓名");String ename=in.next(); System.out.println("请输入员工工资");float salary=in.nextFloat(); System.out.println("请输入员工部门");String dname=in.next(); //System.out.println("请输入员工入职日期");String hirdate=in.next(); Connection conn=null; PreparedStatement pstmt=null; try { conn =DBUtils.getConnection(); //String sql="insert into employee(eno,ename,salary,dname,hirdate)valus(?,?,?,?,?) "; String sql="insert into employee(eno,ename,salary,dname) valus (?,?,?,?)"; pstmt=conn.prepareStatement(sql); pstmt.setInt(1,eno); pstmt.setString(2,ename); pstmt.setFloat(3,salary); //No value specified for parameter 数据类型不匹配导致报错/valus(?,?,?,?)?数量错误 pstmt.setString(4,dname); //pstmt.setString(5,hirdate); //int num1=pstmt.executeUpdate();//返回值为修改数量 pstmt.executeUpdate();//返回值为修改数量 System.out.println("添加完成了"); //System.out.println(num1); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException throwables) { throwables.printStackTrace(); }finally { DBUtils.closeConnectin(null,pstmt,conn); } } } package com.imooc.jdbc.common; import javax.xml.transform.Result; import java.sql.*; public class DBUtils { /** * 创建新的数据库连接 * @return 新的Connection对象 * @throws SQLException * @throws ClassNotFoundException */ public static Connection getConnection() throws ClassNotFoundException, SQLException { Class.forName("com.mysql.cj.jdbc.Driver"); Connection conn = DriverManager.getConnection( /*"jdbc:mysql://localhost:3306/imooc?useSSL=true&useUnicode=false&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&allowPublicRetrieval=true",*/ "jdbc:mysql://localhost:3306/imooc?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true", "root", "123456"); return conn; } /** * 关闭连接,释放资源 * @param rs 结果集对象 * @param stmt Statement对象 * @param conn Connection对象 */ public static void closeConnectin(ResultSet rs, Statement pstmt, Connection conn){ try { if (rs !=null){ rs.close(); } if (pstmt !=null){ pstmt.close(); } if (conn !=null && !conn.isClosed()){ conn.close(); } } catch (SQLException throwables) { throwables.printStackTrace(); } } }
17
收起
正在回答 回答被采纳积分+1
1回答
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星