Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to rectify the database connectivity error in jsp?
    text
    copied!<p>i wrote a code jsp to store the employee details in the database . i created a database called employee and table empdetails in that employee database. i deployed the code in the tomcat server. but the data that am giving in the concern fields are not getting stored in the database table empdetails.</p> <p>can anyone help me out.</p> <p>the code is as follows</p> <pre><code>&lt;%@ page language="java" pageEncoding="ISO-8859-1"%&gt; &lt;%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%&gt; &lt;%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%&gt; &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" &gt; &lt;%@ page import="java.sql.*"%&gt; &lt;%@ page import="java.io.*"%&gt; &lt;HTML&gt; &lt;HEAD&gt; &lt;TITLE&gt;welcome to City Solutions &lt;/TITLE&gt; &lt;/HEAD&gt; &lt;BODY bgcolor="#ffffcc"&gt; &lt;font size="+3" color="green"&gt;&lt;br&gt;Welcome to City Solutions !&lt;/font&gt; &lt;br&gt; Employee Registration.. &lt;FORM action="prepared_statement_query.jsp" method="get"&gt; &lt;TABLE style="background-color: #ECE5B6;" WIDTH="30%"&gt; &lt;TR&gt; &lt;TH width="50%"&gt; Name &lt;/TH&gt; &lt;TD width="50%"&gt; &lt;INPUT TYPE="text" NAME="name"&gt; &lt;/TD&gt; &lt;/tr&gt; &lt;TR&gt; &lt;TH width="50%"&gt; City &lt;/TH&gt; &lt;TD width="50%"&gt; &lt;INPUT TYPE="text" NAME="city"&gt; &lt;/TD&gt; &lt;/tr&gt; &lt;TR&gt; &lt;TH width="50%"&gt; Phone &lt;/TH&gt; &lt;TD width="50%"&gt; &lt;INPUT TYPE="text" NAME="phone"&gt; &lt;/TD&gt; &lt;/tr&gt; &lt;TR&gt; &lt;TH width="50%"&gt; Qualification &lt;/TH&gt; &lt;TD width="50%"&gt; &lt;INPUT TYPE="text" NAME="Qualification"&gt; &lt;/TD&gt; &lt;/tr&gt; &lt;TR&gt; &lt;TH width="50%"&gt; Year &lt;/TH&gt; &lt;TD width="50%"&gt; &lt;select name="year"&gt; &lt;option value="select"&gt; select &lt;/option&gt; &lt;option value="2011"&gt; 2011 &lt;/option&gt; &lt;option value="2010"&gt; 2010 &lt;/option&gt; &lt;option value="2009"&gt; 2009 &lt;/option&gt; &lt;option value="2008"&gt; 2008 &lt;/option&gt; &lt;option value="2007"&gt; 2007 &lt;/option&gt; &lt;option value="2006"&gt; 2006 &lt;/option&gt; &lt;/select&gt; &lt;/TD&gt; &lt;/tr&gt; &lt;TR&gt; &lt;TH width="50%"&gt; Experience &lt;/TH&gt; &lt;TD width="50%"&gt; &lt;INPUT size="4" TYPE="text" NAME="Experience"&gt; &lt;/TD&gt; &lt;/tr&gt; &lt;TR&gt; &lt;TH width="50%"&gt; Position &lt;/TH&gt; &lt;TD width="50%"&gt; &lt;select name="Position"&gt; &lt;option value="select"&gt; select &lt;/option&gt; &lt;option value="Java"&gt; JAVA &lt;/option&gt; &lt;option value="Testing"&gt; TESTING &lt;/option&gt; &lt;option value="ETL"&gt; ETL &lt;/option&gt; &lt;option value="BA"&gt; BA &lt;/option&gt; &lt;/select&gt; &lt;/TD&gt; &lt;/tr&gt; &lt;TR&gt; &lt;TH&gt;&lt;/TH&gt; &lt;TD width="50%"&gt; &lt;INPUT TYPE="submit" VALUE="submit"&gt; &lt;/TD&gt; &lt;/tr&gt; &lt;/TABLE&gt; &lt;% String name = request.getParameter("name"); String city = request.getParameter("city"); String phone = request.getParameter("phone"); String qualification = request.getParameter("qualification"); String year = request.getParameter("year"); System.out.println("year" + year); String experience = request.getParameter("experience"); String position = request.getParameter("position"); /* Create string of connection url within specified format with machine name, port number and database name. Here machine name id localhost and database name is student. */ String connectionURL = "jdbc:mysql://localhost:3306/employee"; // declare a connection by using Connection interface Connection connection = null; // declare object of Statement interface that uses for // executing sql statements. PreparedStatement pstatement = null; // Load JBBC driver "com.mysql.jdbc.Driver" Class.forName("com.mysql.jdbc.Driver").newInstance(); int updateQuery = 0; // check if the text box is empty //if(name!=null &amp;&amp; city!=null &amp;&amp; phone!=null){ // check if the text box having only blank spaces if (name != "" &amp;&amp; city != "" &amp;&amp; phone != "" &amp;&amp; qualification != "" &amp;&amp; year != "" &amp;&amp; experience != "" &amp;&amp; position != "") { try { /* Create a connection by using getConnection() method that takes parameters of string type connection url, user name and password to connect to database. */ connection = DriverManager.getConnection(connectionURL, "root", "root"); // sql query to insert values in the specified table. String queryString = "INSERT INTO empdetails(Name,City,Phone,Qualification,Year,Experience,Position) VALUES (?, ?, ?, ?, ?, ?, ?)"; /* createStatement() is used for create statement object that is used for sending sql statements to the specified database. */ pstatement = connection.prepareStatement(queryString); pstatement.setString(1, name); pstatement.setString(2, city); pstatement.setString(3, phone); pstatement.setString(4, qualification); pstatement.setString(5, year); pstatement.setString(6, experience); pstatement.setString(7, position); updateQuery = pstatement.executeUpdate(); if (updateQuery != 0) { %&gt; &lt;br&gt; &lt;TABLE style="background-color: #E3E4FA;" WIDTH="50%" border="1"&gt; &lt;tr&gt; &lt;th&gt; Your Information Has to be Stored &lt;/th&gt; &lt;/tr&gt; &lt;/table&gt; &lt;% } } catch (Exception ex) { out.println("Unable to connect to Database."); } finally { // close all the connections. //pstatement.close(); //connection.close(); } } %&gt; &lt;/FORM&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload