public class Student
{
  private Integer studId;
  private String name;
  private String email;
  private Date dob;
  // setters and getters
}

public Student findStudentById(int studId)
{
  Student student = null;
  Connection conn = null;
  try{
    //obtain connection
    conn = getDatabaseConnection();
    String sql = "SELECT * FROM STUDENTS WHERE STUD_ID=?";
    //create PreparedStatement
    PreparedStatement pstmt = conn.prepareStatement(sql);
    //set input parameters
    pstmt.setInt(1, studId);
    ResultSet rs = pstmt.executeQuery();
    //fetch results from database and populate into Java objects
    if(rs.next()) {
      student = new Student();
      student.setStudId(rs.getInt("stud_id"));
      student.setName(rs.getString("name"));
      student.setEmail(rs.getString("email"));
      student.setDob(rs.getDate("dob"));
    }
  } catch (SQLException e){
    throw new RuntimeException(e);
  }finally{
    //close connection
    if(conn!= null){
      try {
        conn.close();
      } catch (SQLException e){ }
    }
  }
  return student;
}

public void createStudent(Student student)
{
  Connection conn = null;
  try{
    //obtain connection
    conn = getDatabaseConnection();
    String sql = "INSERT INTO STUDENTS(STUD_ID,NAME,EMAIL,DOB)
    VALUES(?,?,?,?)";
    //create a PreparedStatement
    PreparedStatement pstmt = conn.prepareStatement(sql);
    //set input parameters
    pstmt.setInt(1, student.getStudId());
    pstmt.setString(2, student.getName());
    pstmt.setString(3, student.getEmail());
    pstmt.setDate(4, new
    java.sql.Date(student.getDob().getTime()));
    pstmt.executeUpdate();
  } catch (SQLException e){
    throw new RuntimeException(e);
  }finally{
    //close connection
    if(conn!= null){
    try {
      conn.close();
      } catch (SQLException e){ }
    }
  }
}

protected Connection getDatabaseConnection() throws SQLException
{
  try{
    Class.forName("com.mysql.jdbc.Driver");
    return DriverManager.getConnection
    ("jdbc:mysql://localhost:3306/test", "root", "admin");
  } catch (SQLException e){
    throw e;
  } catch (Exception e){
    throw new RuntimeException(e);
  }
}

JDBC Boilerplate的更多相关文章

  1. HTML5 Boilerplate - 让页面有个好的开始

    最近看到了HTML5 Boilerplate模版,系统的学习与了解了一下.在各种CSS库.JS框架层出不穷的今天,能看到这么好的HTML模版,感觉甚爽.写篇博客,推荐给大家使用.   一:HTML5 ...

  2. Asp.net Boilerplate源码中NotNullAttribute的用处

    看Asp.net Boilerplate 1.1.3.0源码时发现有一个NotNullAttribute的定义和27处的引用,就是不知道它的作用,当然顾名思义是可以的,就是不知道它是怎么判断的,在哪里 ...

  3. Java数据库连接技术——JDBC

    大家好,今天我们学习了Java如何连接数据库.之前学过.net语言的数据库操作,感觉就是一通百通,大同小异. JDBC是Java数据库连接技术的简称,提供连接各种常用数据库的能力. JDBC API ...

  4. 玩转spring boot——结合AngularJs和JDBC

    参考官方例子:http://spring.io/guides/gs/relational-data-access/ 一.项目准备 在建立mysql数据库后新建表“t_order” ; -- ----- ...

  5. [原创]java使用JDBC向MySQL数据库批次插入10W条数据测试效率

    使用JDBC连接MySQL数据库进行数据插入的时候,特别是大批量数据连续插入(100000),如何提高效率呢?在JDBC编程接口中Statement 有两个方法特别值得注意:通过使用addBatch( ...

  6. JDBC MySQL 多表关联查询查询

    public static void main(String[] args) throws Exception{ Class.forName("com.mysql.jdbc.Driver&q ...

  7. JDBC增加删除修改

    一.配置程序--让我们程序能找到数据库的驱动jar包 1.把.jar文件复制到项目中去,整合的时候方便. 2.在eclipse项目右击"构建路径"--"配置构建路径&qu ...

  8. JDBC简介

    jdbc连接数据库的四个对象 DriverManager  驱动类   DriverManager.registerDriver(new com.mysql.jdbc.Driver());不建议使用 ...

  9. JDBC Tutorials: Commit or Rollback transaction in finally block

    http://skeletoncoder.blogspot.com/2006/10/jdbc-tutorials-commit-or-rollback.html JDBC Tutorials: Com ...

随机推荐

  1. git代理设置方法解决

    git config --global https.proxy http://127.0.0.1:1080 git config --global https.proxy https://127.0. ...

  2. Camstar Portal modeling user guid --WorkCenter workCell workStation的关系

    业务需求:如何让用户登陆时选择  生产线(如:SMT生产线) Operation(如:维修员)  工作单元(如:印刷段) 工作站(如:上板) 关系图: 从上图可以看出: workCell是Resour ...

  3. REMOVE ONCLICK DELAY ON WEBKIT FOR IPHONE

    Developing on the webkit for iPhone I encountered a curious delay ononClick events. It seems that th ...

  4. http之100-continue(转)

    1.http 100-continue用于客户端在发送POST数据给服务器前,征询服务器情况,看服务器是否处理POST的数据,如果不处理,客户端则不上传POST数据,如果处理,则POST上传数据.在现 ...

  5. ELK:kibana使用的lucene查询语法【转载】

    kibana在ELK阵营中用来查询展示数据 elasticsearch构建在Lucene之上,过滤器语法和Lucene相同 全文搜索 在搜索栏输入login,会返回所有字段值中包含login的文档 使 ...

  6. (转)Jquery最实用的实例及源码(http://www.cnblogs.com/kingfly/archive/2012/12/05/2802539.html)

    1:窗口拖动 http://jqueryui.com/resizable/#max-min 2:导航条前后顺序拖动 http://jqueryui.com/sortable/ 3:类似百度首页板块顺序 ...

  7. NC 单据保存时间过长,判断数据库锁表解决办法

    SELECT s.sid, s.serial# FROM gv$locked_object l, dba_objects o, gv$session s WHERE l.object_id = o.o ...

  8. H5学习系列之文件读取API--本文转自http://blog.csdn.net/jackfrued/article/details/8967667

    HTML5定义了FileReader作为文件API的重要成员用于读取文件,根据W3C的定义,FileReader接口提供了读取文件的方法和包含读取结果的事件模型. FileReader的使用方式非常简 ...

  9. Spring 4 官方文档学习 Spring与Java EE技术的集成

    本部分覆盖了以下内容: Chapter 28, Remoting and web services using Spring -- 使用Spring进行远程和web服务 Chapter 29, Ent ...

  10. advanced validation on purchase.

    安装模块 此模块在 标准功能的 2级审批基础上 增加 老板审批 增加 不同技术类和 非技术类的分支 核心审批工作流 如下图示 为审批用户 授予 purchase manager 权限 否则,看不到 审 ...