首页 > 解决方案 > 如何使用 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>

标签: javaapijdbc

解决方案


与其让我们跟着你一起去寻找你的堆栈跟踪记录在哪里,只需将它添加到你的 catch 块中。这将是目前实际向您显示错误消息的最快途径。

    } catch (SQLException e) {
      System.out.format("SQL State: %s\n%s\n", e.getSQLState(), e.getMessage());
    ...
    ...
    }

推荐阅读