Databases| SQL| MySQL| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
Using MYSQL Database with JSP & Servlets. 
 

Using MYSQL Database with JSP & Servlets. Using MYSQL Database with JSP & Servlets. MY SQL is a powerful RDBMS, which can handle large amount of data. And more interestingly it is free to use, except for some commercial use. you can download it

 

Using MYSQL Database with JSP & Servlets.

                         

    
MY
SQL is a powerful RDBMS, which can handle large amount of data. And more interestingly it is free to use, except for some commercial use. you can download it from www.mysql.com.  It is available for both Linux and NT platforms, but it is primarily for Linux and Unix servers and is mostly used on Linux and Unix platform.    

This lesson is intended to provide hands an experience with MYSQL database. We will use tomcat web server to run over web application which acceres the MYSQL database. Here I am using MYSQL & tomcat server both running an Linux machine.   

Downloading and installing MYSQL Server.

Downloading  MYSQL server from www.mysql.com. Upload the downloaded file on to your Linux server. go to the directory where you uploaded the file. To install the file issue the Following command. 

rpm-ivh mysql-server-3.23.361.1386.rpm.

This will install the MySQL server on your Linux machine.


Testing the installation is complete. Issue the following command after the installation is complete.

MYSQL


    

And you will see the mysql prompt (mysql>) appears ,this means that you sucessfully installed mysql on your Linux server. 

 Syntax is:
  
mysql -h hostname -u username -p[password]
 Or
  
mysql -h hostname -u username --password=password

Typing help on the mysql prompt shows online help.

  mysql> help

  MySQL         commands:
  help (\h)         Display this text
  ? (\h)              Synonym for `help'
  clear (\c)        Clear command
  connect (\r)    Reconnect to the server. Optional arguments are db and host
  edit (\e)          Edit command with $EDITOR
  exit (\q)          Exit mysql. Same as quit
  go (\g)           Send command to mysql server
  ego (\G)        Send command to mysql server; Display result vertically
  print (\p)       Print current command
  quit (\q)        Quit mysql
  rehash (\#)    Rebuild completion hash
  status (\s)      Get status information from the server
  use (\u)         Use another database. Takes database name as argument

  Connection id: 1 (Can be used with mysqladmin kill)

  mysql> 

Creating a Database.

Now we create a database named "mysqltutorial" for our tutorial. create database is used to create database. Issue the following command to create mysqltutorial database.

mysql> create database mysqltutorial;
Query OK, 1 row affected (0.18 sec)
mysql> 

After creating database it is required to connect to the database to create tables for our tutorial. To connect to a database "use <database name>" command is used. Type the following command to connect to mysqltutorial database.

  mysql> use mysqltutorial;
  Database changed
  mysql> 

In MySQL all the database commands are followed by a semi-colon(;).

Now we will  create a table in the database 'test' for storing the names of countries. "Create table <table name>" command is used to create the table. Issue the following command on mysql promopt:

  mysql> create table test(srno int, countryname char(50));
  Query OK, 0 rows affected (0.29 sec)
  
   mysql>

In the above example we have created a table named "test" to store country name, but the name "test" is not good. So we will change the name of table to "country". We will use "rename" command to rename the table. Type the following command to rename the table:

  mysql> alter table test RENAME countryname;
  Query OK, 0 rows affected (0.04 sec) 

To view structure of database "desc <table name>" is used. To view structure of countryname  issue the following command:

mysql> desc countryname;

 

Above command renames the table. To insert some records in the database insert into table (fields..) values (values..) command is used. Issue the following command to enter the country name in the database.

   mysql> insert into countryname(srno,countryname) values(1,'India');
   Query OK, 1 row affected (0.09 sec)
   
   mysql> 

   mysql> insert into countryname(srno,countryname) values(2,'USA');
   Query OK, 1 row affected (0.09 sec)
  
   mysql> 

Above command enters a record in the countryname table. To view records from the countryname table issue the following command:

  mysql> select * from countryname;

In this section you learned how to work with MySQL database server, create database, create tables in database, rename table and insert and select the records from database table. In the next lesson we will learn how to connect to the database from JSP page.

Send you queries/suggestions regarding this tutorial to Deepak Kumar.

                         

» View all related tutorials
Related Tags: c database design com ide data software io get install this id secure war tab work databases to base ast

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

13 comments so far (
post your own) View All Comments Latest 10 Comments:

Thanks a lot for ur helping tendency....
Its really very helpful 4 us....
Actually in my company v r working in MyEclipse, jsp projects using struts....My SQL is working as database. . .

need this kind of coding more. . . please help us.

Thanks and Regards. Juliet.

Posted by jyo on Thursday, 11.13.08 @ 02:25am | #81634

hi this is gayathri i want coding in userid is already exist or not how check i want coding using jsp,servlet with javascript
if enter the uid it display the alert msg pls very urgent

Posted by gayathri on Thursday, 09.18.08 @ 12:27pm | #80273

site is very nice...i suggest you to plz add some free ebooks downloads to d site ..

Posted by jitin khurana on Saturday, 08.9.08 @ 07:37am | #71840

thank u for this lesson, now i want to learn how to connect to the database from JSP page.

Posted by islam on Friday, 04.4.08 @ 20:40pm | #55332

I have a daubt about the database connection that how the system find the actual server without using the connection poling in the above jsp code.
con = DriverManager.getConnection("jdbc:mysql://localhost/test","root","mysql");

here what is test is it the context or the DBName.?

Posted by sreejith on Monday, 02.18.08 @ 16:30pm | #48884

<%@ page language="java" import="java.sql.*" errorPage="" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%! Connection con=null; %>
<%! Statement st= null; %>
<%! ResultSet rs= null; %>
<%! boolean found =false; %>
<%
Class.forName("com.mysql.jdbc.Driver").newInstance();
try
{
con = DriverManager.getConnection("jdbc:mysql://localhost/test","root","mysql");
out.println("System connected\n");
st = con.createStatement();
String s1=request.getParameter("uname");
//out.println(s1);
rs=st.executeQuery("select * from user where name='"+s1+"'");
session.setAttribute("name",s1);
String s2=request.getParameter("pswd");
//out.println("After Executing the Query" + rs);
while(rs.next())
{
out.println(rs.getString(s1));
String rs1=rs.getString(1);
//session.setAttribute("user");
String rs2=rs.getString(2);
if(s2.equalsIgnoreCase(rs2))
{
int id=rs.getInt("userid");
//out.println("After Executing the Query" );
int userid=rs.getInt("userid");
out.println(userid);
}
}
}
catch(Exception e)
{
System.out.println("validation failure"+e);
}
finally
{
st.close();
rs.close();
con.close();
}
%>

THE ERROR IS

System connected After Executing the Querycom.mysql.jdbc.ResultSet@13c4c09

Posted by ujwala on Friday, 11.16.07 @ 10:20am | #37546

I want to learn how to connect to the database from JSP page.
Thank you~~~~~~

Posted by meehuajang on Saturday, 11.3.07 @ 13:24pm | #35452

how can connect datebase mysql
i want class level coding....
please help me..
very urgent

Posted by rameesh on Friday, 10.5.07 @ 11:46am | #31785

hi
i want to know
how to pass database argument to jsp form from servlet

Posted by prakash on Thursday, 09.27.07 @ 10:54am | #30061

good

Posted by vasudha on Tuesday, 06.12.07 @ 11:30am | #18968

Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.