1、在MySQL数据库中先新建一个数据库
表结构如下:
2、在IDEA中连接数据库
在连接数据库之前,先将mysql-connector-java-8.0.19.jar导入
但是有时候这种导入方式不成功,会报错(java.lang.ClassNotFoundException: com. mysql.cj.jdbc.Driver),这时直接将jar包放进tomcat的离别文件夹中即可
3、创建数据库处理工具类DbPreparedUtil.java
package com.example.jdbc_res;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class DbPreparedUtil {
private Connection connection;
private PreparedStatement statement;
/**
* 初始化和获取connection
* @return
* @throws Exception
*/
public Connection getConnection() throws Exception{
if (this.connection==null){//赋值
Class.forName("com.mysql.cj.jdbc.Driver");
System.out.println("成功执行第一步");
//第二步,获取连接
this.connection=
DriverManager.getConnection(
"jdbc:mysql://localhost:3306/testdb?serverTimezone=UTC",
"root",
"123456");
System.out.println("成功执行第二步");
}
return this.connection;
}
/**
* 初始化和获取statement
* @return
* @throws Exception
*/
public PreparedStatement getStatement(String sql)
throws Exception{
this.closeStatement();
if(this.statement==null){
this.statement=
this.getConnection().prepareStatement(sql);
}
return this.statement;
}
/**
* 执行查询的方法
* @param sql
* @return
* @throws Exception
*/
public List executeQuery(String sql, Object...parameters) throws Exception{
PreparedStatement statement=this.getStatement(sql);
int index=1;
for(Object param:parameters){
statement.setObject(index,param);
index++;
}
ResultSet rs=statement.executeQuery();
List res=this.getResult(rs);
rs.close();
this.closeStatement();
System.out.println("成功执行第三步");
return res;
}
/**
* 执行更新,插入,删除的方法
* @param sql
* @return
* @throws Exception
*/
public int executeUpdate(String sql,Object...parameters) throws Exception{
PreparedStatement statement=this.getStatement(sql);
int index=1;
for(Object param:parameters){
statement.setObject(index,param);
index++;
}
System.out.println("成功执行第四步");
return statement.executeUpdate();
}
/**
* 关闭statement
*/
public void closeStatement() throws Exception{
if(this.statement!=null){
this.statement.close();
this.statement=null;
}
}
/**
* 关闭连接
*/
public void close() throws Exception{
try {
this.closeStatement();
}catch (Exception e){
e.printStackTrace();
}
if(this.connection!=null){
this.connection.close();
}
System.out.println("成功执行第五步");
}
/**
* resultset转化为list
* @param rs
* @return
*/
public List getResult(ResultSet rs)throws Exception{
ResultSetMetaData metaData=rs.getMetaData();
List trs=new ArrayList();
while (rs.next()){
Map temp=new HashMap();
for(int i=1;i<=metaData.getColumnCount();i++){
String key=metaData.getColumnName(i);
Object val=rs.getObject(i);
temp.put(key,val);
}
trs.add(temp);
}
return trs;
}
}
4、创建注册JSP页面register.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>register</title>
</head>
<body>
<form action="result.jsp" method="post">
用户名:<input type="text" name="username"/><br>
密 码:<input type="password" name="password"/><br>
性 别:
<input type="radio" name="sex" value="男" checked ="checked" >男
<input type="radio" name="sex" value="女">女<br>
说 明:<input type="text" name="note"/><br>
<input type="submit" name="submit"/>
</form>
</body>
</html>
5、利用数据工具类实现注册信息入库
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>result</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
String sex = request.getParameter("sex");
String note = request.getParameter("note");
System.out.println(username);
System.out.println(password);
System.out.println(sex);
System.out.println(note);
String sex_new;
if(sex.equals("男")){
sex_new = "1";
}else{
sex_new = "0";
}
DbPreparedUtil dbPreparedUtil = new DbPreparedUtil();
String sql = "insert into userinfo values(?,?,?,?)";
int result = dbPreparedUtil.executeUpdate(sql , new Object[]{username,password,sex_new,note});
if(result>0){
%>
注册成功
<%}else{%>
注册不成功
<%}%>
</body>
</html>