版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、学号姓名实验序号实验十二实验名称数据库编程实验地点实验日期实验内容1.在数据库(xscj)里建立如下数据表tb_student:使用的数据库系统不限。2.使用Java JDBC对该数据表进行增删改查,输出到屏幕。JDBC连接方式不限。(1)从键盘输入数据并能写入数据表中(2)修改学生成绩(3)删除指定学号的数据(4)按学生姓名模糊查询实验过程及步骤1. 在SQL数据库中创建xscj数据库,建立数据表tb_student。2. 使用Java JDBC对该数据表进行增删改查,输出到屏幕。一:BaseDao:package com.util;import java.sql.Connection;im
2、port java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.Statement;public class BaseDao protected Connection conn=null; protected Statement stmt=null;protected PreparedStatement pstmt=null;protected ResultSet rs=null;public Connection getConn()Connectio
3、n aconn=null;tryClass.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");aconn=DriverManager.getConnection实验过程及步骤("jdbc:sqlserver:/localhost:1433;DatabaseName=xscj","sa","123456");catch(Exception ex)ex.printStackTrace();return aconn;public void closeAll()
4、tryif(rs!=null)rs.close();if(pstmt!=null)pstmt.close();if(stmt!=null)stmt.close();if(conn!=null)conn.close();catch(Exception ex)ex.printStackTrace();二:StudentDaopackage com.dao;import java.util.List;import com.bean.Student;public interface StudentDao public int insert(Student stu); public int delete
5、student(int stuno); public List<Student> getStudentByName(String stuname); public int modify(Student stu);三:StudentDaoImplpackage com.dao.impl;import java.util.ArrayList;import java.util.List;import com.bean.Student;import com.dao.StudentDao;import com.util.BaseDao;public class StudentDaoImpl
6、extends BaseDao implements StudentDao public int insert(Student stu) int row = 0;try conn = this.getConn();String sql = "insert into tb_student(stuno,stuname,stuscore) values(?,?,?)"实验过程及步骤pstmt = conn.prepareStatement(sql);pstmt.setInt(1, stu.getStuNo();pstmt.setString(2, stu.getStuname()
7、;pstmt.setInt(3, stu.getStuscore();row = pstmt.executeUpdate(); catch (Exception ex) ex.printStackTrace(); finally this.closeAll();return row;public int deletestudent(int stuno) int row = 0;try conn = this.getConn();String sql = "delete tb_student where stuno=? "pstmt = conn.prepareCall(sq
8、l);pstmt.setInt(1, stuno);row = pstmt.executeUpdate(); catch (Exception ex) ex.printStackTrace(); finally this.closeAll();return row; public List<Student> getStudentByName(String stuname) List<Student> stulist = new ArrayList<Student>();try conn = this.getConn();String sql = "
9、select stuno,stuname,stuscore from tb_student where stuname like ?"pstmt = conn.prepareStatement(sql);pstmt.setString(1, "%" + stuname + "%");rs = pstmt.executeQuery();while (rs.next() Student stu1 = new Student();stu1.setStuNo(rs.getInt(1);stu1.setStuname(rs.getString(2);st
10、u1.setStuscore(rs.getInt(3);stulist.add(stu1); catch (Exception ex) ex.printStackTrace(); finally 实验过程及步骤this.closeAll();return stulist;public int modify(Student stu) int row = 0;try conn = this.getConn();String sql = "update tb_student set stuno=?,stuname=?,stuscore=? where stuno=?"pstmt
11、= conn.prepareStatement(sql);pstmt.setInt(1, stu.getStuNo();pstmt.setString(2, stu.getStuname();pstmt.setInt(3, stu.getStuscore();pstmt.setInt(4, stu.getStuNo();row = pstmt.executeUpdate(); catch (Exception ex) ex.printStackTrace(); finally this.closeAll();return row;四:Studentpackage com.bean;public
12、 class Student private int stuNo;private String stuname;private int stuscore;public int getStuNo() return stuNo; public void setStuNo(int stuNo) this.stuNo = stuNo;public String getStuname() return stuname;public void setStuname(String stuname) this.stuname = stuname;实验过程及步骤public int getStuscore()
13、return stuscore;public void setStuscore(int stuscore) this.stuscore = stuscore;(1)从键盘输入数据并能写入数据表中package com.test;import java.util.Scanner;import com.bean.Student;import com.dao.impl.StudentDaoImpl;public class test1 public static Student stuInPut() / 输入学生信息Student stu = new Student();Scanner in = n
14、ew Scanner(System.in);System.out.println("请输入学号");stu.setStuNo(in.nextInt();Scanner in1 = new Scanner(System.in);System.out.println("请输入姓名");stu.setStuname(in1.nextLine();Scanner in2 = new Scanner(System.in);System.out.println("请输入成绩");stu.setStuscore(in2.nextInt();retu
15、rn stu;public static void main(String args) Student stu = new Student();StudentDaoImpl studentdaoimpl = new StudentDaoImpl();stu = stuInPut(); / 插入学生信息if (studentdaoimpl.insert(stu) != 0)System.out.println("成功插入学生信息!");实验过程及步骤(2)修改学生成绩package com.test;import com.bean.Student;import com.dao
16、.impl.StudentDaoImpl;public class test2 public static void main(String args) Student stu = new Student();StudentDaoImpl studentdaoimpl = new StudentDaoImpl(); stu.setStuNo(1); /修改学生信息,将编号为一的学生成绩改为88 stu.setStuscore(88); stu.setStuname("zs"); if( studentdaoimpl.modify(stu)!=0 ) System.out.p
17、rintln("成功更改学生信息!");实验过程及步骤(3)删除指定学号的数据package com.test;import com.dao.impl.StudentDaoImpl;public class test3 public static void main(String args) StudentDaoImpl studentdaoimpl = new StudentDaoImpl();if (studentdaoimpl.deletestudent(3) != 0) / 删除学号为3的学生System.out.println("成功删除学生信息!&qu
18、ot;);(4)按学生姓名模糊查询 (查询姓名包含“l”的学生信息)package com.test;import java.util.ArrayList;import java.util.List;import com.bean.Student;import com.dao.impl.StudentDaoImpl;public class test4 public static void main(String args) StudentDaoImpl studentdaoimpl = new StudentDaoImpl();List<Student> stulist = new ArrayList<Student>();stulist = studentdaoimpl.getStudentByName(&
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025鞋业市场竞争格局分析及创新动向报告
- 2026年甘肃省兰州大学第二医院(第二临床医学院)医疗医技、技师及护理岗位招聘122人(第二批)笔试考试备考题库及答案解析
- 2025长白山生态保护行业发展现状评估及可持续发展规划研究报告
- 2025邮政通讯业市场分析及服务升级与投资趋势研究报告
- 海洋涡流与物种迁移-洞察及研究
- 电焊机装配工操作技能水平考核试卷含答案
- 2025邮政快递行业运营效率与网络建设投资评估研究报告
- 浸渍纸层压板工操作管理强化考核试卷含答案
- 2025造船设备行业市场全面检索及行业动态与投资计划研究报告
- 2025造船业技术创新及市场竞争发展策略深度研究报告
- 产后康复中心合作协议(医疗版)
- 颈内动脉瘤临床诊治指南
- 基建工程索赔管理人员索赔证据收集与审核指南
- AI智能生产平台-AI+质量管理
- 农村山塘维修合同
- 量子点材料的发光性能研究与应用
- 2025广东广州市卫生健康委员会直属事业单位广州市红十字会医院招聘47人(第一次)笔试考试参考题库及答案解析
- 中国外运招聘笔试题库2025
- 建筑物拆除施工沟通协调方案
- 2025食品行业专利布局分析及技术壁垒构建与创新保护策略报告
- 2025四川省教育考试院招聘编外聘用人员15人考试笔试模拟试题及答案解析
评论
0/150
提交评论