gpt4 book ai didi

java - MySQL 和 JSP 数据库与 Glassfish 服务器 4 的连接

转载 作者:行者123 更新时间:2023-11-29 06:38:41 25 4
gpt4 key购买 nike

这是我的代码。

    **insert.html**
<html>
<head>
<title>Donate Blood !</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="store.jsp" method="POST">
Name: <input type="text" name="nam"><br>
Rollno: <input type="text" name="rno"><br>
Blood Group: <input type="text" name="grp"><br>
<input type="submit" value="Add me !">
</form>
</body>
</html>

**Store.jsp**
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%
String name = request.getParameter("nam");
String roll = request.getParameter("rno");
String group = request.getParameter("grp");

Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/blood","root","");
Statement st = con.createStatement();
int i=st.executeUpdate("insert into blood(Name,Rollno,BloodGroup) values('+name+','+roll+','+group+')");
%>

我的数据库.. enter image description here

我在 netbeans 8.0 中运行这段代码。我得到的错误日志是 enter image description here

它表明数据对于血型来说太长了,但我只是在该文本框中给出了“A+”。怎么了。由于我是 JSP 的新手,请帮助我。提前致谢。

最佳答案

尝试使用 PreparedStatement 来避免字符串连接错误。

PreparedStatement st = con.prepareStatement("insert into blood(Name,Rollno,BloodGroup) values(?,?,?)");
st.setString(1, name);
st.setString(2, roll);
st.setString(3, group);
st.executeUpdate();

关于java - MySQL 和 JSP 数据库与 Glassfish 服务器 4 的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22902065/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com