201521123030 《Java程序设计》 第14周学习总结

1. 本周学习总结

1.1 以你喜欢的方式(思维导图或其他)归纳总结多数据库相关内容。

2. 书面作业

1. MySQL数据库基本操作

建立数据库,将自己的姓名、学号作为一条记录插入。(截图,需出现自己的学号、姓名)


在自己建立的数据库上执行常见SQL语句(截图)

-参考:实验任务书-题目1

2. 使用JDBC连接数据库与Statement

2.1 使用Statement操作数据库。(粘贴一段你认为比较有价值的代码,出现学号)

2.2 使用JDBC操作数据库主要包含哪几个步骤?

1.创建连接数据库 Connection con=DriverManager.getConnection(JDBC URL,数据库用户名,密码);
2.通过连接发送Sql语句 Statement stmt = con.createStatement();
3.通过Sql语句获得结果集 ResultSet resultSet = statement.executeQuery(Sql);
4.处理结果 while(resultSet.next())
5.异常处理(SQLException 必须的处理的异常)
6.释放资源 按照从ResultSet->Statement->Connection的顺序关闭
-参考:实验任务书-题目2

3. PreparedStatement与参数化查询

3.1 使用PreparedStatement根据用户指定的查询条件进行查询。(粘贴一段你认为比较有价值的代码,出现学号)

PreparedStatement pst = null;
ResultSet rs = null;
Scanner in=new Scanner(System.in);
int idl=0;

//201521123030
try {

Class.forName(driverName);
conn = DriverManager.getConnection(URL, userName, password);
pst = conn.prepareStatement("select * from students where id=?");
pst.setInt(1, 4);//根据id查找id=4的row;
rs = pst.executeQuery();

// id | stuno | name | gender | birthdate | major
while (rs.next()) {
int id = rs.getInt("id");
String stuno = rs.getString("stuno");
Date date = rs.getDate("birthdate");
System.out.println("id=" + id + " stuno=" + stuno + " birthdate=" + date);

}

3.2 批量更新-批量插入1000个学生,统计整个操作所消耗的时间。(使用方法executeBatch)

普通方法
long t1=System.currentTimeMillis();
//处理1000个学生信息
for(int i=0;i<1000;i++){
String strSql = "insert into students(stuno,name) values(?,?)";
pst = con.prepareStatement(strSql);
pst.setString(1, "2015000");
pst.setString(2, "mmp");
pstt.addBatch();//添加到同一个批处理中
}//201521123030
long t2=System.currentTimeMillis();
Calendar c=Calendar.getInstance();
c.setTimeInMillis(t2-t1);
System.out.println("耗时: " + c.get(Calendar.MINUTE) + "分 " + c.get(Calendar.SECOND) + "秒 " + c.get(Calendar.MILLISECOND) + " 微秒");
int[] arr=pStatement.executeBatch();//执行批处理
pstt.executeUpdate();
pst.close();//立即释放资源
System.out.println(Arrays.toString(arr));

参考:实验任务书-题目3

4. JDBCUtil与DAO

4.1 粘贴一段你认为比较有价值的代码,出现学号

//201521123030
class StudentDao
{

public Connection conn = null;
public Statement statement = null;
public PreparedStatement pst=null;
public ResultSet rs=null;
private static String querySql ="select * from lin";

public StudentDao() {
try {
Driver driver = new com.mysql.cj.jdbc.Driver();
DriverManager.registerDriver(driver);
String url = "jdbc:mysql://localhost:3306/student?serverTimezone=UTC&useSSL=false";
String user = "root";
String password = "123456";
conn = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();
}
}

public boolean add(Student stu)
{
boolean flag=true;
String sql= "insert into lin (Name,ID) values(?,?)";
try{
pst=conn.prepareStatement(sql);
pst.setString(1,stu.name);
pst.setInt(2,stu.id);
int i=pst.executeUpdate();
if(i==0){
flag=false;
}

}catch (Exception e)
{
e.printStackTrace();
}
finally {
try {
pst.close();
}catch(SQLException e) {}
}

return flag;
}

public boolean delete(Student stu)
{
boolean flag=true;
String sql="delete from user where id=?";
try{
pst=conn.prepareStatement(sql);
pst.setInt(1,stu.id);
int i=pst.executeUpdate();
if(i==0){
flag=false;
}
}catch(Exception e)
{
e.printStackTrace();
}
finally {
try {
pst.close();
}catch(SQLException e) {}
}

return flag;
}

public boolean update(Student stu)
{
boolean flag=true;
String sql="update lin set Name=? where ID=?";
try{
pst=conn.prepareStatement(sql);
pst.setString(1,stu.name);
pst.setInt(2,stu.id);
int i=pst.executeUpdate();
if(i==0){
flag=false;
}
}catch (Exception e)
{
e.printStackTrace();
}
finally {
try {
pst.close();
}catch(SQLException e) {}
}

return flag;
}

public List findAll()
{
List students=new ArrayList();
try{
pst=conn.prepareStatement(querySql);
rs=pst.executeQuery();
while(rs.next())
{
students.add(new Student(rs.getString("Name"),rs.getInt("ID")));
}
}catch (Exception e)
{
e.printStackTrace();
}
finally {
try {
rs.close();
pst.close();
}catch(SQLException e) {}
}

return students;
}

public Student findById(int id)
{
Student stu=new Student();
String sql="SELECT * FROM lin where id=?";

try{
pst=conn.prepareStatement(sql);
pst.setInt(1,id);
rs=pst.executeQuery();
stu.name=rs.getString("Name");
stu.id=rs.getInt("ID");
}catch(Exception e)
{
e.printStackTrace();
}
finally{
try {
rs.close();
pst.close();
}catch(SQLException e) {}
}

return stu;

}

public List findByName(String name)
{
String sql="SELECT * FROM lin where Name like "?%"";
List students=new ArrayList();
try{
pst=conn.prepareStatement(sql);
pst.setString(1,name);
rs=pst.executeQuery();
while(rs.next())
{
students.add(new Student(rs.getString("Name"),rs.getInt("ID")));
}
}catch (Exception e)
{
e.printStackTrace();
}
finally {
try{
rs.close();
pst.close();
}catch (SQLException e){}
}

return students;
}
}

4.2 使用DAO模式访问数据库有什么好处?

DAO(Data Access Object):数据存取对象,位于业务逻辑和持久化数据之间,能够实现对持久化数据的访问
分工比较细,为了方便后期维护,使程序更加健壮。

参考:实验任务书-题目5

5. 使用数据库改造购物车系统

5.1 使用数据库改造以前的购物车系统(应有图形界面)。如果以前为完成购物车系统,可编写基于数据库的学生管理系统。包括对学生的增删改查,要求使用。

try {
//更新商品数据
String strSql = "update Commodity set name = ?";
rs = pStatement.executeQuery(strSql);
rs.setString(1, name);
rs.setDouble(2, price);
rs.setInteger(3, num);
row = rs.executeUpdate();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
pStatement.close();
rs.close();
row.close();
}

5.2 相比较使用文件,使用数据库存储与管理数据有何不一样?

可存储的量比较大,方便管理数据更新。
数据冗余小,可扩充性大。
作为一些软件的后台数据管理,就必须利用数据库进行操作。

3. 码云

3.1. 码云代码提交记录


在码云的项目中,依次选择“统计-Commits历史-设置时间段”, 然后搜索并截图

posted @ 2017-05-28 19:46  周汉麟  阅读(289)  评论(0)    收藏  举报