Vous êtes sur la page 1sur 9

Java Database Connectivity with MySQL

To connect Java application with the MySQL database, we need to follow 5 following steps.
In this example we are using MySql as the database. So we need to know following informations
for the mysql database:
1. Driver class: The driver class for the mysql database is com.mysql.jdbc.Driver.
2. Connection URL: The connection URL for the mysql database is
jdbc:mysql://localhost:3306/sonoo where jdbc is the API, mysql is the database,
localhost is the server name on which mysql is running, we may also use IP address, 3306
is the port number and sonoo is the database name. We may use any database, in such
case, we need to replace the sonoo with our database name.
3. Username: The default username for the mysql database is root.
4. Password: It is the password given by the user at the time of installing the mysql
database. In this example, we are going to use root as the password.
Let's first create a table in the mysql database, but before creating table, we need to create
database first.

create database sonoo;

use sonoo;

create table emp(id int(10),name varchar(40),age int(3));

Example to Connect Java Application with mysql database

In this example, sonoo is the database name, root is the username and password both.
download the jar file mysql-connector
https://static.javatpoint.com/src/jdbc/mysql-connector.jar

Two ways to load the jar file:

1. Paste the mysqlconnector.jar file in jre/lib/ext folder


2. Set classpath

1) Paste the mysqlconnector.jar file in JRE/lib/ext folder:


Download the mysqlconnector.jar file. Go to jre/lib/ext folder and paste the jar file here.
2) Set classpath:
There are two ways to set the classpath:
 temporary
 permanent

How to set the temporary classpath


open command prompt and write:

How to set the temporary classpath


open command prompt and write:
C:>set classpath=c:\folder\mysql-connector-java-5.0.8-bin.jar;.;

How to set the permanent classpath


Go to environment variable then click on new tab. In variable name write classpath and in variable value
paste the path to the mysqlconnector.jar file by appending mysqlconnector.jar;.; as C:\folder\mysql-
connector-java-5.0.8-bin.jar;.

Best Free Database Software:


1. MySQL
2. Microsoft SQL
3. PostgreSQL
4. Teradata Database
5. SAP HANA, Express Edition
6. MongoDB
7. CouchDB
8. DynamoDB
9. MarkLogic
10. RethinkDB
11. ArangoDB
12. Neo4j
13. OrientDB
14. Titan
15. Cayley
16. Hive
17. Elasticsearch
18. Cassandra

Most Popular Databases


https://scalegrid.io/blog/2019-database-trends-sql-vs-nosql-top-databases-single-vs-multiple-database-
use/

SQL Database Use: 60.48%


NoSQL Database Use: 39.52%
Most Popular Databases

So, which databases are most popular in 2019? Knowing that SQL was used by over 3/5 of
respondents, you might assume Oracle stole the show. Guess again. MySQL dominated this
report with 38.9% use, followed by MongoDB at 24.6%, PostgreSQL at 17.4%, Redis at 8.4%, and
Cassandra at 3.0%. Oracle trailed behind at just 1.8% from these database reporters, and
CouchDB, Berkeley DB, Microsoft SQL Server, Redshift, Firebase, Elasticsearch, and InfluxDB
users combined our Other category at 2.4%.
While these numbers might shock, there’s no mistaking the rise in popularity of MySQL,
MongoDB, and PostgreSQL. So how does this survey compare to best-known source for
database management system trends? DB-Engines Ranking – Trend Popularity report places
these leaders in the top 5, but Oracle keeps hold at number one and Microsoft SQL Server at
number 3.

Tutorial Membuat Stored Procedure Dengan Menggunakan MySQL


https://www.yudana.id/tutorial-membuat-stored-procedure-dengan-menggunakan-mysql/
Secara garis besar, format untuk membuat stored procedure seperti di bawah ini

CREATE PROCEDURE stored_procedure_name(parameter INT)


BEGIN
...blok kode...
END;

Create Procedure
Following statements create a stored procedure. By default, a procedure is associated with the default
database (currently used database). To associate the procedure with a given database, specify the name
as database_name.stored_procedure_name when you create it. Here is the complete syntax :

CREATE [DEFINER = { user | CURRENT_USER }]


PROCEDURE sp_name ([proc_parameter[,...]])
[characteristic ...] routine_body
proc_parameter: [ IN | OUT | INOUT ] param_name type
type:
Any valid MySQL data type
characteristic:
COMMENT 'string'
| LANGUAGE SQL
| [NOT] DETERMINISTIC
| { CONTAINS SQL | NO SQL | READS SQL DATA
| MODIFIES SQL DATA }
| SQL SECURITY { DEFINER | INVOKER }
routine_body:
Valid SQL routine statement

Cara Koneksi Database mysql (java)

import java.sql.*;

class MysqlCon {

public static void main(String args[]) {


try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/sonoo", "root", "root");
//here sonoo is database name, root is username and password
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from emp");
while (rs.next()) {
System.out.println(rs.getInt(1) + " " + rs.getString(2) + " " + rs.getString(3));
}
con.close();
} catch (Exception e) {
System.out.println(e);
}
}
}

https://www.java67.com/2015/07/difference-between-type-1-2-3-and-4-jdbc-drivers-java.html

JDBC Driver in Java


There are total 4 types of JDBC drivers exists in Java. They are known as type 1, 2, 3, and 4 drivers. In
order to understand difference between different JDBC drivers, first and most important thing to
understand is why Java has so many types of JDBC drivers? Why not just one? the answer lies in
portability and performance. The first JDBC driver is known as type 1 JDBC driver and the most recent
one is known as type 4 JDBC driver. There has been some talk about type 5 JDBC driver but I have not
heard anything concrete about it from Oracle or any other reliable source. So, type 4 JDBC driver is still
the latest one.

What is type 1 driver in JDBC?


This is the oldest JDBC driver, mostly used to connect database like MS Access from Microsoft Windows
operating system. Type 1 JDBC driver actually translate JDBC calls into ODBC (Object Database
connectivity) calls, which in turn connects to database. Since it acts as bridge between JDBC and ODBC, it
is also known as JDBC ODBC bridge driver. This driver had very poor performance because of several
layers of translation which took place before your program connects to database. It has also less portable
because it relies on ODBC driver to connect to database which is platform dependent. It is now obsolete
and only used for development and testing, I guess Java 7 even removed this driver from JDK.

What is type 2 driver in JDBC?


This was the second JDBC driver introduced by Java after Type 1, hence it known as type 2. In this driver,
performance was improved by reducing communication layer. Instead of talking to ODBC driver, JDBC
driver directly talks to DB client using native API. That's why its also known as native API or partly Java
driver. Since it required native API to connect to DB client it is also less portable and platform dependent.
If native library e.g. ocijdbc11.dll, which is required to connect Oracle 11g database is not present in
client machine then you will get java.lang.UnsatisfiedLinkError: no dll in java.library.path error.
Performance of type 2 driver is slightly better than type 1 JDBC driver.

What is type 3 driver in JDBC?


This was the third JDBC driver introduced by Java, hence known as type 3. It was very different than type
1 and type 2 JDBC driver in sense that it was completely written in Java as opposed to previous two
drivers which were not written in Java. That's why this is also known as all Java driver. This driver uses 3
tier approach i.e. client, server and database. So you have a Java client talking to a Java server and Java
Server talking to database. Java client and server talk to each other using net protocol hence this type of
JDBC driver is also known as Net protocol JDBC driver. This driver never gained popularity because
database vendor was reluctant to rewrite their existing native library which was mainly in C and C++

What is type 4 JDBC driver?


This is the driver you are most likely using to connect to modern database like Oracle, SQL Server,
MySQL, SQLLite and PostgreSQL. This driver is implemented in Java and directly speaks to database using
its native protocol. This driver includes all database call in one JAR file, which makes it very easy to use.
All you need to do to connect a database from Java program is to include JAR file of relevant JDBC driver.
Because of light weight, this is also known as thin JDBC driver. Since this driver is also written in pure
Java, its portable across all platform, which means you can use same JAR file to connect to MySQL even if
your Java program is running on Windows, Linux or Solaris. Performance of this type of JDBC driver is
also best among all of them because database vendor liked this type and all enhancement they make
they also port for type 4 drivers.

Difference between type 1 and type 2 JDBC driver?


Though both type 1 and type 2 drivers are not written in Java, there was some significant difference
between them. Type 2 driver has better performance than type 1 driver because of less layer of
communication and translation. As opposed to type 1 JDBC driver, in which JDBC calls are translated into
ODBC calls before they go to database, type 2 JDBC driver directly connect to db client using native
library.

Difference between type 2 and type 3 JDBC driver?


Main difference between type 2 and type 3 JDBC driver is that as opposed to type 2 driver, type 3 is
completely written in Java. Another difference which comes from this fact is that type 3 driver is more
portable than type 1 and type 2 drivers because it doesn't require any native library on client side to
connect to database. In terms of architecture, this was 3 tier architecture and uses net protocol for client
server communication.

Difference between type 3 and type 4 JDBC driver?


Main difference between type 3 and type 4 JDBC driver was removal of 3 tier architecture. Type 4 JDBC
driver directly connect to database using their native protocol as opposed to net protocol used by type 3
driver. Though both type 3 and type 4 driver is written in Java. Another key difference is ease of use, type
4 drivers just require one JAR file into classpath in order to connect to db. Performance of type 4 JDBC
driver is also better than type 3 driver because of direct connectivity to database as opposed to 3 tier
architecture of type 3 driver.

When to use different types of JDBC driver?


You should always use type 4 JDBC driver, there is hardly any situation when you need to go to previous
version of JDBC driver. Though, if you want to connect to Oracle database using TNS name using OCI
client, you need to use type 2 JDBC driver also known as thick JDBC driver. That requires database native
client library e.g. ocijdbc11.dll and if that's not present in the machine then your Java program will throw
java.lang.unsatisfiedlinkerror no ocijdbc11 in java.library.path error at run time.
That's all about difference between type 1, 2, 3, and type 3 JDBC driver in Java. JDBC drivers are evolved
in Java from less portable to most portable and from low performance to high performance. Type 1 JDBC
driver is the oldest while type 4 JDBC driver is the latest. In real world, you will be mostly likely using
type 4 JDBC driver, which is bundled in a JAR file. Just make sure to put them into your Java application's
classpath when you connect to database from Java program.

Vous aimerez peut-être aussi