1.form表单的获取
form.jsp
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2024/4/13
Time: 23:08
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=GBK" language="java" %>
<html>
<head>
<title>form_show.jsp</title>
</head>
<body>
<ul style="list-style: none; line-height: 30px">
<li>输入用户姓名:<%=new String(request.getParameter("name").getBytes("ISO8859_1"),"GBK")%></li>
<li>选择性别:<%=new String(request.getParameter("sex").getBytes("ISO8859_1"),"GBK")%></li>
<li>选择密码提示问题:<%=new String(request.getParameter("question").getBytes("ISO8859_1"),"GBK") %></li>
<li>请输入问题答案:<%=new String(request.getParameter("answer").getBytes("ISO8859_1"),"GBK") %>
</li>
<li>请选择个人爱好:
<%
String[] likes=request.getParameterValues("like");
for(int i=0;i<likes.length;i++){
%>
<%=new String(likes[i].getBytes("ISO8859_1"),"GBK")+" "%>
<%
}
%>
</li>
</ul>
</body>
</html>
form_show.jsp
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2024/4/13
Time: 23:08
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=GBK" language="java" %>
<html>
<head>
<title>form_show.jsp</title>
</head>
<body>
<ul style="list-style: none; line-height: 30px">
<li>输入用户姓名:<%=new String(request.getParameter("name").getBytes("ISO8859_1"),"GBK")%></li>
<li>选择性别:<%=new String(request.getParameter("sex").getBytes("ISO8859_1"),"GBK")%></li>
<li>选择密码提示问题:<%=new String(request.getParameter("question").getBytes("ISO8859_1"),"GBK") %></li>
<li>请输入问题答案:<%=new String(request.getParameter("answer").getBytes("ISO8859_1"),"GBK") %>
</li>
<li>请选择个人爱好:
<%
String[] likes=request.getParameterValues("like");
for(int i=0;i<likes.length;i++){
%>
<%=new String(likes[i].getBytes("ISO8859_1"),"GBK")+" "%>
<%
}
%>
</li>
</ul>
</body>
</html>
2.mysql数据库及表的建立
数据库创建sql
CREATE DATABASE `my_database`
数据表创建sql
CREATE TABLE `user` (
`id` int NOT NULL AUTO_INCREMENT COMMENT '编号',
`username` varchar(255) COLLATE utf8mb4_general_ci NOT NULL COMMENT '用户名',
`sex` char(1) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '性别',
`birthdate` date DEFAULT NULL COMMENT '出生日期',
`age` int DEFAULT NULL COMMENT '年龄',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci
3.maven项目的建立
Hellowworld.java
package package1;
public class Helloworld {
public static void main(String args[]) {
System.out.println("Hello world");
}
}
4.使用jdbc进行数据库的增删改查操作
package package1;
import java.sql.*;
public class TestJDBC {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection c = DriverManager
.getConnection(
"jdbc:mysql://127.0.0.1:3306/my_database?characterEncoding=UTF-8",
"root", "2004");
Statement s = c.createStatement();
// 准备sql语句
// 注意: 字符串要用单引号'
String sql = "select * from user";
ResultSet rs =s.executeQuery(sql);
while (rs.next()) {
System.out.println( rs.getString(2) +","+rs.getString(3)+","+rs.getString(4)+","+rs.getString(5));
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}