java面向对象实验十二数据库编程及答案.docVIP

  • 4
  • 0
  • 约 8页
  • 2016-10-23 发布于湖北
  • 举报

java面向对象实验十二数据库编程及答案.doc

学号 姓名 实验序号 实验十二 实验名称 数据库编程 实验地点 实验日期 实验内容 1.在数据库(xscj)里建立如下数据表tb_student: 使用的数据库系统不限。 2.使用Java JDBC对该数据表进行增删改查,输出到屏幕。JDBC连接方式不限。 (1)从键盘输入数据并能写入数据表中 (2)修改学生成绩 (3)删除指定学号的数据 (4)按学生姓名模糊查询 实验过程及步骤 在SQL数据库中创建xscj数据库,建立数据表tb_student。 使用Java JDBC对该数据表进行增删改查,输出到屏幕。 一:BaseDao: package com.util; import java.sql.Connection; import 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(){ Connection aconn=null; try{ Class.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(){ try{ if(rs!=null)rs.close(); if(pstmt!=null)pstmt.close(); if(stmt!=null)stmt.close(); if(conn!=null)conn.close(); }catch(Exception ex){ ex.printStackTrace(); }}} 二:StudentDao package com.dao; import java.util.List; import com.bean.Student; public interface StudentDao { public int insert(Student stu); public int deletestudent(int stuno); public ListStudent getStudentByName(String stuname); public int modify(Student stu); } 三:StudentDaoImpl package 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 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()); pstmt.setInt(3, stu.getStuscore()); row = pstmt.executeUpdate(); } catch (Exception e

文档评论(0)

1亿VIP精品文档

相关文档