码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-05-28 23:14:18      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:连接数   nts   cut   exe   指定   自己的   getc   where   creat   

1. 本周学习总结

技术分享

2. 书面作业

1. MySQL数据库基本操作
建立数据库,将自己的姓名、学号作为一条记录插入。(截图,需出现自己的学号、姓名)
在自己建立的数据库上执行常见SQL语句(截图)

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

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

//201521123088
try {
    conn = DriverManager.getConnection(URL,userName,password);
    Statement statement = conn.createStatement();
    ResultSet resultSet = statement.executeQuery(sql);
    //JDBC连接数据库
    while(resultSet.next()){
        int id = resultSet.getInt("id");
        String stuno = resultSet.getString("stuno");
        String name = resultSet.getString("name");
        System.out.print("id="+id+" stuno="+stuno+" name="+name);
    }   
} catch (SQLException e) {
    e.printStackTrace();
}finally{
    if(conn!=null)
        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    conn = null;
}

 

2.2 你认为使用JDBC操作数据库的套路是什么?有那几点需要注意。
装载驱动→与数据库建立连接→向数据库发送SQL语句→获得和处理查询或更新语句返回的结果→关闭连接,释放资源

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

//201521123088
String strSql = "select * from students where Id < ?";//举例,查询条件为id<10的学生信息
pStatement = con.prepareStatement(strSql);
pStatement.setInt(1, 10);
rs = pStatement.executeQuery();
while(rs.next()){   
    System.out.println(rs.getInt("id"));
    System.out.println(rs.getString("stuno"));
    System.out.println(rs.getString("name"));
    System.out.println(rs.getInt("age"));
}
pStatement.close();

 

  1. 码云

3.1. 码云代码提交记录

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

标签:连接数   nts   cut   exe   指定   自己的   getc   where   creat   

原文地址:http://www.cnblogs.com/lzy-mini/p/6916804.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!