首页 > 数据库 >10.9(数据库,半成品)

10.9(数据库,半成品)

时间:2023-10-09 22:13:33浏览次数:43  
标签:preparedStatement java String 10.9 数据库 半成品 connection import classTeacher

今天下午连接数据库,半成品

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

相关文章

  • 2023.10.9
    今天java学习了连接数据库,首先打开MYSQL的官网 解压到某个目录下,然后在根目录下创建一个txt文件,名字叫my,文件后缀为ini,需要按照下一步操作。[mysqld]#设置3306端口port=3306#设置mysql的安装目录----------是你的文件路径-------------basedir=E:\mysql\mysql#设置mysq......
  • luffy前端配置、跨域问题、后端数据库迁移、后台主页功能、后台管理
    luffy前端配置#1跟后端交互:axioscnpminstall-Saxios以后想发送ajax请求,必须导入,使用可以把axios放到vue实例中,以后任意组件中this.$axios.get()main.js中加入importaxiosfrom'axios'Vue.prototype.$axios=axios;#2操作cookie:vue......
  • 数据库事务的四大特性(ACID)
    ACIDACID是数据库事务的四个关键特性,分别是原子性(Atomicity)、一致性(Consistency)、隔离性(Isolation)和持久性(Durability)。原子性(Atomicity):解释:事务是一个原子操作单元,要么全部执行成功,要么全部失败回滚,不存在部分执行的情况。例子:考虑银行转账操作,从一个账户扣除一定金......
  • 10.9
    上午学习了左程云算法,但是没学完,太长了太累了,做了一道力扣下午把左程云学完了,然后开始看headfirstJava,全是什么多态,重载之类的,例子又比较少,看的想吐,枯燥,打算这一部分知识还是看视频吧晚上拼积木拼到七点半,终于拼完了。然后把昨天的力扣每日一题重新做了一遍,然后开始看fedavg论......
  • 10.9日
    一、虚拟参与了一场cf比赛,完成了两道题,第三题有点难,赛后补题。这里来一个题解链接。https://blog.csdn.net/weixin_73550568/article/details/133713779?spm=1001.2014.3001.5501二、电子产品的工程实训,会焊电路板了。三、完成了一个录入课程信息的springboot项目。四、打一场......
  • 10.9测验
     ......
  • 2023.10.9——每日总结
    学习所花时间(包括上课):0h代码量(行):0行博客量(篇):1篇今天,上午学习,下午学习;我了解到的知识点:1.DIV+CSS;明日计划:学习......
  • 锁表的原理是 数据库使用独占式封锁机制,当执行上面的语句时
    3.1、A程序执行了对tableA的insert,并还未commite时,B程序也对tableA进行insert则此时会发生资源正忙的异常就是锁表;3.2、锁表常发生于并发而不是并行(并行时,一个线程操作数据库时,另一个线程是不能操作数据库的,cpu和i/o分配原则)4、减少锁表的概率:减少insert、update、......
  • uniCloud-用db schema在客户端访问数据库
    先下载所有dbschema在前端代码中直接访问数据库表拿数据index.vue<template> <viewclass="content"> <viewv-for="itemindataList":key="item._id"> {{item.name}}---{{item.gender}}---{{item.age}} </view> <......
  • MySQL数据库被锁表你有遇到过吗?
    1.被锁原因1、锁表发生在insertupdate、delete中;2、锁表的原理是数据库使用独占式封锁机制,当执行上面的语句时,对表进行锁住,直到发生commite或者回滚或者退出数据库用户;3、锁表的原因:3.1、A程序执行了对tableA的insert,并还未commite时,B程序也对tableA进行insert......