Java est un langage de programmation orienté objet créé par James Gosling et Patrick Naughton, employés de Sun Microsystems, avec le soutien de Bill Joy, présenté officiellement le 23 mai 1995 au SunWorld. La société Sun a été ensuite rachetée en 2009 par la société Oracle qui détient et maintient désormais Java.
Microsoft SQL Server est un système de gestion de base de données en langage SQL incorporant entre autres un SGBDR développé et commercialisé par la société Microsoft.
Comme d’autres langage peut se connecter aux différentes base de données, JAVA aussi à la possibilité de se connecter a d’autre bases de données que ORACLE
SQL Server, MYSQL, MongoDB, MS Accès….
Dans cette article nous allons connecter notre application à la base de données SQL Server et MYSQL,
SQL Server possède deux modes de connexion, soit en utilisant le mode SQL Server soit le mode Authentification Windows, nous allons découvrir comment connecter au mode SQL Server, Pour pouvoir établir la connexion il est préférable de télécharger la librairie jdbc4-2.0.jar cette librairie SQL servira au fournisseur de pilote SQL Server
Télécharger le pilote sur ce lien : https://www.dropbox.com/s/ssewd6jlh4th1x5/sqljdbc4-2.0.jar?dl=0
import java.sql.*;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
import javax.swing.JOptionPane;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Code-Codage
*/
public class SqlMode {
//Délaration des Méthode
private static String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static String host = "localhost:3306";
private static String username = "root";
private static String password = "root";
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Connection cnx = null;
PreparedStatement requete = null;
ResultSet resulta = null;
try
{
Class.forName(driver);
cnx = DriverManager.getConnection(host,username,password);
cnx = DriverManager.getConnection(host,username,password);
requete = cnx.prepareStatement("SELECT * FROM PERSONNE");
resulta = requete.executeQuery();
ResultSetMetaData RsMData = requete.getMetaData();
int q = RsMData.getColumnCount();
DefaultTableModel RecordTable = (DefaultTableModel)jTable1.getModel();
RecordTable.setRowCount(0);
while(resulta.next()){
Vector coloneTable = new Vector();
for(int i = 1; i <= q; i++)
{
coloneTable.add(resulta.getString("ID"));
coloneTable.add(resulta.getString("NOM"));
coloneTable.add(resulta.getString("POSTNOM"));
}
RecordTable.addRow(coloneTable);
}
}catch(Exception ex)
{
Logger.getLogger(SqlMode.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Connexion Authentification Windows
import java.sql.*;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
import javax.swing.JOptionPane;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Code-Codage
*/
public class AuthentificationWindows {
private static String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static String host = "jdbc:sqlserver://SAMSUNG;" + "databaseName=room_reservation; integratedSecurity=true";
private static String username = "SAMSUNG\\CODE-CODAGE";
private static String password = "";
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
// TODO code application logic here
Connection cnx = null;
PreparedStatement requete = null;
ResultSet resulta = null;
try
{
Class.forName(driver);
cnx = DriverManager.getConnection(host,username,password);
cnx = DriverManager.getConnection(host,username,password);
requete = cnx.prepareStatement("SELECT * FROM PERSONNE");
resulta = requete.executeQuery();
ResultSetMetaData RsMData = requete.getMetaData();
int q = RsMData.getColumnCount();
DefaultTableModel RecordTable = (DefaultTableModel)jTable1.getModel();
RecordTable.setRowCount(0);
while(resulta.next()){
Vector coloneTable = new Vector();
for(int i = 1; i <= q; i++)
{
coloneTable.add(resulta.getString("ID"));
coloneTable.add(resulta.getString("NOM"));
coloneTable.add(resulta.getString("POSTNOM"));
}
RecordTable.addRow(coloneTable);
}
}catch(Exception ex)
{
Logger.getLogger(AuthentificationWindows.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Pour pouvoir établir la connexion il est préférable de télécharger la librairie mysql-connector-java-5.1.47.jar cette librairie au fournisseur de pilote MYSQL
Lien 1 : https://www.dropbox.com/s/74857kt58m6svjr/mysql-connector-java-5.1.47-bin.jar?dl=0 ou https://www.dropbox.com/s/vzfh2unmnj1d7y4/mysql-connector-java-5.1.47.jar?dl=0
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package gestion_des_clients;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
/**
*
* @author Code-Codage
*/
public class MYSQL {
/* Déclaration des variables de connexion qui contiendront les données de connexion*/
private static String username = "root";
private static String password = "";
private static String database = "jdbc:mysql://localhost/gestionclient";
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
/* Déclaration de la variable connexion et la variable requetee ensuite initialisation comme valeur null*/
Connection cnx = null;
PreparedStatement requete = null;
ResultSet resulta = null;
try
{
Class.forName("com.mysql.jdbc.Driver");
/*
Appe de la variable cnx et affectation directement
*/
cnx = DriverManager.getConnection(database,username,password);
requete = cnx.prepareStatement("SELECT * FROM PERSONNE");
resulta = requete.executeQuery();
ResultSetMetaData RsMData = requete.getMetaData();
int q = RsMData.getColumnCount();
DefaultTableModel RecordTable = (DefaultTableModel)jTable1.getModel();
RecordTable.setRowCount(0);
while(resulta.next()){
Vector coloneTable = new Vector();
for(int i = 1; i <= q; i++)
{
coloneTable.add(resulta.getString("ID"));
coloneTable.add(resulta.getString("NOM"));
coloneTable.add(resulta.getString("POSTNOM"));
}
RecordTable.addRow(coloneTable);
}
}catch(Exception ex)
{
JOptionPane.showConfirmDialog(null, ex);
}
}
}