- 1、本文档共8页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
学号 姓名 实验序号 实验十二 实验名称 数据库编程 实验地点 实验日期 实验内容 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)