title: inserting data through a servlet using JDBC
body: <p>I am trying to make a Social network site where a user can log or register. Once logged in with correct credentials, the user could search in a friends list and invite him/her to be friend.</p>
  
  <p>So far I have made 2 jsp files-
  <a href="http://i.stack.imgur.com/iVpe2.jpg" rel="nofollow">1</a> index.jsp [2] UserRegistration.jsp
  [2]mySQl has SocialNetwork Schema with UserInfo and userRegistration tables.</p>
  
  <p>[3] and in src folder my RegistrationServlet is like this:</p>
  
  <pre><code>// ********************************************************************  
      // JDBC driver name and database URL  
      final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
      final String DB_URL = "jdbc:mysql://localhost:3306/SocialNetwork";  
      // ***********************************************************************  
      // Database credentials  
      final String USER = "root";  
      final String PASS = "root";  
      Connection conn = null;  
      Statement stmt = null;  
      try {  
          // Register JDBC driver  
          Class.forName(JDBC_DRIVER);  
  
  
          // connection to mySQL server and  Open a connection  
          System.out.println("Connecting to a selected database...");  
          conn = DriverManager.getConnection(DB_URL, USER, PASS);  
          System.out.println("Connected database successfully...");  
  
          // create a Statement object ()  
          System.out.println("Inserting records into the table...");  
          stmt = conn.createStatement();  
  
  
          //  Execute a query  
          String sql = "INSERT INTO SocialNetwork.userInfo"  
                  + "(userName, userPW)" + "VALUES('" + nUser + "','" + nPW  
                  + "')";  
          int i = stmt.executeUpdate(sql);  
          out.println("Connection successfull.." + i);  
  
          //Update the Db  
          stmt.executeUpdate(sql);  
  
                     // executeQuery () method and save it on ResultSet  
  
          sql = "SELECT * FROM SocialNetwork.userInfo";  
  
          ResultSet rs = stmt.executeQuery(sql);  
  
  
          // check inserted rows in the Db table  
          while (rs.next()) {  
              String Db_Name = rs.getString("userName");  
              String passwordFromDb = rs.getString("userPW");  
              // rs.getString(newPassW)  
              out.println("&lt;br&gt;DbName **" + Db_Name);  
              out.println("-&gt;DbPass  **" + passwordFromDb);  
  
          }  
  
      } catch (Exception e) {  
          e.printStackTrace();  
      }  
  </code></pre>
  
  <p>How could I connect the UserLogin servlet to the UserInfo table so that if the user’s name and password are found and matched in the DB , he could see his/her friends’ list. </p>
  
  <p>If that username and password is not found in the UserInfo, a message should display that the user has to Register first. </p>
  
  <p>Once the user is logged-in, he should see a list of all the Usernames and Age. 
  Later, the user could select a user from that list and invite that user to be his/her friend. </p>