9lessons programming blog
Loading Search
Tuesday, September 16, 2008

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 link http://tomcat.apache.org/



Run Your Project
Sponsored Links

Share this post

Comments
{ 14 comments }
Anonymous said...

wat about the login.html and reg.html for these........sri?

Vamsy said...

i want to have those yaar............

plzz post those first sri......

Anonymous said...

.how to connect these jsp to html pages.........

Vamsy said...

hie srinivas,........plz explain how jsp and html pages get connected........plz post yaar.....!!

Srinivas Tamada said...

login.html and reg.html Code posted......

vitamin a said...

Simply stated, JDBC makes it possible to write platform independent Java programs that can be used to manipulate the data in a wide range of SQL databases without the requirement to modify and/or recompile the Java programs when moving from platform to platform or from DBMS to DBMS. MySQL is available for a wide variety of platforms.

infopediaonlinehere said...

its a nice post

Stefan said...

very good this post. thanks

harini said...

srinivas ur post was excellent..can you help me out with this..this user login when submit must go to another page that displays another html form.

Srinivas Tamada said...

@Harini

login.jsp include redirect .html

request.getRequestDispatcher("/xyz.html");

Anonymous said...

Where is Download button :)

surimca said...

Thanks this Connecting JSP To Mysql Database Lesson very useful to me!!!!!!

Mahesh Dada said...

Hi Srinivas, I refer ur example for my application but i face on problem means getting an error so plz help me.
The exception is follow:

org.apache.jasper.JasperException: javax.servlet.ServletException: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

plz plz help me

Regards,
Mahesh

deepika said...

hi sir,
how to run this project in glassfish server.
i got the error as resources not available can u explan it clearly pls

Post a Comment

Subscribe now!Recent Posts

Categories

Subscribe now!Popular Posts

People Says

@9lessons thank you for the great tutorials, we truly appreciate your contributions to the design community.

Smashing Magazine

Like Me

follow me
products

9lessons labs

9lessons clouds

Android application

Chrome Extension