Monday 24 October 2016

Fetch Data From Database Using Java and Ajax


Hi friends, I think you all know how to fetch data from databse using Servlet, but some time you need to fetch data in another way like if insert into text and press the Enter key then data should me come into other text fields.

So I am giving example here. learn and enjoy this code

1.Jsp page

Here i have taken four input type text,
See Book Id,there is onKeyup property means when you enter into Book ID text and then press the Enter button then you will get data form database in to others fields.

onkeyup="get(this.value,<%=session.getAttribute("sid")%>)"

here this.value  means it will take enter value and ,<%=session.getAttribute("sid")%> means it will take id of your product, it is not necessary if your product is only one login

 <label class="text-danger"><strong>BOOK ID :</strong></label> <input type="text" name="lib_bookID"  id="lib_bookID"onkeyup="get(this.value,<%=session.getAttribute("sid")%>)" />
                    
                    
<label class="text-danger"><strong>AUTHOR NAME :</strong></label> <input type="text" style="color: green;"
name="lib_author" id="lib_author"/>
                          

<label class="text-danger"><strong>BOOK NAME :</strong></label> <input type="text" name="lib_book_name" id="lib_book_name"/>
                                                                                                             
<label class="text-danger"><strong>PUBLICATION NAME :</strong></label> <input type="text" name="publication_book" id="publication_book" />
                                               

2. Ajax


xmlhttp.open("GET","Getdata?book_number="+book_number + "&sid="+sid ,true);

see here, I have taken two parameter, because I have two login for different products.

And also check url ,this url must match with servlet url

<script>
       
        function get(book_number,sid)
               {
                   var xmlhttp=new XMLHttpRequest();
                   xmlhttp.onreadystatechange=function()
                     {
                     if (xmlhttp.readyState==4 && xmlhttp.status==200)
                       {
                       
 var responseArray = xmlhttp.responseText.split(",");
                         document.getElementById("lib_book_name").value=responseArray[0];
                         document.getElementById("lib_author").value=responseArray[1];
                         document.getElementById("publication_book").value=responseArray[2];
                       }
                     };
                    
                  
                   xmlhttp.open("GET","Getdata?book_number="+book_number + "&sid="+sid ,true);
                   xmlhttp.send();

               }
        </script>

3. Servlet code
package com.bookdetails;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.mail.Session;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dbActivity.JDBCUtil;
@WebServlet("/Getdata")
public class Getdata extends HttpServlet {
       private static final long serialVersionUID = 1L;
       public Getdata() {
              super();
                     }

      
       protected void doGet(HttpServletRequest request,
                     HttpServletResponse response) throws ServletException, IOException {
             
String book_number = request.getParameter("book_number");
String sid=request.getParameter("sid");
      
      

              System.out.println(book_number);
              System.out.println(sid);

              String book, auth, puslh;
              try {

                     Connection con = JDBCUtil.getConnection();
       // here create connection according to youJDBCUtil is my connection class

       
                     PreparedStatement ps = con
                                  .prepareStatement("SELECT BOOK_NAME,AUTHOR_NAME,PUBLISHER_NAME from BOOK_REGISTRATION where BOOK_NUMBER=? and SID=?");
                     ps.setString(1, book_number);
                     ps.setString(2, sid);
                     ResultSet rs = ps.executeQuery();
                     if (rs.next()) {
                           book = rs.getString("BOOK_NAME");
                           auth = rs.getString("AUTHOR_NAME");
                           puslh = rs.getString("PUBLISHER_NAME");
                     } else {
                           book = "";
                           auth = "";
                           puslh = "";
                     }
                     response.getWriter().write(book + "," + auth + "," + puslh);

                    
              } catch (Exception e) {

                     e.printStackTrace();
              }

       }

       protected void doPost(HttpServletRequest request,
                     HttpServletResponse response) throws ServletException, IOException {

       }

}

If any query then put your questions into comment box.

Thanks
Neeraj Srivastava

Happy Learning

Friday 14 October 2016

How to get rid of “maximum user connections” error ?

1.Run this query

SELECT max_user_connections FROM mysql.user
WHERE user='db_user' AND host='localhost';

(in user after where plz write your db username,all will be same)

If this is a nonzero value, change it back with:

GRANT USAGE ON *.* TO db_user@localhost MAX_USER_CONNECTIONS 0;
or

UPDATE mysql.user SET max_user_connections = 0
WHERE user='db_user' AND host='localhost';

FLUSH PRIVILEGES;



Once you get to this point, now check the global setting using

SHOW VARIABLES LIKE 'max_user_connections';




If this is a nonzero value, you need to do two things

THING #1 : Look for the setting in /etc/my.cnf

[mysqld]
max_user_connections = <some number>
comment that line out

THING #2 : Set the value dynamically

SET GLOBAL max_user_connections = 0;

MySQL restart is not required





//increate the connection

UPDATE mysql.user SET
max_connections = 1000
WHERE user='myuser' AND host='localhost';
FLUSH PRIVILEGES;


To set the maximum number of queries per hour at 1000 on a given connection do this:

UPDATE mysql.user SET
max_questions = 1000
WHERE user='myuser' AND host='localhost';
FLUSH PRIVILEGES;
To set the maximum number of updates per hour at 1000 on a given connection do this:

UPDATE mysql.user SET
max_updates = 1000
WHERE user='myuser' AND host='localhost';
FLUSH PRIVILEGES;
To set the maximum number of connections per hour at 1000 on a given connection do this:

UPDATE mysql.user SET
max_connections = 1000
WHERE user='myuser' AND host='localhost';
FLUSH PRIVILEGES;


Happy Learning

Sunday 9 October 2016

How to configure spring nature in eclipse?



1. Install New Software

 Eclipse IDE, click “Help” -> “Install New Software…”. 
Type “http://springide.org/updatesite” to access the Spring IDE update site.

Select all the Spring IDE features you want to install.

Take long time to install and restart Eclipse after finished.

OR

1. Eclipse Marketplace
(NO NEED TO KEEP REMEMBER LONG URL)

This is the prefer way, because you no need to remember the long Spring ide update URL.
In Eclipse IDE, click “Help” -> “Eclipse Marketplace“, type “Spring IDE“, follow the wizard to finish the installation.


2.select new project as a dynamic projects in eclipse

3. right click on project and then go to spring tool (second last option,just before properties)

click on "add spring project nature"

and now your project is ready for spring mvc :)


if any query then make a note in comment box

Thanks

Happy Learning

Friday 7 October 2016

Why Apache Tomcat 7.0.40 disappears after 1 second

Catalina needs JAVA_HOME to work properly. So configure path to java jre and JAVA_HOME in environment variables.
To see the error, in command prompt execute
\path\apache-tomcat-7.0.40\bin > catalina.bat run