how to connect jsp to mysql

Ads
 

how to connect jsp to mysql

View Answers

June 4, 2008 at 7:27 PM

Hi ragini,


This is form code ,

<html>
<head>
<title>Insert into database</title>

</head>

<body>

<table border="1" width="50%" bgcolor="pink" cellspacing="0" cellpadding="0">
<tr>
<td width="100%">
<form method="POST" action="/examples/jsp/LoginAction" >

<h2 align="center">login page</h2>
<table border="1" width="100%">
<tr>
<td width="50%"><b>User Name:</b></td>
<td width="50%"><input type="text" name="userid" size="20"/></td>
</tr>
<tr>
<td width="50%"><b>Password:</b></td>
<td width="50%"><input type="password" name="password" size="20"/> </td>
</tr>

</table>
<p><input type="submit" value="Submit" name="submit">
<input type="reset" value="Reset" name="reset"></p>

</form>
</td>
</tr>
</table>

</body>
</html>

--------------------------------

web.xml

<servlet>
<servlet-name>loginaction</servlet-name>
<servlet-class>javacode.LoginAction</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>loginaction</servlet-name>
<url-pattern>/jsp/LoginAction</url-pattern>
</servlet-mapping>


June 4, 2008 at 7:29 PM

This is servlets code,

package javacode;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class LoginAction extends HttpServlet {
private Connection con;
String url = "jdbc:mysql://192.168.10.211:3306/";;
String db = "amar";
String driver = "com.mysql.jdbc.Driver";
String userName ="amar";
String password="amar123";
public void init()throws ServletException {
System.out.println("INIT");
try {
Class.forName(driver);
con=DriverManager.getConnection(url+db,userName,password);
}//try
catch(Exception e){
e.printStackTrace();
throw new ServletException("unable to get database cooenction.");
}//catch

}//init
public void doGet(HttpServletRequest req,HttpServletResponse res)throws IOException,ServletException {

System.out.println("in service");
res.setContentType("text/html");
PrintWriter out=res.getWriter();
PreparedStatement ps=null;
try{
String userid=req.getParameter("userid");
String pass=req.getParameter("password");
if(userid==null||userid.equals("")||pass==null||pass.equals("")){
out.println("<html><body>");
out.println("<i>UserName/Password are empty,they cannot be empty </i>");
out.println("</body></html>");
return;
}//if
ps=con.prepareStatement("select * from login where userid=? and password=?");
ps.setString(1,userid);
ps.setString(2,pass);
ResultSet rs=ps.executeQuery();
out.println("<html><body>");
if(rs.next())
out.println("Successfully login");
else
out.println("username and password not currect, please try again.");
out.println("</body></html>");
}//try
catch(Exception e){
out.println("unable to process the request please try later");
}//catch
finally {
try{
ps.close();
}
catch(Exception e) {}
}//finally
}//doget
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException,
IOException {
doGet(req, res);
}
public void destroy() {
System.out.println("In destroy");
try {
con.close();
}
catch(Exception e) {
System.out.println(e);
}
}//destroy
}

------------------------------------------------

Read for more information.

http://www.roseindia.net/servlets

Thanks

Ads









Related Tutorials/Questions & Answers:
How to connect mysql with jsp
How to connect mysql with jsp  how to connect jsp with mysql while using apache tomcat
How to connect to MySQL in JSP?
How to connect to MySQL in JSP?  How to connect to MySQL in JSP..., You can use the Java JDBC code to connect to MySQL database in JSP page. Check the tutorial: Connect JSP with mysql Thanks
Advertisements
how to connect jsp to mysql - Java Beginners
how to connect jsp to mysql  I m new in Mysql and JSP i m ceating.java file using bean file for connectivity but i m not able to connect jsp... me hw to conncet jsp with mysql this is connection file package connect
how to connect to database in php using mysql
how to connect to database in php using mysql  how to connect to database in php using mysql
how to connect mySql drivers into eclipse(configuration)?
how to connect mySql drivers into eclipse(configuration)?  how to connect mySql drivers into eclipse(configuration)? I need the configuration in eclipse. what do i have to download from mySql and just downloading is enough or do
how to connect jsp with sql database by netbeans in a login page?
how to connect jsp with sql database by netbeans in a login page?  how to connect jsp with sql database by netbeans in a login page
how to connect jsp with sql database by netbeans in a login page?
how to connect jsp with sql database by netbeans in a login page?  how to connect jsp with sql database by netbeans in a login page
how to connect jsp with sql database by netbeans in a login page?
how to connect jsp with sql database by netbeans in a login page?  how to connect jsp with sql database by netbeans in a login page
how to connect to MS access database in JSP?
how to connect to MS access database in JSP?  how to connect to MS access database in JSP? Any seetings/drivers need to be set or installed before... DSN will get created. 6) Restart your server and run your jsp code. Here is your
Mysql connect
Mysql connect  i can have a mysql database file and mysql-connector-net .but i have not mysql database. how to connect vb.net thsis database.(C:\Documents and Settings\Admin\My Documents\VB.NET\Stock delna\STC.sql ). plz help me
how to fetch image from mysql using jsp
how to fetch image from mysql using jsp  how to fetch image from mysql using jsp
How to connect to MySQL from command prompt?
How to connect to MySQL from command prompt? This video tutorial walks you through the steps to connect to MySQL from command prompt in Windows machine... of: "How to connect to MySQL from command prompt?" Then is sure
How to connect to dao n bean classes with jsp
= totalvillagearea; } } **how to connect with this edao and ebean pkg with my jsp...How to connect to dao n bean classes with jsp  I have made this edao...; </table> <!-- End of New Village Registration --> <jsp:useBean
How to retrieve image from mysql database in JSP?
How to retrieve image from mysql database in JSP?  Hi, I need JSP same codes for learning to get image which is stored in MySQL Database. How to retrieve image from mysql database in JSP? Thanks   Hi, You can write
mysql_connect arguments
mysql_connect arguments   How many arguments a mysql_connection function required to connect to the database?   ?mysql_connect? function... And if the given arguments are correct it will connect to the database and print
How to connect
How to connect  how to connect to a remote host using jsp? We need to update files to the database of the application on the local machine from the updated database on our webpage
HOW WRITE GREEK CHARACTERS INTO MYSQL BY JSP ?
HOW WRITE GREEK CHARACTERS INTO MYSQL BY JSP ?  HALLOW TO ALL ! I'M USING MYSQL5.5 , TOMCAT7 & JSP. IN MY BROWSER I'M SEEING GREEK CHARS BECAUSE...:mysql://localhost:3306/XXXXX?user=userName&password=usersPassword&
how to display image and text in single jsp page from the mysql database
how to display image and text in single jsp page from the mysql database  hello please help me to display the image and text in single jsp page from mysql database if have any reference code please send me Thanks in advance
jsp/mysql connection
jsp/mysql connection  I am puttng the following files in the lib folder of jdk to connect jsp and mysql and in the lib folder of tomacat and in the lib folder of project of netbeans but I am unable to to connect jsp and mysql
jsp/mysql connection
jsp/mysql connection  I am puttng the following files in the lib folder of jdk to connect jsp and mysql and in the lib folder of tomacat and in the lib folder of project of netbeans but I am unable to to connect jsp and mysql
ModuleNotFoundError: No module named 'connect-mysql'
'connect-mysql' How to remove the ModuleNotFoundError: No module named 'connect-mysql' error? Thanks   Hi, In your python...ModuleNotFoundError: No module named 'connect-mysql'  Hi, My
ModuleNotFoundError: No module named 'connect-mysql'
'connect-mysql' How to remove the ModuleNotFoundError: No module named 'connect-mysql' error? Thanks   Hi, In your python...ModuleNotFoundError: No module named 'connect-mysql'  Hi, My
how to auto generate number in jsp with the database connection mysql for employee number?
how to auto generate number in jsp with the database connection mysql for employee number?  how to auto generate number in jsp with the database connection mysql for employee number?   <%@page import="java.sql.
how to auto generate number in jsp with the database connection mysql for employee number?
how to auto generate number in jsp with the database connection mysql for employee number?  how to auto generate number in jsp with the database connection mysql for employee number?   <%@page import="java.sql.
how to upload an image from a jsp page to a mysql database table using jsp
how to upload an image from a jsp page to a mysql database table using jsp  how to upload an image from a jsp page to a mysql database table using jspstrong text
connection to mysql in jsp
connection to mysql in jsp  please i got this error message when trying to connect to mysql database in jsp. Unknown character set: 'utf8mb4
JSP &MYSQL
JSP &MYSQL  Develop a web page for the telephone directory using jsp and mysql
How to save form fields into the MySql Database without submit button in jsp?
How to save form fields into the MySql Database without submit button in jsp?  I want to store user inputs into the database using javasccript or ajax or jqury but without submit button. Form Contains three fields
need help....how to connect and disconnect multiple databases(databases created in mysql) using java and my sql
need help....how to connect and disconnect multiple databases(databases created in mysql) using java and my sql  i am working on a project... to know that how to query multiple databases(databases created in my mysql) so
JSP with Mysql
JSP with Mysql  Hi sir, I trying to several time but i am not get an answer. My questions are 1. How to inserting same field data to different mysql table using jsp. That is creating a form with 5 fields,data's are same
not able to connect to mysql using.. jdbc
not able to connect to mysql using.. jdbc   i am not able to connect to mysql using jdbc .. is there any classpath that i need to set..because i am using mysql-connector-java jar file..to connect to mysql.. Pls provide the steps

Ads