//增
package com.test.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import com.test.jdbc.DBConnection; import com.test.model.Student; public class StudentDao { //新增数据到数据库 public boolean add(Student stu) { //执行增删改查的sql语句 //连接数据库 Connection conn = DBConnection.getConn(); //预编译sql执行 String sql ="insert into studeny values(null,?,?,?,?,?,now())"; try { PreparedStatement ps=conn.prepareCall(sql); ps.setString(1, stu.getName()); ps.setInt(2, stu.getAge()); ps.setString(3, stu.getGender()); ps.setDouble(4, stu.getHeight()); ps.setInt(5, stu.getClassID()); //执行操作更改 return ps.executeUpdate()>0; } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } return false; } public static void main(String[] args) { Student stu = new Student(); stu.setName("张三"); stu.setAge(10); stu.setClassID(10); stu.setHeight(170); stu.setGender("男"); StudentDao s = new StudentDao(); if(s.add(stu)) { System.out.println("保存成功!"); } else System.out.println("保存失败!"); } }
标签:ps,java,报告,sql,stu,Student,import,今日 From: https://www.cnblogs.com/zhaoyueheng/p/17173159.html