0% found this document useful (0 votes)
98 views5 pages

Connecting JSP To Mysql Database Lesson

The document provides instructions for connecting a Java Server Pages (JSP) application to a MySQL database using Tomcat. It includes code examples for login and registration JSP pages that retrieve and insert user data into a database table. The document also outlines the basic Tomcat directory structure and how to run the project.

Uploaded by

jpnraambabu
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views5 pages

Connecting JSP To Mysql Database Lesson

The document provides instructions for connecting a Java Server Pages (JSP) application to a MySQL database using Tomcat. It includes code examples for login and registration JSP pages that retrieve and insert user data into a database table. The document also outlines the basic Tomcat directory structure and how to run the project.

Uploaded by

jpnraambabu
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Connecting JSP To Mysql Database Lesson

My brother Ravi Tamada request one mail about his college presentation. He is planning to do web dynamic project. So i am giving small explanation about JSP (Java Server Pages) to Mysql Connection structure, Tomcat directory structure and simple database examples.

Login.html

Code :
<body> <form action="login.jsp" method="post"> User name :<input type="text" name="usr" /> password :<input type="password" name="pwd" /> <input type="submit" /> </form> </body>

Reg.html

code:
<form action="reg.jsp" method="post"> Email :<input type="text" name="email" /> First name :<input type="text" name="fname" /> Last name :<input type="text" name="lname" /> User name :<input type="text" name="userid" /> password :<input type="password" name="pwd" /> <input type="submit" /> </form>

Mysql Create Database Test: Mysql no doubt about it best open source database http://mysql.com/

Create Table Users:

login.jsp
<%@ page import ="java.sql.*" %> <%@ page import ="javax.sql.*" %> <% String userid=request.getParameter("user"); session.putValue("userid",userid); String pwd=request.getParameter("pwd"); Class.forName("com.mysql.jdbc.Driver"); java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ test","root","root"); Statement st= con.createStatement(); ResultSet rs=st.executeQuery("select * from users where user_id='"+userid+"'"); if(rs.next()) { if(rs.getString(2).equals(pwd)) { out.println("welcome"+userid); } else { out.println("Invalid password try again"); } } else %>

reg.jsp
<%@ page import ="java.sql.*" %> <%@ page import ="javax.sql.*" %> <% String user=request.getParameter("userid"); session.putValue("userid",user); String pwd=request.getParameter("pwd"); String fname=request.getParameter("fname"); String lname=request.getParameter("lname"); String email=request.getParameter("email"); Class.forName("com.mysql.jdbc.Driver"); java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root","root"); Statement st= con.createStatement(); ResultSet rs; int i=st.executeUpdate("insert into users values ('"+user+"','"+pwd+"','"+fname+"', '"+lname+"','"+email+"')");

%>

welcome.jsp
<%@ page import ="java.sql.*" %> <%@ page import ="javax.sql.*" %> <% String user=session.getValue("userid").toString(); %> Registration is Successfull. Welcome to <%=user %>

Tomcat Directory Structure Tomcat open source web server you can download from this linkhttp://tomcat.apache.org/

Run Your Project

You might also like