i want to use the insert statement in my query, but the output shows Unable to connect database.
I use this form:
<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>
And my jsp code is:
<%
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();
}
}
}
%>
database test is running on localhost. I´m modifying it, but i dont know, where is the problem.
DESC, in one of your columns, is a MySQL Reserved Keyword. You should escape it using backtick,To avoid future problems, it is much better to avoid keywords that are MySQL’s Reserved keywords.