用JAVA+jdbc+Mysql实现网页跳转

前言

这是一份作业

Mysql

建立数据库
在这里插入图片描述

jdbc

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class jdkc_text {
   
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
   
        //输入psvm或main后回车
        //1.导入jar包
        //2.注册驱动
        Class.forName("com.mysql.jdbc.Driver");//ALT+回车

        //3.获取数据库连接对象 Connection
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/testtt","root","123456");

        //4.定义sql
        String sql = "upadte user set name = 'ZUTTER' where id = 2";

        //5.获取执行sql的对象 Statement
        Statement statement = connection.createStatement();

        //6.执行sql语句 接收返回结果
        int count = statement.executeUpdate(sql);

        //7.处理结果
        System.out.println(count);

        //8.释放资源
        statement.close();
        connection.close
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用HTML+CSS+JS+MySQL+JDBC+Servlet等技术实现在HTML页面上的登录功能,你可以按照以下步骤进行操作: 1. 首先,创建一个HTML登录页面。在HTML中,你可以使用`<form>`元素创建一个表单,并在表单中添加输入框和提交按钮。例如: ```html <form action="loginServlet" method="POST"> <label for="username">用户名:</label> <input type="text" id="username" name="username" required><br><br> <label for="password">密码:</label> <input type="password" id="password" name="password" required><br><br> <input type="submit" value="登录"> </form> ``` 2. 创建一个Servlet来处理登录请求。在Servlet中,你可以使用JDBC连接数据库,并执行登录验证的逻辑。例如,你可以在`doPost()`方法中获取用户输入的用户名和密码,然后查询数据库验证登录信息。 ```java @WebServlet("/loginServlet") public class LoginServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); // 使用JDBC连接数据库,执行登录验证的逻辑 // ... } } ``` 3. 在Servlet中,你可以使用JDBC连接MySQL数据库。首先,你需要导入JDBC驱动程序,并在`doPost()`方法中编写数据库连接和查询的代码。例如: ```java import java.sql.*; @WebServlet("/loginServlet") public class LoginServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); Connection connection = null; PreparedStatement statement = null; ResultSet resultSet = null; try { // 加载JDBC驱动程序 Class.forName("com.mysql.jdbc.Driver"); // 建立数据库连接 connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "username", "password"); // 执行查询语句 String query = "SELECT * FROM users WHERE username=? AND password=?"; statement = connection.prepareStatement(query); statement.setString(1, username); statement.setString(2, password); resultSet = statement.executeQuery(); if (resultSet.next()) { // 登录成功 response.sendRedirect("welcome.html"); } else { // 登录失败 response.sendRedirect("login.html"); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { // 关闭数据库连接和资源 try { if (resultSet != null) resultSet.close(); if (statement != null) statement.close(); if (connection != null) connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } } ``` 在上述代码中,你需要将`jdbc:mysql://localhost:3306/mydatabase`替换为你的MySQL数据库的连接URL,以及`username`和`password`为你的数据库的用户名和密码。 4. 创建一个`welcome.html`页面作为登录成功后的页面。 这样,当用户在HTML页面上输入用户名和密码并点击登录按钮后,表单数据会被提交到Servlet进行处理。Servlet会连接数据库验证用户信息,并根据验证结果进行相应的页面

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值