今天下午连接数据库,半成品
jsp <%@ page contentType="text/html;charset=UTF-8" language="java" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>添加信息</title> <script> function submitButtonClick() { let className = document.getElementById("className").value; let classTeacher = document.getElementById("classTeacher").value; let classPlace = document.getElementById("classPlace").value; if (!(classTeacher=="王建民" || classTeacher=="刘立嘉"|| classTeacher=="刘丹"|| classTeacher=="杨子光" || classTeacher=="张云霞" || classTeacher=="武勇亮" || classTeacher=="高飞" || classTeacher=="孙静")) { alert("Teacher Error"); return; } if (classPlace[1] !== "教") { alert("Addr Error"); return; } let xmlHttp = new XMLHttpRequest(); xmlHttp.open("POST","addInfo",false); xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xmlHttp.send("className=" + className + "&classTeacher=" + classTeacher + "&classPlace=" + classPlace); } </script> </head> <body> <label for="className">课程名称:</label> <input type="text" id="className" name="className" required> <br> <label for="classTeacher">任课教师:</label> <input type="text" id="classTeacher" name="classTeacher" required> <br> <label for="classPlace">上课地点:</label> <input type="text" id="classPlace" name="classPlace" required><br> <button id="submitButton" onclick="submitButtonClick()">Go!</button> </body> </html> java1 package com.example.demo; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; 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; @WebServlet("/addInfo") public class AddInfoServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); Connection connection = null; try { String driver = "com.mysql.cj.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/class"; String user = "root"; String password = "123567"; Class.forName(driver); connection = DriverManager.getConnection(url, user, password); if (connection != null) { System.out.println("数据库连接成功!"); } else { System.out.println("数据库连接失败!"); } String className = request.getParameter("className"); String classTeacher = request.getParameter("classTeacher"); String classPlace = request.getParameter("classPlace"); System.out.println(className); System.out.printf(classTeacher); System.out.println(classPlace); String insertSQL = "INSERT INTO class2 (className, classTeacher, classPlace) VALUES (?, ?, ?)"; PreparedStatement preparedStatement = connection.prepareStatement(insertSQL); preparedStatement.setString(1, className); preparedStatement.setString(2, classTeacher); preparedStatement.setString(3, classPlace); int rowsAffected = preparedStatement.executeUpdate(); System.out.println(rowsAffected + " 行数据已插入。"); preparedStatement.close(); } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); } finally { try { if (connection != null) { connection.close(); } } catch (SQLException e) { e.printStackTrace(); } } } } java2 package com.example.demo; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class DatabaseConnection { public static Connection getConnection() throws SQLException, ClassNotFoundException { String driver = "com.mysql.cj.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/class"; String user = "root"; String password = "123567"; Class.forName(driver); return DriverManager.getConnection(url, user, password); } } java3 package com.example.demo; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; public class MySQLConnection { public static void main(String[] args) { Connection connection = null; try { String driver = "com.mysql.cj.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/class"; String user = "root"; String password = "123567"; Class.forName(driver); connection = DriverManager.getConnection(url, user, password); if (connection != null) { System.out.println("数据库连接成功!"); } else { System.out.println("数据库连接失败!"); } } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); } try { String insertSQL = "INSERT INTO class2 (className, classTeacher,classPlace) VALUES (?, ?,?)"; // 创建PreparedStatement对象 PreparedStatement preparedStatement = connection.prepareStatement(insertSQL); preparedStatement.setString(1, "Tm"); // 设置第一个参数值 preparedStatement.setString(2, "Tm"); // 设置第二个参数值 preparedStatement.setString(3, "Tm"); // 设置第三个参数值 int rowsAffected = preparedStatement.executeUpdate(); // 执行插入操作并返回受影响的行数 System.out.println(rowsAffected + " 行数据已插入。"); preparedStatement.close(); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (connection != null) { connection.close(); } } catch (SQLException e) { e.printStackTrace(); } } } }
标签:preparedStatement,java,String,10.9,数据库,半成品,connection,import,classTeacher From: https://www.cnblogs.com/xuxingkai/p/17753291.html