Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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/
-------------------------------------------------
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
-------------------------------------------------
INSERT INTO students VALUES
(1,"Mickael Kumar", "Papa Kumar"),
(2,"Rajender Poulain", "Papa Poulain");
-------------------------------------------------
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;
try {
// Connection to the database
Connection con = DriverManager.getConnection(
"jdbc:mysql://127.0.0.1:3306/" + name_database, "root", "");
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 “”.
-------------------------------------------------
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.
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.