JSP文件:[/testYuYue.jsp] 的第 [39] 行发生了一个错误 The method setString(int, String) in the type PreparedStatement is not applicable for the arguments (int, int) 36: PreparedStatement pstmt = conn.prepareStatement(INSERT_SQL); { 37: pstmt.setString(1, usernum); 38: pstmt.setString(2, stime); 39: pstmt.setString(3,0); 40: pstmt.setString(4, people); 41: count = pstmt.executeUpdate(); 42: }} catch (SQLException e)
时间: 2024-02-27 21:55:13 浏览: 19
根据错误提示,第39行的问题是setString方法的参数类型不匹配。setString方法需要接收一个String类型的参数,但是你传入了一个int类型的参数0。因此,你需要将第39行的代码改为:
```
pstmt.setInt(3, 0);
```
或者将0改为字符串类型的"0":
```
pstmt.setString(3, "0");
```
这样就可以解决这个错误了。
相关问题
设计一个jsp文件,实现留言板功能
好的,留言板功能一般需要数据库的支持,我们先来设计一下数据库表。
假设我们需要存储留言的用户名、留言内容和留言时间,我们可以创建一个名为 message 的表,表结构如下:
```
CREATE TABLE message (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50),
content TEXT,
create_time DATETIME
);
```
接下来,我们可以创建一个 JSP 页面,实现留言板的功能。下面是一个简单的实现,代码中使用了 JSTL 和 EL 表达式。
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>留言板</title>
</head>
<body>
<h1>留言板</h1>
<h2>发表留言</h2>
<<form action="message.jsp" method="post">
<label for="username">用户名:</label>
<input type="text" name="username" id="username"><br>
<label for="content">留言内容:</label>
<textarea name="content" id="content" rows="5"></textarea><br>
<button type="submit">发表留言</button>
</form>
<hr>
<h2>留言列表</h2>
<c:forEach items="${messages}" var="message">
<p>${message.username} 于 ${message.create_time} 留言:</p>
<p>${message.content}</p>
<hr>
</c:forEach>
</body>
</html>
```
在 JSP 页面中,我们使用了一个表单来提交留言。表单中包含了用户名和留言内容两个字段,并且使用了 POST 方法提交表单。
在页面的下半部分,我们使用了 JSTL 的 forEach 标签来循环遍历留言列表,使用 EL 表达式来显示留言的用户名、留言时间和留言内容。
接下来,我们需要编写 message.jsp 文件来处理表单提交和查询留言的逻辑。下面是一个简单的实现。
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page import="java.sql.*,java.util.*" %>
<%
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/test";
String user = "root";
String password = "123456";
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
if (request.getMethod().equals("POST")) {
String username = request.getParameter("username");
String content = request.getParameter("content");
Date create_time = new Date();
stmt = conn.prepareStatement("INSERT INTO message(username, content, create_time) VALUES (?, ?, ?)");
stmt.setString(1, username);
stmt.setString(2, content);
stmt.setTimestamp(3, new Timestamp(create_time.getTime()));
stmt.executeUpdate();
}
stmt = conn.prepareStatement("SELECT * FROM message ORDER BY create_time DESC");
rs = stmt.executeQuery();
List<Map<String, Object>> messages = new ArrayList<>();
while (rs.next()) {
Map<String, Object> message = new HashMap<>();
message.put("username", rs.getString("username"));
message.put("content", rs.getString("content"));
message.put("create_time", rs.getTimestamp("create_time"));
messages.add(message);
}
request.setAttribute("messages", messages);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
}
%>
<jsp:forward page="message_board.jsp"></jsp:forward>
```
在 message.jsp 文件中,我们首先定义了数据库的连接参数,并且使用 JDBC 连接到数据库。然后,我们通过 request.getMethod() 方法判断当前请求的方法是否为 POST,如果是 POST 方法,就获取表单提交的用户名、留言内容和留言时间,并且插入到数据库的 message 表中。
接下来,我们通过执行 SQL 查询语句,获取留言列表,并且将留言列表保存到一个 List<Map<String, Object>> 对象中。最后,我们使用 request.setAttribute() 方法将留言列表保存到 request 对象中,并且通过 jsp:forward 标签跳转回到 message_board.jsp 页面。在 message_board.jsp 页面中,我们就可以使用 JSTL 和 EL 表达式来显示留言列表了。
编写程序,实现对数据库StudentInfo中的表student进行插入操作.要求使用预处理语句对象PreparedStatement完成以下数据的插入: insert into student values('001','张三',18,'女') 注:给定数据库登录用户名:sa,密码:123. Input.jsp: 请输入待新增的学生信息 学号: 姓名: 年龄: 性别: addStudent.jsp: <% String url="jdbc:sqlserver://localhost;databaseName=StudentInfo"; request.setCharacterEncoding("utf-8"); String sno = request.getParameter("sno"); String sname = request.getParameter("sname"); int sage = Integer.valueOf(request.getParameter("sage")); String sex = request.getParameter("sex"); try{ //补全此部分代码 } // 捕获异常 catch(SQLException ex) { System.out.println ("\n*** 发生SQL异常 ***\n"+ex.getMessage()); } catch(ClassNotFoundException ex) { System.out.println(ex); }
addStudent.jsp:
```
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Add Student</title>
</head>
<body>
<h1>Add Student</h1>
<form action="insertStudent.jsp" method="post">
<label for="sno">学号:</label>
<input type="text" id="sno" name="sno" /><br>
<label for="sname">姓名:</label>
<input type="text" id="sname" name="sname" /><br>
<label for="sage">年龄:</label>
<input type="text" id="sage" name="sage" /><br>
<label for="sex">性别:</label>
<input type="text" id="sex" name="sex" /><br>
<input type="submit" value="提交" />
</form>
</body>
</html>
```
insertStudent.jsp:
```
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert Student</title>
</head>
<body>
<%
String url="jdbc:sqlserver://localhost;databaseName=StudentInfo";
request.setCharacterEncoding("utf-8");
String sno = request.getParameter("sno");
String sname = request.getParameter("sname");
int sage = Integer.valueOf(request.getParameter("sage"));
String sex = request.getParameter("sex");
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection(url,"sa","123");
String sql = "insert into student values(?,?,?,?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, sno);
pstmt.setString(2, sname);
pstmt.setInt(3, sage);
pstmt.setString(4, sex);
int num = pstmt.executeUpdate();
if(num > 0){
out.print("插入成功!");
}
pstmt.close();
conn.close();
}
catch(SQLException ex){
System.out.println ("\n*** 发生SQL异常 ***\n"+ex.getMessage());
}
catch(ClassNotFoundException ex){
System.out.println(ex);
}
%>
</body>
</html>
```