序列的创建与应用

序列的创建:
-- 创建序列
CREATE SEQUENCE note_sequ ;

 

-- 创建留言表
CREATE TABLE note
(
 id int not null primary key , -- sequence
 title varchar(20) not null ,
 author varchar(20) not null ,
 content varchar(50) not null
) ;


-- 事务提交
commit ;
=============================================================
序列的应用:
public class NoteDAOImpl implements NoteDAO
{
 // 增加操作
 public void insert(Note note) throws Exception
 {
  String sql = "INSERT INTO note(id,title,author,content) VALUES(note_sequ.nextVal,?,?,?)" ;
  PreparedStatement pstmt = null ;
  DataBaseConnection dbc = null ;
  dbc = new DataBaseConnection() ;
  try
  {
   pstmt = dbc.getConnection().prepareStatement(sql) ;
   pstmt.setString(1,note.getTitle()) ;
   pstmt.setString(2,note.getAuthor()) ;
   pstmt.setString(3,note.getContent()) ;
   pstmt.executeUpdate() ;
   pstmt.close() ;
  }
  catch (Exception e)
  {
   // System.out.println(e) ;
   throw new Exception("操作中出现错误!!!") ;
  }
  finally
  {
   dbc.close() ;
  }
 }

你可能感兴趣的:(sql)