简单的添加
简单数据:name和sex

先把各个需要的东西建好,dyn和ser单独,然后导包
dao
package com.Dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import com.Bean.Student;
import com.Util.Utils;
public class Mannger {
// TODO Auto-generated method stub
//更改类名
public int add(Student A) throws ClassNotFoundException, SQLException {
Connection connection = Utils.getConnection();//类名
//更改表名
String sql = "insert into demotest1(name,sex) values(?,?)";
PreparedStatement preparedStatement = null;
int i=0;
try {
//创建语句传输对象
preparedStatement = connection.prepareStatement(sql);
// preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, A.getName());
preparedStatement.setString(2, A.getSex());
i = preparedStatement.executeUpdate();
preparedStatement.close();
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
}
return i;
}
}
bean
package com.Bean;
public class Student {
private String Name;
private String Sex;
public String getName() {
return Name;
}
public String getSex() {
return Sex;
}
public void setName(String name) {
Name = name;
}
public void setSex(String sex) {
Sex = sex;
}
}
package com.Util;
import java.sql.*;
public class Utils {
public static Connection getConnection() throws ClassNotFoundException, SQLException {
Connection connection = null;//连接数据库
PreparedStatement ps = null;//Statement 对象用于将 SQL 语句发送到数据库中。
ResultSet rs = null;
//1. 导入驱动jar包
//2.注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//更改数据库名
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/Demotest","root", "123456");
return connection;
}
public static void close(Connection connection ) {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void close(PreparedStatement preparedStatement ) {
try {
if (preparedStatement != null) {
preparedStatement.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void close(ResultSet resultSet ) {
try {
if (resultSet != null) {
resultSet.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.Servlet;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.Bean.Student;
import com.Dao.Mannger;
/**
* Servlet implementation class add
*/
@WebServlet("/add")
public class add extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public add() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
//设置相应的文本类型
//从这
response.setContentType("text/html;charset=utf-8");//设置响应类型,并防止中文乱码
Mannger A =new Mannger();
int i = 0;
//记得改类型名
String name = request.getParameter("name");
String sex = request.getParameter("sex");
Student B = new Student();
B.setName(name);
B.setSex(sex);
try {
i= A.add(B);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(i>0)
response.getWriter().append("添加成功!");
else
response.getWriter().append("添加失败!");
}
//到这
/**
* @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);
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加信息</title>
</head>
<body>
<div id="addSubjectForm" align="center">
<form action="add" method="post">
<tr>
<h2>请输入信息</h2>
</tr>
<table align="center">
<tr>
<td>姓名:</td>
<td>
<input type="text" name="name">
</td>
</tr>
<tr>
<td>性别:</td>
<td>
<input type="text" name="sex" >
</td>
</tr>
<tr>
<td colspan="2"><div align="center">
<input type="submit" value="录入">
</div>
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
另外,建立的数据库为Demotest,表名为demotest1

浙公网安备 33010602011771号