14周-数据库
1. 本周学习总结
1.1 以你喜欢的方式(思维导图或其他)归纳总结多数据库相关内容。
2. 书面作业
1. MySQL数据库基本操作
建立数据库,将自己的姓名、学号作为一条记录插入。(截图,需出现自己的学号、姓名)
在自己建立的数据库上执行常见SQL语句(截图)
-参考:实验任务书-题目1
创建一个表:
写入数据:
删除数据:
删除表:
2. 使用JDBC连接数据库与Statement
2.1 使用Statement操作数据库。(粘贴一段你认为比较有价值的代码,出现学号)
//201521123073
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false";
String userName = "root";
String password = "123456";
String driverName = "com.mysql.jdbc.Driver";
Class.forName(driverName);
con = DriverManager.getConnection(url, userName, password);
//201521123073插入学生信息:
String strSql = "insert into students(stuno,name,age,birthdate,major) values('2015050', '小红', 20,'1997-11-06','cs')";
stmt = con.createStatement();
resultNum = stmt.executeUpdate(strSql);
System.out.println(resultNum + "条记录已经插入");
//201521123073删除学生信息:
strSql = "delete from students where id='2'";// 删除id=2的数据
resultNum = stmt.executeUpdate(strSql);
System.out.println(resultNum + "条记录删除完毕");
//201521123073
更改学生信息:
strSql = "update students set major='中文' where id='1'";
resultNum = stmt.executeUpdate(strSql);
System.out.println(resultNum + "条记录更新完毕");
//201521123073
选择性的输出学生信息:
strSql = "select stuno,name,birthdate,major from students";
rs = stmt.executeQuery(strSql);
System.out.println("学号\t\t姓名\t出生日期\t\t专业");
while (rs.next()) {
System.out.print(rs.getString("stuno") + "\t\t");
System.out.print(rs.getString(2) + " \t");
System.out.print(rs.getDate("birthdate") + "\t");
System.out.print(rs.getString(4) + "\t\n");
}
2.2 使用JDBC操作数据库主要包含哪几个步骤?
答:
1.加载数据库驱动
2.建立连接
3.向数据库发送SQL语句
4.获得并处理结果集
5.关闭连接,释放资源
3. PreparedStatement与参数化查询
3.1 使用PreparedStatement根据用户指定的查询条件进行查询。(粘贴一段你认为比较有价值的代码,出现学号)
3.2 批量更新-批量插入1000个学生,统计整个操作所消耗的时间。(使用方法executeBatch)
参考:实验任务书-题目3
4. JDBCUtil与DAO
4.1 粘贴一段你认为比较有价值的代码,出现学号
4.2 使用DAO模式访问数据库有什么好处?
//201521123073
public class StudentDaoJDBCImpl implements StudentDao {
@Override
public int add(Student stu) {
// TODO Auto-generated method stub
Connection conn = null;
PreparedStatement pstat = null;
String sql = "insert into student(id,name) values(?,?) ";//表中有id和name这列
int result = -1;
try {
conn = JDBCUtil.getConnection();
pstat = conn.prepareStatement(sql);
pstat.setInt(1, stu.getId());
pstat.setString(2, stu.getName());
result = pstat.executeUpdate();
}catch (SQLException sqle) {
sqle.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}finally{
JDBCUtil.realeaseAll(null,pstat, conn);
}
return result;
}
@Override
public int delete(int sid) {
// TODO Auto-generated method stub
Connection conn = null;
PreparedStatement pstat = null;
String sql = "delete from student where id=?";//表中有id和name这列
int result = -1;
try {
conn = JDBCUtil.getConnection();
pstat = conn.prepareStatement(sql);
pstat.setInt(1, sid);
result = pstat.executeUpdate();
}catch (SQLException sqle) {
sqle.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}finally{
JDBCUtil.realeaseAll(null,pstat, conn);
}
return result;
}
@Override
public int update(Student stu) {
// TODO Auto-generated method stub
Connection conn = null;
PreparedStatement pstat = null;
String sql = "update student set age=? where id=?";//表中有id和name这列
int result = -1;
try {
conn = JDBCUtil.getConnection();
pstat = conn.prepareStatement(sql);
pstat.setString(1,stu.getName());
pstat.setInt(2, stu.getId());
result = pstat.executeUpdate();
}catch (SQLException sqle) {
sqle.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}finally{
JDBCUtil.realeaseAll(null,pstat, conn);
}
return result;
}
@Override
public List<Student> findAll() {
// TODO Auto-generated method stub
Connection conn = null;
Statement stat = null;
ResultSet rs = null;
Student stu=null;
List<Student> students=new ArrayList<Student>();
String sql = "select * from student";//表中有id和name这列
try {
conn = JDBCUtil.getConnection();
stat = conn.createStatement();
rs = stat.executeQuery(sql);
while(rs.next()){
stu=new Student(rs.getInt("id"),rs.getString("name"));
students.add(stu);
}
}catch (SQLException sqle) {
sqle.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}finally{
JDBCUtil.realeaseAll(rs,stat, conn);
}
return students;
}
@Override
public Student findById(String sid) {
// TODO Auto-generated method stub
Connection conn = null;
PreparedStatement pstat = null;
ResultSet rs = null;
String sql = "select * from student where id=?";//表中有id和name这列
Student student = null;
try {
conn = JDBCUtil.getConnection();
pstat = conn.prepareStatement(sql);
pstat.setString(1, sid);
rs = pstat.executeQuery();
while(rs.next()){
student = new Student(rs.getInt("id"),rs.getString("name"));
}
}catch (SQLException sqle) {
sqle.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}finally{
JDBCUtil.realeaseAll(rs,pstat, conn);
}
return student;
}
@Override
public List<Student> findByName(String name) {
// TODO Auto-generated method stub
Student student = null;
Connection conn = null;
PreparedStatement pstat = null;
ResultSet rs = null;
Student stu=null;
List<Student> students=new ArrayList<Student>();
String sql = "select * from students where name like '?%'";//表中有id和name这列
try {
conn = JDBCUtil.getConnection();
pstat = conn.prepareStatement(sql);
pstat.setString(1, name);
rs = pstat.executeQuery();
while(rs.next()){
stu=new Student(rs.getInt("id"),rs.getString("name"));
students.add(stu);
}
}catch (SQLException sqle) {
sqle.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}finally{
JDBCUtil.realeaseAll(rs,pstat, conn);
}
return students;
}
}
(4.2)答:DAO模式相当于建立一个接口。
数据库的修改只要修改DAO层的访问方式就可以了,不用对整个项目都进行修改
5. 使用数据库改造购物车系统
5.1 使用数据库改造以前的购物车系统(应有图形界面)。如果以前为完成购物车系统,可编写基于数据库的学生管理系统。包括对学生的增删改查,要求使用。
5.2 相比较使用文件,使用数据库存储与管理数据有何不一样?
3. 码云
3.1. 码云代码提交记录
在码云的项目中,依次选择“统计-Commits历史-设置时间段”, 然后搜索并截图
4.课外阅读
4.1 JDBC(TM) Database Access
4.2 代码结构中Dao,Service,Controller,Util,Model是什么意思,为什么划分
4.3 mysq数据库管理工具navicat基本使用方法