jdbc实现简单的增删改查

先是Book类。

然后一个主页,写一个表单,提交Book的信息到AddBook。

AddBook.jsp连接jdbc,并向Book表插入。

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import ="java.sql.Connection"%>
<%@ page import ="java.sql.DriverManager"%>
<%@ page import ="java.sql.PreparedStatement"%>
<%@ page import ="java.sql.ResultSet"%>
<%@ page import ="java.sql.Statement"%>
<%@ page import ="java.sql.*"%> <%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>uuuu</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head> <body>
<%request.setCharacterEncoding("UTF-8"); %>
<jsp:useBean id = "book" class = "Book" ></jsp:useBean>
<jsp:setProperty property="" name="book"/>
<%
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
//new oracle.jdbc.driver.OracleDriver();
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "yu", "123");
String sql = "insert into Book(id,name,price,book_count,author)values(?,?,?,?,?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, book.getId());
ps.setString(2, book.getName());
ps.setDouble(3, book.getPrice());
ps.setInt(4, book.getBookCount());
ps.setString(5, book.getAuthor());
int row = ps.executeUpdate();
if(row > 0){
out.print("成功了" + row + "条数据");
}
ps.close();
conn.close();
}catch(Exception e){
e.printStackTrace();
} %>
<br>
<a href="index.jsp">返回</a>
</body>
</html>

增  的 思路就是这么简单。

查询需要用到ResultSet。

修改只需update的sql语句。

删除只需delete的sql语句。

上一篇:cocos2dx 实现flappybird


下一篇:excel表格公式出现#REF是什么意思