java - 如何使用 Java API 将数据插入数据库?
问题描述
我正在尝试使用以下代码将数据插入数据库,但有些东西无法正常工作。它运行没有错误,但它没有向数据库中插入任何内容。
public Map<String,String> createuser(String handle, String password, String fullname, String location, String xmail, String bdate)
{
Map<String,String> userIdMap = new HashMap<>();
PreparedStatement stmt = null;
try
{
Connection conn = ds.getConnection();
String queryString = null;
queryString = "INSERT INTO Identity(handle, password, fullname, location, xmail, bdate) VALUES (?, ?, ?, ?, ?, ?)";
stmt = conn.prepareStatement(queryString);
stmt.setString(1, handle);
stmt.setString(2, password);
stmt.setString(3, fullname);
stmt.setString(4, location);
stmt.setString(5, xmail);
stmt.setString(6, bdate);
userIdMap.put(handle, password);
userIdMap.put(fullname, location);
userIdMap.put(xmail, bdate);
int s = stmt.executeUpdate(queryString);
stmt.close();
conn.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
return userIdMap;
} // createuser()
可能的错误信息
<html><head><title>Grizzly 2.3.23</title><style><!--div.header {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#003300;font-size:22px;-moz-border-radius-topleft: 10px;border-top-left-radius: 10px;-moz-border-radius-topright: 10px;border-top-right-radius: 10px;padding-left: 5px}div.body {font-family:Tahoma,Arial,sans-serif;color:black;background-color:#FFFFCC;font-size:16px;padding-top:10px;padding-bottom:10px;padding-left:10px}div.footer {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#666633;font-size:14px;-moz-border-radius-bottomleft: 10px;border-bottom-left-radius: 10px;-moz-border-radius-bottomright: 10px;border-bottom-right-radius: 10px;padding-left: 5px}BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;}B {font-family:Tahoma,Arial,sans-serif;color:black;}A {color : black;}HR {color : #999966;}--></style> </head><body><div class="header">Request failed.</div><div class="body">Request failed.</div><div class="footer">Grizzly 2.3.23</div></body></html>
解决方案
与其让我们跟着你一起去寻找你的堆栈跟踪记录在哪里,只需将它添加到你的 catch 块中。这将是目前实际向您显示错误消息的最快途径。
} catch (SQLException e) {
System.out.format("SQL State: %s\n%s\n", e.getSQLState(), e.getMessage());
...
...
}
推荐阅读
- parsing - 如何在我的词法分析器/解析器中支持不同的语言版本
- java - 如何通过 Intent 初始化 Android DatePicker?
- git - 变基后丢失的更改,如何安全地将更改从本地主机推送到远程/源主机
- node.js - 如何将node应用程序打包成windows exe应用程序
- node.js - 当我有 3 个 package.json 时,npm 只安装两个 package.json
- vaadin - Vaadin:如何更新图表的数据?
- mysql - 如何将数据插入从单个表单接收的多个数据库表中?Django/MySql
- node.js - mongoose:对 $project 中的字段进行排序和分页
- javascript - 使用jQuery更改输入数字字段时防止提交表单
- javascript - 简单的 html 复制和粘贴问题 - 也复制按钮