📄 notedao.java
字号:
package com.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.domain.Note;
import com.util.MyDataSource;
public class NoteDAO {
public void create(Note note) throws Exception {
MyDataSource mds = MyDataSource.getInstance();
Connection con = mds.getConnection();
String sql = "insert into notes(title, content, author, create_time) values (?, ?, ?, CURDATE())";
PreparedStatement pSta = con.prepareStatement(sql);
pSta.setString(1, note.getTitle());
pSta.setString(2, note.getContent());
pSta.setString(3, note.getAuthor());
pSta.executeUpdate();
con.close();
}
public List findAll() throws Exception {
MyDataSource mds = MyDataSource.getInstance();
Connection con = mds.getConnection();
String sql = "select * from notes";
Statement sta = con.createStatement();
ResultSet rs = sta.executeQuery(sql);
ArrayList notes = new ArrayList();
while(rs.next()) {
Note tn = new Note();
tn.setId(rs.getInt("id"));
tn.setTitle(rs.getString("title"));
tn.setContent(rs.getString("content"));
tn.setAuthor(rs.getString("author"));
tn.setCreateTime(rs.getTimestamp("create_time"));
notes.add(tn);
}
con.close();
return notes;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -