1

我想insert在查询中使用该语句,但输出显示Unable to connect database. 我使用这种形式:

<form class="form-signin" action="CreateTeam.jsp">
                <h2 class="form-signin-heading">Create new team</h2>
                <input type="text" class="input-block-level" placeholder="Team name" name="name" id="name" required />
                <input type="text" class="input-block-level" placeholder="Team description" name="desc" id="desc" required />
                <button class="btn btn-large btn-primary" type="submit" >Create!</button>
                </form>

我的jsp代码是:

<%
                String name = request.getParameter("name");
                String desc = request.getParameter("desc");

                Connection connection = null;
                PreparedStatement pstatement = null;
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                int updateQuery = 0;
                if (name != null && desc != null) {
                    if (name != "" && desc != "") {

                        try {
                            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
                            String queryString1 = "insert into teams (name,desc) values (?, ?)";
                            pstatement = connection.prepareStatement(queryString1);
                            pstatement.setString(1, name);
                            pstatement.setString(2, desc);
                            updateQuery = pstatement.executeUpdate();
                            if (updateQuery != 0) {
                                response.sendRedirect("../AdministrationControlPanel.jsp");
                            }
                        } catch (Exception ex) {
                            out.println("Unable to connect to database.");

                        } finally {
                            pstatement.close();
                            connection.close();
                        }
                    }
                }
            %>

数据库test在本地主机上运行。我正在修改它,但我不知道问题出在哪里。

4

2 回答 2

1

DESC,在您的一个专栏中,是 MySQL 保留关键字。您应该使用反引号将其转义,

INSERT INTO teams (name,`desc`) values (?, ?)

为了避免将来出现问题,最好避免使用 MySQL 保留关键字的关键字。

于 2013-01-30T14:36:11.790 回答
1

descMySQL你必须引用的关键字。

于 2013-01-30T14:37:00.137 回答