Saturday

JSP connection with MYSQL in Linux

Now Let us  see how to connect jsp (Java Server Page) with Mysql in Linux - Ubuntu, mint, debian,. Java server page is a technology used to create dynamic web pages & server-side scripting language. It is similar to php but uses java programming language.

To run a jsp we need a web server,
   1.  Install  tomcat server in your system :
            by giving the following command in terminal

                          sudo apt-get install tomcat7      
          You can install a new version(version-8 ) of tomcat by specifying the version number in the  command.
 
          To test whether tomcat installed properly
         Open your browser & type the following URL:

                             http://localhost:8080/

             You will get the following result


  
  Install JDBC Driver :
                  To make a connection between JSP & Mysql, install a JDBC(connector/J) driver .
                     
                          Download mysql-connector-java-version-bin.zip
                                         link to Download
                          Extract the zip and get the mysql-connector-java-5.1.13-bin.jar file
                          Copy the .jar file to  the following location
                     
                                     filesystem/usr/share/tomcat6/lib
                                                       i.e
                                           /usr/share/tomcat6/lib
                       
      Restart the tomcat server :
              
                           sudo /etc/init.d/tomcat7 start
                                          sudo /etc/init.d/tomcat7 stop

    Apache tomcat server can be restarted by using following single cmd also.
                                          sudo /etc/init.d/tomcat6 restart 


         Now Let us create a Sample JSP with Mysql connection.  
                   
                  test.jsp

<!DOCTYPE HTML>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<html>


<head>
<title>Techie Touch | Mysql connectivity</title>
</head>
<body>
<h1>Connection status </h1>
<%
try {
String connectionURL = "jdbc:mysql://localhost:3306/Database_name";
Connection connection = null;
// Load JBBC driver "com.mysql.jdbc.Driver"
Class.forName("com.mysql.jdbc.Driver").newInstance();
/* user name = root
password =ur mysql password. */
connection = DriverManager.getConnection(connectionURL, "root", "password");
if(!connection.isClosed())
%>
<font size="+3" color="green"></b>
<%
out.println("Successfully connected to " + "MySQL server using TCP/IP...");
connection.close();
}
catch(Exception ex){
%>
</font>
<font size="+3" color="red"></b>
<%
out.println("Unable to connect to database.");
}
%>
</font>
<p>techie-touch.blogspot.com</p>
<p>For more details..<a href=" http://techie-touch.blogspot.com">Visit</a></p>
</body>
</html>

        ★  Save the file as test.jsp in the following path 
               
                       /var/lib/tomcat6/webapps/test.jsp
 
        ★   Now open the browser & run the above file by giving the following URL:
 
                      http://localhost:8080/test.jsp

                            *****     Like us in FB & get updates ...... LIKE      ****
                                                                                      

______________________________________________________________________________

0 comments:

Post a Comment