数据库类型是number 代码取出来是long 怎么判空
时间: 2024-03-13 13:46:19 浏览: 17
如果你想判断一个 long 类型的数据库字段是否为空,你可以使用以下代码:
```
Long value = resultSet.getLong("columnName");
if (resultSet.wasNull()) {
// 处理空值的情况
} else {
// 处理非空值的情况
}
```
在上面的代码中,我们首先使用 `getLong` 方法获取 long 类型的值,然后使用 `wasNull` 方法判断该值是否为 null。如果该值为 null,则 `wasNull` 方法返回 true,否则返回 false。所以,我们可以根据 `wasNull` 方法的返回值来判断该字段是否为空。
相关问题
java写一个接收前端表单数据写入数据库加上前端的代码
以下是一个简单的Java Servlet示例,可以接收前端表单数据,将其写入MySQL数据库,并返回一个确认页面给前端。
Java Servlet代码:
import java.io.IOException;
import java.io.PrintWriter;
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("/AddDataServlet")
public class AddDataServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取前端表单数据
String name = request.getParameter("name");
String age = request.getParameter("age");
String gender = request.getParameter("gender");
//将数据写入MySQL数据库
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password");
PreparedStatement ps = conn.prepareStatement("INSERT INTO user (name, age, gender) VALUES (?, ?, ?)");
ps.setString(1, name);
ps.setString(2, age);
ps.setString(3, gender);
ps.executeUpdate();
ps.close();
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
//返回确认页面给前端
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>确认页面</title></head>");
out.println("<body>");
out.println("<h1>添加成功!</h1>");
out.println("<p>姓名:" + name + "</p>");
out.println("<p>年龄:" + age + "</p>");
out.println("<p>性别:" + gender + "</p>");
out.println("</body>");
out.println("</html>");
}
}
前端代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加用户</title>
</head>
<body>
<h1>添加用户</h1>
<form action="AddDataServlet" method="post">
<label>姓名:</label><input type="text" name="name"><br>
<label>年龄:</label><input type="number" name="age"><br>
<label>性别:</label>
<input type="radio" name="gender" value="男">男
<input type="radio" name="gender" value="女">女<br>
<input type="submit" value="提交">
</form>
</body>
</html>
写一个javaweb代码,在jsp页面提交账号密码后,存储在数据表里,数据库名是stu,数据表名为stu_account,账号对应的属性为account_number,密码对应的属性为password
好的,以下是代码示例:
1. 创建stu数据库和stu_account数据表
首先需要在MySQL数据库中创建名为stu的数据库,然后在该数据库中创建名为stu_account的数据表,代码如下:
```sql
CREATE DATABASE stu;
USE stu;
CREATE TABLE stu_account (
id INT(11) NOT NULL AUTO_INCREMENT,
account_number VARCHAR(20) NOT NULL,
password VARCHAR(20) NOT NULL,
PRIMARY KEY (id)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
```
2. 创建JSP页面
在Web项目的WebContent目录下创建一个名为register.jsp的JSP页面,代码如下:
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册页面</title>
</head>
<body>
<form action="register" method="post">
账号:<input type="text" name="account_number" /><br />
密码:<input type="password" name="password" /><br />
<input type="submit" value="注册" />
</form>
</body>
</html>
```
3. 创建Servlet
在Java项目的src目录下创建一个名为RegisterServlet的Servlet,代码如下:
```java
package com.example.servlet;
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("/register")
public class RegisterServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String accountNumber = request.getParameter("account_number");
String password = request.getParameter("password");
// 数据库连接信息
String url = "jdbc:mysql://localhost:3306/stu";
String username = "root";
String userpassword = "123456";
Connection conn = null;
PreparedStatement stmt = null;
try {
// 加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
// 获取数据库连接
conn = DriverManager.getConnection(url, username, userpassword);
// 构造SQL语句
String sql = "INSERT INTO stu_account(account_number, password) VALUES (?, ?)";
// 创建PreparedStatement对象
stmt = conn.prepareStatement(sql);
// 设置SQL参数
stmt.setString(1, accountNumber);
stmt.setString(2, password);
// 执行SQL语句
stmt.executeUpdate();
// 跳转到注册成功页面
response.sendRedirect("register_success.jsp");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
response.getWriter().println("注册失败:" + e.getMessage());
} finally {
// 关闭数据库连接和PreparedStatement对象
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
```
4. 创建注册成功页面
在Web项目的WebContent目录下创建一个名为register_success.jsp的JSP页面,用于显示注册成功的信息,代码如下:
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册成功</title>
</head>
<body>
<h1>注册成功!</h1>
</body>
</html>
```
5. 部署Web项目
将以上代码保存后,将Web项目部署到Web服务器中即可。在浏览器中访问register.jsp页面,输入账号和密码,点击注册按钮,会将数据存储到stu数据库中的stu_account数据表中。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)