问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

ACCESS数据库dao调用格式

发布网友 发布时间:2022-04-08 21:18

我来回答

2个回答

懂视网 时间:2022-04-09 01:40

dao; import model.C2S; import utils.Dbtuil; import java.sql.*; import java.util.ArrayList; import java.util.List; /** * Created by yinus * Date:2016/4/6 * Time:18:44 */ public class C2SDao { public void add(C2S cs) throws SQLException { Connection connection = Dbtuil.getConnection(); String sql = "INSERT INTO db_c2s (stu_id, cou_id, c2s_credit, c2s_create_time, c2s_update_time)" + " VALUES (?,?,?,?,?)"; PreparedStatement ps = connection.prepareStatement(sql); ps.setInt(1,cs.getStu_id()); ps.setInt(2,cs.getCou_id()); ps.setDouble(3,cs.getCredit()); ps.setDate(4,new Date(cs.getCreate_time().getTime())); ps.setDate(5,new Date(cs.getUpdate_time().getTime())); ps.execute(); ps.close(); } public void update(C2S cs) throws SQLException { Connection connection = Dbtuil.getConnection(); String sql = "UPDATE db_c2s" + " SET c2s_credit=? , c2s_update_time=GETDATE()" + " WHERE stu_id= ? AND cou_id= ?"; PreparedStatement ps = connection.prepareStatement(sql); ps.setDouble(1,cs.getCredit()); ps.setInt(2,cs.getStu_id()); ps.setInt(3,cs.getCou_id()); ps.execute(); ps.close(); } public List<C2S> queryById(int id,boolean isStudentId) throws SQLException { List<C2S> css = new ArrayList<>(); C2S c = null; String sql=null; Connection connection = Dbtuil.getConnection(); if (isStudentId) { sql = "SELECT * FROM db_c2s" + " WHERE stu_id = ?"; } else { sql = "SELECT * FROM db_c2s" + " WHERE cou_id = ?"; } PreparedStatement ps = connection.prepareStatement(sql); ps.setInt(1,id); ResultSet rs = ps.executeQuery(); while (rs.next()) { c = new C2S(rs.getInt("stu_id"),rs.getInt("cou_id"),rs.getDouble("c2s_credit")); c.setCreate_time(rs.getDate("c2s_create_time")); c.setUpdate_time(rs.getDate("c2s_update_time")); css.add(c); } rs.close(); ps.close(); return css; } public C2S queryByStudentIdAndCourseId(int stu_id,int cou_id) throws SQLException { Connection connection = Dbtuil.getConnection(); String sql = "SELECT * FROM db_c2s WHERE stu_id=? AND cou_id=?"; PreparedStatement ps = connection.prepareStatement(sql); ps.setInt(1,stu_id); ps.setInt(2,cou_id); ResultSet rs = ps.executeQuery(); C2S cs = null; while (rs.next()){ cs = new C2S(); cs.setStu_id(rs.getInt("stu_id")); cs.setCou_id(rs.getInt("cou_id")); cs.setCredit(rs.getDouble("c2s_credit")); cs.setCreate_time(rs.getDate("c2s_create_time")); cs.setUpdate_time(rs.getDate("c2s_update_time")); } return cs; } public void delete(int id,boolean isStudentId) throws SQLException { Connection connection = Dbtuil.getConnection(); String sql=null; if (isStudentId) { sql = "DELETE FROM db_c2s WHERE stu_id= ?"; } else { sql = "DELETE FROM db_c2s WHERE cou_id =?"; } PreparedStatement ps = connection.prepareStatement(sql); ps.setInt(1,id); ps.execute(); ps.close(); } public void delete(int sId,int cId) throws SQLException { Connection connection = Dbtuil.getConnection(); String sql="DELETE FROM db_c2s WHERE stu_id=? AND cou_id=?"; PreparedStatement ps = connection.prepareStatement(sql); ps.setInt(1,sId); ps.setInt(2,cId); ps.execute(); ps.close(); } }
package dao;

import utils.Dbtuil;
import model.Course;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by yinus
 * Date:2016/4/6
 * Time:18:44
 */

public class CourseDao {
 public void addCourse(Course course) throws SQLException {
 Connection connection= Dbtuil.getConnection();
 String sql = "INSERT INTO db_courses" +
  " (cou_id, cou_title, cou_create_time, cou_update_time)" +
  " VALUES (?,?,?,?)";
 PreparedStatement ps = connection.prepareStatement(sql);
 ps.setInt(1,course.getId());
 ps.setString(2,course.getTitle());
 ps.setDate(3,new Date(course.getCreateTime().getTime()));
 ps.setDate(4,new Date(course.getUpdateTime().getTime()));
 ps.execute();
 ps.close();
 }

 public void delCourse(int id) throws SQLException {
 Connection connection=Dbtuil.getConnection();
 String sql = " DELETE FROM db_courses" +
  " WHERE cou_id=?" ;
 PreparedStatement ps = connection.prepareStatement(sql);
 ps.setInt(1,id);
 ps.execute();
 ps.close();
 }

 public void updateCourse(Course course) throws SQLException {
 Connection connection= Dbtuil.getConnection();
 String sql = "UPDATE db_courses" +
  " SET cou_title=?, cou_update_time = GETDATE()" +
  " WHERE cou_id=?";
 PreparedStatement ps = connection.prepareStatement(sql);
 ps.setString(1,course.getTitle());
 ps.setInt(2,course.getId());
 ps.execute();
 ps.close();
 }

 public List<Course> queryCourses(String title) throws SQLException {
 Course course =null;
 List<Course> courses = new ArrayList<>();
 Connection connection = Dbtuil.getConnection();
 String sql = " SELECT * FROM db_courses" +
  " WHERE courses.cou_title LIKE ?" ;
 PreparedStatement ps = connection.prepareStatement(sql);
 ps.setString(1,"%"+title+"%");
 ResultSet resultSet=ps.executeQuery();
 while (resultSet.next()){
  course = new Course();
  course.setId(resultSet.getInt("cou_id"));
  course.setTitle(resultSet.getString("cou_title"));
  course.setCreateTime(resultSet.getDate("cou_create_time"));
  course.setUpdateTime(resultSet.getDate("cou_update_time"));
  courses.add(course);
 }
 resultSet.close();
 ps.close();
 return courses;
 }

 public Course querySingleCourse(int id) throws SQLException {
 Course course = null;
 Connection connection=Dbtuil.getConnection();
 String sql = " SELECT * FROM db_courses" +
  " WHERE cou_id = ?" ;
 PreparedStatement ps = connection.prepareStatement(sql);
 ps.setInt(1,id);
 ResultSet rs=ps.executeQuery();
 while (rs.next())
 {
  course=new Course();
  course.setId(rs.getInt("cou_id"));
  course.setTitle(rs.getString("cou_title"));
  course.setCreateTime(rs.getDate("cou_create_time"));
  course.setUpdateTime(rs.getDate("cou_update_time"));
 }
 rs.close();
 ps.close();
 return course;
 }
}
package dao;

import utils.Dbtuil;
import model.Student;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by yinus
 * Date:2016/4/6
 * Time:18:44
 */

public class StudentDao {
 public void addStudent(Student student) throws SQLException {
 Connection connection=Dbtuil.getConnection();
 String sql = "insert into db_students (stu_id,stu_name,stu_create_time,stu_update_time) values (?,?,?,?)";
 PreparedStatement ps = connection.prepareStatement(sql);
 ps.setInt(1,student.getId());
 ps.setString(2,student.getName());
 ps.setDate(3,new Date(student.getCreateTime().getTime()));
 ps.setDate(4,new Date(student.getUpdateTime().getTime()));
 ps.execute();
 ps.close();
 }
 public void delStudent(int id) throws SQLException {
 Connection connection=Dbtuil.getConnection();
 String sql = " DELETE FROM db_students WHERE stu_id=?" ;
 PreparedStatement ps = connection.prepareStatement(sql);
 ps.setInt(1,id);
 ps.execute();
 ps.close();
 }

 public void updateStudent(Student student) throws SQLException {
 Connection connection=Dbtuil.getConnection();
 String sql = " UPDATE db_students SET stu_name=?,stu_create_time=?,stu_update_time=GETDATE() WHERE stu_id=?";
 PreparedStatement ps = connection.prepareStatement(sql);
 ps.setString(1,student.getName());
 ps.setDate(2,new Date(student.getCreateTime().getTime()));
 ps.setInt(3,student.getId());
 ps.execute();
 ps.close();
 }

 public List<Student> queryStudents(String name) throws SQLException {
 Student student=null;
 List<Student> students = new ArrayList<>();
 Connection connection=Dbtuil.getConnection();
 String sql = " SELECT * FROM db_students WHERE stu_name LIKE ?" ;
 PreparedStatement ps = connection.prepareStatement(sql);
 ps.setString(1,"%"+name+"%");
 ResultSet resultSet=ps.executeQuery();
 while(resultSet.next()){
  student = new Student();
  student.setId(resultSet.getInt("stu_id"));
  student.setName(resultSet.getString("stu_name"));
  student.setCreateTime(resultSet.getDate("stu_create_time"));
  student.setUpdateTime(resultSet.getDate("stu_update_time"));
  students.add(student);
 }
 resultSet.close();
 ps.close();
 return students;
 }

 public Student querySingleStudent(int id) throws SQLException {
 Student student=null;
 Connection connection=Dbtuil.getConnection();
 String sql = " SELECT * FROM db_students WHERE stu_id=?" ;
 PreparedStatement ps = connection.prepareStatement(sql);
 ps.setInt(1,id);
 ResultSet resultSet=ps.executeQuery();
 while (resultSet.next()){
  student=new Student();
  student.setId(resultSet.getInt("stu_id"));
  student.setName(resultSet.getString("stu_name"));
  student.setCreateTime(resultSet.getDate("stu_create_time"));
  student.setUpdateTime(resultSet.getDate("stu_update_time"));
 }
 resultSet.close();
 ps.close();
 return student;
 }
}

 

第二次数据库作业--dao

标签:

热心网友 时间:2022-04-08 22:48

DAO(Data Access Object) 数据访问对象是第一个面向对象的数据库接口,它显露了 Microsoft Jet 数据库引擎(由 Microsoft Access 所使用),并允许 Visual Basic 开发者通过 ODBC 像直接连接到其他数据库一样,直接连接到 Access 表。DAO 最适用于单系统应用程序或小范围本地分布使用。
详细介绍请参考百度百科:

http://ke.baidu.com/link?url=v8oOxJybAXAVWuLccsosHn1t7h1mv*Dfe0vHQEj8DGWM_ikRjwxWqs4mvSeaD_gs4P_EMj-U4tEkRUdjhfNK
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
芭比娃娃是不是有生命? 寒窑赋翻译 " 四大千古奇文 "之《寒窑赋》原文及译文,值得一看! 趁山姆卡还没过期,秋天可以买什么美食? win7任务栏怎么恢复到默认 win7任务栏恢复到默认设置 如何将任务栏还原到电脑底部教你简单操作让任务栏回归原位 任务栏怎样才能恢复原样? 2024年劳力士比赛时间表 猜谜语,二马来盘河,打0到9数字,求解 设f(x)=|x-1|(x+1)-x,若关于x的方程f(x)=k有三个不同的实数解,则实数k... 请问我的体重超过标准体重的19%,属于肥胖的哪个级别? 男生发一个b是什么意思? 肥胖者通常胆固醇超标,哪位能告诉一下不同身高的人超重和肥胖的标准? 怎样用发育等级评价法评价五岁男孩身高体重发育状况 肥胖的原因,肥胖的危害,吃什么容易长胖 智者不入爱河 怎么反驳这句话? 学龄儿童11~15岁标准体重的估算方法是:年龄×3-2.(单位:千克)《学生体质健康标准》体重等级评价表 儿童各年龄阶段体重估计标准是:2至10岁:体重=年龄×2+8; 11至15岁:体重=年龄×3-2.实际体重与标 体重计算公式和体重等级评价标准表如下,看李东和王雷同学的对话请按要求解答问题. 学龄儿童各年龄标 求一份变频器控制柜原理图布置图接线图? 谁有变频器的原理图, 还有变频模块的型号及资料,有的就不要藏着,谢谢各位了! 有没有人知道笔记本电脑放音乐电影都没有声音是怎么回事,求大神 变频柜两用一备电路图 恒压供水控制柜原理图恒压供水控制器和变频器原理接线图是怎么样? mysql数据库应用从入门到精通 为什么只有118页 中国银行个人经营贷款的适用对象有要求吗? 这个u盘读取器为什么插iphone上没反应.是要下载什么软件么 我就是想在手机上看u盘的文件而已 这个u盘读取器为什么插u盘后插iphone上没有反应.是要下载什么软件么 我就是想用手机看u盘内容 在windows10中如果要删除桌面上的图标 读卡器下载和u盘下载的程序有什么不问? -_-b 这个网络用语是什么意思? 包的牌子两个F ,第二个是倒着的F 之前蚂蚁包卡里剩的流量现在不能提取了吗?我看了好像没有提取流量这一项功能了。 通过支付宝提取的100M流量没有到账,为什么? 这个月提取支付宝送的流量总是退回,提了无数次退回无数次,是什么情况?还有口碑兑换流量也是退回, 京东流量支付宝流量提不到联通手机怎么解决? 蚂蚁宝卡换取流量停止了吗?为什么现在支付宝积分换不了了? 支付宝内累计的流量多点l提取能返还吗? 支付宝中联通增送的流量如何提取? 求Despacito原版mp3,百度云,谢谢啦 求推荐时长在一分钟到两分钟左右的中文歌曲。不要纯音乐 - 信息提示 金蝶标准版,一项固定资产设备对应折旧需要两个科目,请问怎么设置? 中国移动装宽带是收一百块猫的押金 和调试费用, 然后路由器还是要自己买吗? 业务活动费-固定资产折旧科目需要设置功能分类和经济分类吗? 直接用移动WLAN还是自己买个无线路由器信号好? 移动上门安装光线是要自己买路由器的吗 手机IMEI码,刷机可以该吗? 手机用IMEI码锁了 刷系统能解锁吗? 安卓手机刷机可以激活imei码然后手机定位吗