好激动好激动,熬夜这么久终于做出了!!!
为了梳理知识、巩固复习,也为了大家少走弯路特地整理发出来
(JavaIDE使用的是eclipse,数据库使用的是MySQL,数据库可视化使用的navicat,开发网页使用的tomcat)
先发一下目录结构
在发代码之前先将一下这里各个包及文件的作用
Bean包下的Bean.java 文件用于存放实体类
Dao包下的Dao.java 文件用于实现数据与数据库的交互(既实现增删改查等操作)
DBUtil包下的DBUtil.java 文件用于封装连接数据库的操作(这样进行增删改查操作时就不需要每个方法都写一遍连接数据库的操作啦)
Servlet包下的Servlet.java 文件用于实现数据与前端网页页面的数据连接(获取网页中用户输入的信息)
webapp目录下的add.jsp 用于编写页面(此处仅为学习通过网页进行添加操作,对页面美观度未作要求)
放接下来就代码啦,小伙伴们接好咯
Bean.java //Bean包下的Bean.java 文件用于存放实体类
package Bean;
public class Bean {
String id;
String name;
public Bean() {
// TODO Auto-generated constructor stub
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Bean(String id, String name) {
super();
this.id = id;
this.name = name;
}
}
Dao.java //Dao包下的Dao.java 文件用于实现数据与数据库的交互(既实现增删改查等操作)
package Dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import DBUtil.DBUtil;
public class Dao {
public static void add(String id, String name) {
// TODO Auto-generated method stub
Connection connection=null;
PreparedStatement preparedStatement=null;
connection= DBUtil.getConnection();
String sql="insert into inf(id,name) values (?,?)";
try{
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setString(1,id);//就是把id替代sql的第一个问号,id由前端传过来
preparedStatement.setString(2,name);
System.out.print("信息传输成功");
int flag = preparedStatement.executeUpdate();
if(flag != 0) {
System.out.print("信息添加成功");
} else {
System.out.print("信息添加失败");
}
preparedStatement.close();
connection.close();
}catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
DBUtil.close(preparedStatement,connection);
}
}
}
DBUtil.java //DBUtil包下的DBUtil.java 文件用于封装连接数据库的操作(这样进行增删改查操作时就不需要每个方法都写一遍连接数据库的操作啦)
//小伙伴们记得修改url和pass
package DBUtil;
import java.sql.*;
public class DBUtil {
private static Connection connection;
private static String url="jdbc:mysql://localhost:3306/test?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
private static String user="root";//用户名
private static String pass="mysql26331";//密码
static {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection(){
try {
connection= DriverManager.getConnection(url,user,pass);
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
public static void close(Connection connection, Statement statement, ResultSet resultSet){
try {
if (connection!=null) {
connection.close();
}
if (statement!=null){
statement.close();
}
if (resultSet!=null){
resultSet.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(PreparedStatement preparedStatement, Connection connection) {
// TODO Auto-generated method stub
try {
preparedStatement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Servlet.java //Servlet包下的Servlet.java 文件用于实现数据与前端网页页面的数据连接(获取网页中用户输入的信息)
package Servlet;
import java.io.IOException;
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 Dao.Dao;
/**
* Servlet implementation class Servlet
*/
@WebServlet("/Servlet")
public class Servlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Servlet() {
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
//response.getWriter().append("Served at: ").append(request.getContextPath());
//防止乱码
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
//获取前端传来的数据
String id=request.getParameter("id");
String name=request.getParameter("name");
Dao.add(id,name);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
this.doGet(request, response);
}
}
add.jsp //webapp目录下的add.jsp 用于编写页面(此处仅为学习通过网页进行添加操作,对页面美观度未作要求)
//这里强调一点,<title>添加</title>语句之前的操作较多是因为本人之前因为乱码问题进行了大量修改
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page 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>
</head>
<body>
<center>
<form action="Servlet" method="post">
<p style="height: 20px; border: 1px solid red;text-align: center;">课程查询</p>
<p>请输入id:<input type="text" name="id"><p/>
<p>请输入姓名:<input type="text" name="name"><p/>
<input type="submit" name="submit" value="添加">
</form>
</center>
</body>
</html>
最后祝大家旗开得胜,晚上睡得早头发掉的少!
标签:java,String,数据库,许久,public,connection,添加,id,name From: https://www.cnblogs.com/yansans/p/16823499.html