Servlet(数据库操作)
新建Servlet类,命名为:SaveServlet(注意,这里我选的版本是3.0,所以WEB-INF的lib目录下没有web.xml配置文件,这个版本用的是全新的注释)将下面这句放在Servlet的类(即SaveServlet)声明之前@WebServlet(name="saveServlet",urlPatterns="/SaveServlet")SaveServlet.java类...
Servlet(数据库操作)
我用的mysql8.0+tomcat9.0+jdk9.0.1
新建Servlet类,命名为:SaveServlet
(注意,这里我选的版本是3.0,所以WEB-INF的lib目录下没有web.xml配置文件,这个版本用的是全新的注释)
将下面这句放在Servlet的类(即SaveServlet)声明之前(重点)
@WebServlet(name="saveServlet",urlPatterns="/SaveServlet")
SaveServlet.java类
package cast;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class SaveServlet
*/
@WebServlet(name="saveServlet",urlPatterns="/SaveServlet")
public class SaveServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private Connection con=null;
/**
* @see HttpServlet#HttpServlet()
*/
public SaveServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
// 驱动程序名
String driver = "com.mysql.cj.jdbc.Driver";
// URL指向要访问的数据库名mydata
String url = "jdbc:mysql://localhost:3306/testform?useSSL=false&serverTimezone=UTC";
// MySQL配置时的用户名
String user = "root";
// MySQL配置时的密码
String password = "123456";
try {
Class.forName(driver);// 加载数据库驱动
System.out.println("加载驱动成功");
con = DriverManager.getConnection(url,user,password); // 获取数据库连接
if (con != null) {
System.out.println("数据库连接成功");
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//设置编码f8
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String username=request.getParameter("username");
String password=request.getParameter("password");
String sex=request.getParameter("sex");
String question=request.getParameter("question");
String answer=request.getParameter("answer");
String email=request.getParameter("Email");
// System.out.println(username+password+sex+question+answer+email);
if(con!=null) {
String sql="insert into list(username,password,sex,question,answer,email)"
+"values(?,?,?,?,?,?)";//插入注册信息的sql语句,使用通配符
PreparedStatement ps;
try {
ps = con.prepareStatement(sql);//创建PreparedStatement对象
ps.setString(1, username); //对sql语句中的参数动态赋值
ps.setString(2, password);
ps.setString(3, sex);
ps.setString(4, question);
ps.setString(5, answer);
ps.setString(6, email);
ps.executeUpdate(); //执行更新操作
PrintWriter out=response.getWriter();
out.print("<h1 align='center'>");
out.print(username+"注册成功");
out.print("</h1>");
out.flush();
out.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else {
response.sendError(500, "数据库连接错误");
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
建立login.html文件
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="SaveServlet" method="post" onsubmit="return reg(this)">
<table align="center" border="0" width="500">
<tr>
<td align="right" width="30%">用户名:</td>
<td><input type="text" name="username" class="box"></td>
</tr>
<tr>
<td align="right">密码:</td>
<td><input type="password" name="password" class="box"></td>
</tr>
<tr>
<td align="right">确认密码</td>
<td><input type="password" name="repassword" class="box"></td>
</tr>
<tr>
<td align="right">性别:</td>
<td>
<input type="radio" name="sex" value="男" checked="chedked">男
<input type="radio" name="sex" value="女">女
</td>
</tr>
<tr>
<td align="right">密码找回问题:</td>
<td><input type="text" name="question" class="box"></td>
</tr>
<tr>
<td align="right">密码找回答案:</td>
<td><input type="text" name="answer" class="box">
</tr>
<tr>
<td align="right">邮箱:</td>
<td><input type="text" name="Email" class="box"></td>
</tr>
<tr>
<td colspan="2" align="center" height="40">
<input type="submit" value="注册">
<input type="reset" value="重置">
</td>
</tr>
</table>
</form>
</body>
</html>
运行结果
(现在文本框中输入值,在点击注册)
数据提交到了SaveServlet.java,里面用了输出流(显示成功注册)
查看数据库中的结果,数据库里确实插入了一条记录,就是刚才在文本中输入的值(这里我的navicat方便查看)
说明:
<1>注意自己数据库的版本,我用的mysql8.0和jdk9.0.1,高版本的驱动连接方式和旧版本不同
使用JDBC链接MySQL5.0的版本时,其驱动加载语句为:com.mysql.jdbc.Driver。
但在MySQL6.0及MySQL6.0以上的版本中,使用的驱动加载语句为:com.mysql.cj.jdbc.Driver。
<2>将数据库的驱动放在WEB-INF/lib目录下面,再配置构建路径,jar包最终会加入Web app libries这个库下面。
如果你碰到了中文乱码问题,点击这里:https://blog.csdn.net/qq_43753724/article/details/105207985
更多推荐
所有评论(0)