目录
前言
提示:JSP的日常一些Demo案例!
提示:以下是本篇文章正文内容,下面案例可供参考
一、目的
为了实现从页面端的表单填写将数据保存到mysql中
二、开始
1.需要工具
- eclipse
- java环境
- tomcat
- mysql-connect-jdbc
2.环境
- eclipse能正常打开
- jsp的默认项目能正常运行
3.代码
这是addEmail.jsp文件 前端数据输入
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>登录信息</title>
</head>
<body>
<form action="saveEmail.jsp" method="get">
<table border="0" cellspacing="" cellpadding="">
<tr>
<td>
选择您的雅虎邮箱:
<input type="text" name="mail" id="" value="" />
<span style="color: red;">@</span>
<SELECT name="domain">
<OPTION value="1" name="domain">yahoo.cn</OPTION>
<OPTION value="2" name="domain">yahoo.com.cn</OPTION>
</SELECT>
</td>
</tr>
<tr>
<td>
密码:
<input type="password" name="passwd1" id="" value="" size="6"/>
</td>
</tr>
<tr>
<td>
再次输入密码:
<input type="password" name="passwd2" id="" value="" size="6"/>
</td>
</tr>
<tr>
<td>
真实姓名:
<input type="text" name="name" id="" value="" />
</td>
</tr>
<tr>
<td>
出生日期:
<input type="text" name="birthday" id="" value="" />
</td>
</tr>
<tr>
<td>
性别:
<input type="radio" name="gender" id="" value="男" checked=""/>男
<input type="radio" name="gender" id="" value="女" />女
</td>
</tr>
<tr>
<td>杂志订阅:
<input type="checkbox" name="habbit" id="" value="旅游" />旅游
<input type="checkbox" name="habbit" id="" value="财经" />财经
<br />
<input type="checkbox" name="habbit" id="" value="汽车" />汽车
<input type="checkbox" name="habbit" id="" value="音乐" />音乐
</td>
</tr>
<tr>
<td>雅虎服务条款
<textarea name="tips" rows="5" cols="50">雅虎中国对于任何包含于、经由、或联结、下载或从任何与本网站有关服务(以下简称「服务」)所获得之资讯、内容或广告(以下简称「资料」),您于此接受并承认信赖任何「资料」所生之风险应自行承担。雅虎中国,有权但无此义务,改善或更正在「服务」或「资料」任何部分之错误或疏失。</textarea>
</td>
</tr>
<tr>
<td>
<input type="submit" name="submit" id="submit" value="同意服务条款并提交" style="width: 200px;"/ >
<input type="reset" name="reset" id="reset" value="清空" />
</td>
</tr>
</table>
</form>
</body>
</html>
以下是两种jsp对mysql插入写法:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"
import="java.util.*"
import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>插入数据页面</title>
<style type="text/css">
table th{
width:100px;
}
</style>
</head>
<body>
<%
//数据呈现
request.setCharacterEncoding("utf-8");
String user=request.getParameter("mail");
String domains=request.getParameter("domain");
String domain="";
if(domains.equals("1")){
domain="yahoo.cn";
}else if(domains.equals("2")){
domain="yahoo.com.cn";
}
String pwd=request.getParameter("passwd1");
String confirm_pwd=request.getParameter("passwd2");
String realname=request.getParameter("name");
String birthday=request.getParameter("birthday");
String sex=request.getParameter("gender");
String[] habbit=request.getParameterValues("habbit");
String tips=request.getParameter("tips");
String habbits="";
for(int i=0;i<habbit.length;i++){
habbits+=habbit[i];
if(i!=habbit.length-1){
habbits+=",";
}
}
%>
<%
//插入数据库
try {
Class.forName("com.mysql.jdbc.Driver");
out.println("加载驱动类成功<br/>");
String url="jdbc:mysql://localhost:3306/yahoodb?user=root&password=123456&useUnicode=true&characterEncoding=utf-8";
String username="root";
String password="123456";
Connection conn = DriverManager.getConnection(url);
if(conn!=null){
out.println("数据库连接成功<br/>");
}
Statement stmt = conn.createStatement();
String sql="insert into t_email values('"+user+"','"+domains+"','"+pwd+"','"+realname+"','"+birthday+"','"+sex+"','"+habbits+"','"+tips+"')";
int result=stmt.executeUpdate(sql);
System.out.println(sql);
if(result!=0){
out.print("成功插入<br/>");
%>
<table border="1px">
<tr>
<th>邮箱</th>
<td><%=user %></td>
</tr>
<tr>
<th>邮箱后缀</th>
<td><%=domain %></td>
</tr>
<tr>
<th>密码</th>
<td><%=pwd %></td>
</tr>
<tr>
<th>确认密码</th>
<td><%=confirm_pwd %></td>
</tr>
<tr>
<th>真实姓名</th>
<td><%=realname %></td>
</tr>
<tr>
<th>生日</th>
<td><%=birthday %></td>
</tr>
<tr>
<th>性别</th>
<td><%=sex %></td>
</tr>
<tr>
<th>爱好</th>
<td>
<%
out.println(habbits);
%>
</td>
</tr>
<tr>
<th>条款</th>
<td width="300px"><%=tips %></td>
</tr>
</table>
<%
}else{
out.print("插入失败<br/>");
}
} catch (Exception e) {
out.println("找不到驱动类<br/>"+e);
}
%>
</body>
</html>
saveEmail2.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"
import="java.util.*"
import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>插入数据页面</title>
<style type="text/css">
table th{
width:100px;
}
</style>
</head>
<body>
<%
//数据呈现
request.setCharacterEncoding("utf-8");
String user=request.getParameter("mail");
String domains=request.getParameter("domain");
String domain="";
if(domains.equals("1")){
domain="yahoo.cn";
}else if(domains.equals("2")){
domain="yahoo.com.cn";
}
String pwd=request.getParameter("passwd1");
String confirm_pwd=request.getParameter("passwd2");
String realname=request.getParameter("name");
String birthday=request.getParameter("birthday");
String sex=request.getParameter("gender");
String[] habbit=request.getParameterValues("habbit");
String tips=request.getParameter("tips");
String habbits="";
for(int i=0;i<habbit.length;i++){
habbits+=habbit[i];
if(i!=habbit.length-1){
habbits+=",";
}
}
%>
<%
//插入数据库
try {
Class.forName("com.mysql.jdbc.Driver");
out.println("加载驱动类成功<br/>");
String url="jdbc:mysql://localhost:3306/yahoodb?user=root&password=123456&useUnicode=true&characterEncoding=utf-8";
Connection conn = DriverManager.getConnection(url);
if(conn!=null){
out.println("数据库连接成功<br/>");
}
String sql="insert into t_email values(?,?,?,?,?,?,?,?)";
//"insert into t_email values('?','?','?','?','?','?','?','?')"
PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1,user );
ps.setString(2,domains );
ps.setString(3,pwd );
ps.setString(4,realname );
ps.setString(5,birthday );
ps.setString(6,sex );
ps.setString(7,habbits );
ps.setString(8,tips );
int result=ps.executeUpdate();
if(result!=0){
out.print("成功插入<br/>");
%>
<table border="1px">
<tr>
<th>邮箱</th>
<td><%=user %></td>
</tr>
<tr>
<th>邮箱后缀</th>
<td><%=domain %></td>
</tr>
<tr>
<th>密码</th>
<td><%=pwd %></td>
</tr>
<tr>
<th>确认密码</th>
<td><%=confirm_pwd %></td>
</tr>
<tr>
<th>真实姓名</th>
<td><%=realname %></td>
</tr>
<tr>
<th>生日</th>
<td><%=birthday %></td>
</tr>
<tr>
<th>性别</th>
<td><%=sex %></td>
</tr>
<tr>
<th>爱好</th>
<td>
<%
out.println(habbits);
%>
</td>
</tr>
<tr>
<th>条款</th>
<td width="300px"><%=tips %></td>
</tr>
</table>
<%
}else{
out.print("插入失败<br/>");
}
} catch (Exception e) {
out.println("找不到驱动类<br/>"+e);
}
%>
</body>
</html>
总结
有两种方式对数据库进行操作
共性就是都是通过Connection conn = DriverManager.getConnection(url);来创建连接
异同点:
一个是:
PreparedStatement ps=conn.prepareStatement(sql);
ps.executeUpdate();后面是不带参数的
另一个是:
Statement stmt = conn.createStatement();
stmt.executeUpdate(sql);