JSP技术可以轻易地将用户的输入信息存储到后台的MySQL数据库中。下面是一个用于实现该功能的示例程序:
<%@ page import="java.sql.*" %><%@ page import="java.io.*" %><%String url = "jdbc:mysql://localhost:3306/example";String username = "root";String password = "";String driver = "com.mysql.jdbc.Driver";Connection con = null;PreparedStatement ps = null;try {// Load MYSQL driverClass.forName(driver);// Establish a connectioncon = DriverManager.getConnection(url, username, password);// Get input values from the userString name = request.getParameter("name");String phone = request.getParameter("phone");String email = request.getParameter("email");// Insert values into databaseString query = "INSERT INTO customers (name, phone, email) VALUES (?, ?, ?)";ps = con.prepareStatement(query);ps.setString(1, name);ps.setString(2, phone);ps.setString(3, email);ps.executeUpdate();// Print success messageout.println("Record has been added successfully!");} catch (Exception e) {out.println(e.getMessage());} finally {try {if (ps != null) {ps.close();}if (con != null) {con.close();}} catch (SQLException e) {out.println(e.getMessage());}}%>
在上述代码中,我们首先导入了java.sql.*和java.io.*两个包,然后建立了与mysql的链接,再获取用户输入信息并实现将数据插入到数据库中的操作,最后打印插入成功的消息。