好吧,最近学了一阵子的ssh回过头来发现,JSP简单的CRUD忘得差不多了,基础不牢靠,记录记录(最原始的写法)
insert
<%
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/book","root","123456");
String sql = "insert into booktype (detail,title,id) values (?,?,null)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1,"descripton");
ps.setString(2,"Japan");
ps.executeUpdate();
ps.close();
conn.close();
%>update
<%
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/book","root","123456");
String sql = "update booktype set title = ? where id = ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1,"china");
ps.setInt(2,2);
ps.executeUpdate();
ps.close();
conn.close();
%>
<%
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/book","root","123456");
String sql = "select * from booktype";
PreparedStatement psQuery = conn.prepareStatement(sql);
ResultSet rs = psQuery.executeQuery();
while(rs.next()){
int id = rs.getInt("id");
String title = rs.getString("title");
String detail = rs.getString("detail");
System.out.println(id);
System.out.println(title);
System.out.println(detail);
}
rs.close();
psQuery.close();
conn.close();
%>delete
<%
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/book","root","123456");
String sql = "delete from booktype where id = ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1,1);
ps.executeUpdate();
ps.close();
conn.close();
%>
本文提供了一个使用JSP进行数据库增删改查(CRUD)操作的基础示例,包括连接数据库、执行插入、更新、选择和删除操作的具体代码实现。
677

被折叠的 条评论
为什么被折叠?



