Sei sulla pagina 1di 3

DBMS Labs: To have your own DBMS and to work on

it with JAVA
1 Downloading and installing tools
1. Download a DBMS with the linked server. For example, easyPHP (but you can download any
other tool) => http://www.easyphp.org/
2. Download and install any IDE for JAVA (NetBeans, Eclipse etc.)
3. Download the connector between mySQL and JAVA (.jar file). This connector is called JDBC (Java
DataBase Connectivity) => http://www.mysql.com/products/connector/

2 Creating your DataBase with PhpMyAdmin


4. Start easyPHP. You have to be administrator of your computer. The icon of easyPHP will appear.
Right click on this icon. Normally you will see Apache and MySQL started.
5. Open a browser and copy this address in the URL address => http://127.0.0.1/home/mysql/
You are now in PhpMyAdmin that is the tool to handle your databases (MySQL is the DBMS).
6. To create a SQL program, left click on the icon on the right top of the screen where it is written
SQL. To create your database, write :

-------------------------------------------------
CREATE DATABASE name_of_your_database;
-------------------------------------------------
7. Then, in order to create your table, write the following SQL code :

-------------------------------------------------
CREATE TABLE students(
roll_no INT,
nameCHAR( 50 ) ,
father_nameCHAR( 50 ) ,
PRIMARY KEY ( roll_no )
);
-------------------------------------------------
8. Now, your database is created. You can fill it with data in two ways:
- PhpMyAdmin
- Java program

3 Filling your database with phpMyAdmin


Right click again on the SQL icon and write for example:

-------------------------------------------------
INSERT INTO students VALUES
(1,"Mickael Kumar", "Papa Kumar"),
(2,"Rajender Poulain", "Papa Poulain");
-------------------------------------------------

4 Filling the database with JAVA


Of course, when a user will use the application, he will not enter himself the SQL code by his own
because he is not a computer scientist. He will do it thanks to an application.

1. Installing the connector. To connect the language to the DBMS, you need a connector. We will
describe the steps for NetBeans.
2. Create a new JAVA project
3. Right click on the project on the left. Choose “set configuration” and “customize”. Then choose
“library” and “Add Jar/Folder”. Browse and choose the connector. You have linked the
connector to your project.
4. Then create a main class like this (if you just want to fill the database):

-------------------------------------------------
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class update {

public static void main(String[] args) {

final String name_database = "college";


final String name_table = "students";

try {
// Connection to the database
Connection con = DriverManager.getConnection(
"jdbc:mysql://127.0.0.1:3306/" + name_database, "root", "");

// Filling the database


String updating = "INSERT INTO " + name_table + " VALUES (87,\"my name\",\"father name\");";
Statement link1;

link1 = con.createStatement();
link1.executeUpdate(updating);

} catch (SQLException e) {
System.out.println("problem with the connection!");
}
}
}

Notice: Your database created in PhpMyAdmin need the same name that the database. It is the same for
the tables created in the database.

Explanation:
Connection to the database:

-------------------------------------------------
Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/" + name_database, "root", "");
-------------------------------------------------

The static method “getConnection” from the class “DriverManager” allow you to open a connection to the
database. This opening is down to an object (here it is con). It is necessary before manipulating it. There
are three arguments:

- First argument: address of the database. Generally, the database is in a server (not in your
machine), and you need to know where it is. You also need to know the connector. It follows
always the same pattern :
name_connector://ip_address:port/name_database
In our case, name_connector is jdbc:mysql. Ip_address is 127.0.0.1 and port is 3306. The name
of the database depends of you.
- Second argument: Name of the user. It is in our case “root”
- Third argument: Password of the user. It is in our case nothing “”.

Running the insert statement:

-------------------------------------------------
String updating = "INSERT INTO " + name_table + " VALUES (87,\"my name\",\"father name\");";
Statement link1;
link1 = con.createStatement();
link1.executeUpdate(updating);
-------------------------------------------------
You need first to create an object statement thanks to the object of the connection (here it is con).
Then, you can execute your update.

5 Scanning text with the keyboard


-------------------------------------------------
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
-------------------------------------------------

6 Exercise
Write a program which fills a table of a database according to the need of the user => Ask the user how
many students he wants to store. Then ask him these students (the fields) and store them in the
database. Finally check if everything is ok (the students are really stored in the database) with
phpMyAdmin.

Potrebbero piacerti anche