<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>无标题文档</title> <style type="text/css"> <!-- body { background-color: #FFCCFF; } --> </style></head> <body> <%String id=request.getParameter("id"); Connection con; String name=null; String age=null; String birth=null; String id1=null; Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student","root","123456"); Statement sql; ResultSet rs; try { sql=con.createStatement(); rs=sql.executeQuery("SELECT * FROM student"); while(rs.next()) { name=rs.getString(2); age=rs.getString(3); birth=rs.getString(4); id1=rs.getString(1); } con.close(); } catch(SQLException e1) { out.print("SQL异常!!!!"); } %> <center> <form action="update2.jsp"> <center> <p> </p> <p>姓名: <input type="text" size="15" value="<%=name%>"> </p> <p> 年龄: <input type="text" size="15" value="<%=age%>"> </p> <p>出生日期: <input type="text" size="15" value="<%=birth%>"> <input type="hidden" value="<%=id1%>" /> </p> <p><input type="submit" value="修改"> <input type="reset" value="重置"></p> </form> </center> </body> </html>
5. update 示例2:
<%@ page contentType="text/html;charset=gb2312" %> <%@ page import="java.sql.*" %> <% String name=request.getParameter("name"); String age=request.getParameter("age"); String birth=request.getParameter("birth"); String id1=request.getParameter("id1"); System.out.println(id1); Connection con=null; try { Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student","root","123456"); Statement sql; sql=con.createStatement(); String sql2="update student set,age='"+age+"',birth='"+birth+"' wherehttps://www.jb51.net/chakan.jsp">查看</a>
6. insert 插入
<%@ page contentType="text/html;charset=gb2312" %> <%@ page import="java.sql.*" %> <% String name=request.getParameter("name"); String age=request.getParameter("age"); String birth=request.getParameter("birth"); Connection con=null; try { Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student","root","123456"); Statement sql; sql=con.createStatement(); String sql2="insert into student(name,age,birth) values('"+name+"','"+age+"','"+birth+"')"; System.out.print(sql2); int s=sql.executeUpdate(sql2); } catch(Exception e){ System.out.println(e); } %> 恭喜你,添加成功!<br /> <a href="https://www.jb51.net/chakan.jsp">查看</a>
7. 创建数据库
/* MySQL Data Transfer Source Host: localhost Source Database: student Target Host: localhost Target Database: student Date: 2009-3-27 13:24:01 */ SET FOREIGN_KEY_CHECKS=0; create database student; use student; -- ---------------------------- -- Table structure for student -- ---------------------------- CREATE TABLE `student` ( `id` int(11) NOT NULL auto_increment, `name` varchar(255) default NULL, `age` varchar(255) default NULL, `birth` varchar(255) default NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=gbk;