You are on page 1of 13

Using Database in the web-

environment
Connecting to the Database
Practice
Setting up the MySQL
If you did not install MySQL
please download it from:
http://dev.mysql.com/downloads/cluster/
The following document explains the
installation process:
http://downloads.mysql.com/tutorials/cluster/
mysql_wp_cluster_quickstart.pdf
Working with the databses
from Netbeans
1- Choose the
Services tab

2- Right click on the


MySQL line, choose
Create Database
3- Enter
summer_13 for
the database
name
4- Right click on
the summer_13
line and choose
execute command
5- In the SQL Command window create table as:
Create Table student( sname varchar(20), sid
varchar(10), address varchar(50));
6- add the jdbc mysql driver into
your web project:
6.1 right click on Libraries
of your project then choose
Add Library

6.3 you will find


6.2 choos the MySQL JDBC
the MySQL driver
driver
added to your
project libraries
Adding data to your table
Create the following db_001.jsp file then run it:
<%
String sql = "";
Class.forName("org.gjt.mm.mysql.Driver");
try {
java.sql.Connection connection =
java.sql.DriverManager.getConnection("jdbc:mysql://localhost/summer_2013",
"root", "");
for (int i = 1; i <= 10; i++) {
java.sql.Statement st = connection.createStatement();
sql =
"Insert Into student (sname, sid, address)"
+ "Values ( \" ahmed_" + i + "\", \"2013_" + i + "\", \"street_" + i + "\");";
st.executeUpdate(sql);
}
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
System.out.println(sql);
}
%>
From the services tab ->choose the
Tables of the summer_13 database
and then right click on the student
table then choose view Data

Run the query by pressing this button

The result
will be:

You might also like