Bonjour
j'ai besoin de me connecter a une base MySQL en java mais je sais pas comment faire j'ai telecharcger le driver mm.mysql mais je n'arrive pas a me connecter
package exercicesjdbc;
import java.sql.*;
public class GestionCafe {
final String myBridge = "sun.jdbc.odbc.JdbcOdbcDriver";
private static Connection con;
private Statement sta;
public GestionCafe()throws ClassNotFoundException,SQLException {
doConnection();
}
private void doConnection()throws ClassNotFoundException,SQLException{
Class.forName(myBridge);
con = DriverManager.getConnection("jdbc:odbc:COFFEEBREAKDB","","");
sta = con.createStatement();
}
public void createTables() throws SQLException{
int sales = sta.executeUpdate("CREATE TABLE Sales (cof_id INTEGER, sup_id INTEGER, price INTEGER, amount INTEGER)");
int suppl = sta.executeUpdate("CREATE TABLE Suppliers(sup_id INTEGER, sup_name VARCHAR(50), street VARCHAR(50), city VARCHAR(20), state VARCHAR(2),zip INTEGER)");
int coffees = sta.executeUpdate("CREATE TABLE DataCoffees(cof_id INTEGER, cof_name VARCHAR(20))");
int invent = sta.executeUpdate("CREATE TABLE DataInventory(cof_id INTEGER,amount INTEGER)");
}
public void fillSales()throws SQLException{
int set1 = sta.executeUpdate("INSERT INTO Sales VALUES(1,101,8,20)");
int set2 = sta.executeUpdate("INSERT INTO Sales VALUES(2,49,9,10)");
int set3 = sta.executeUpdate("INSERT INTO Sales VALUES(3,150,10,5)");
int set4 = sta.executeUpdate("INSERT INTO Sales VALUES(4,101,9,40)");
int set5 = sta.executeUpdate("INSERT INTO Sales VALUES(5,49,10,20)");
}
public void fillSuppliers()throws SQLException{
int set1 = sta.executeUpdate("INSERT INTO Suppliers VALUES(101,'Acme,inc','99 Market Street','Groundsville','CA',95199)");
int set2 = sta.executeUpdate("INSERT INTO Suppliers VALUES(49,'Superior Coffee','1 Party Place','Mendocino','CA',95460)");
int set3 = sta.executeUpdate("INSERT INTO Suppliers VALUES(150,'The High Ground','100 Coffee Lane','Meadows','CA',93966)");
}
public void fillCoffees()throws SQLException{
int set1 = sta.executeUpdate("INSERT INTO DataCoffees VALUES(1,'Colombian')");
int set2 = sta.executeUpdate("INSERT INTO DataCoffees VALUES(2,'French_Roast')");
int set3 = sta.executeUpdate("INSERT INTO DataCoffees VALUES(3,'Espresso')");
int set4 = sta.executeUpdate("INSERT INTO DataCoffees VALUES(4,'Colombian_Decaf')");
int set5 = sta.executeUpdate("INSERT INTO DataCoffees VALUES(5,'French_Roast_Decaf')");
}
public void fillInventory()throws SQLException{
int set1 = sta.executeUpdate("INSERT INTO DataInventory VALUES(1,100)");
int set2 = sta.executeUpdate("INSERT INTO DataInventory VALUES(2,300)");
int set3 = sta.executeUpdate("INSERT INTO DataInventory VALUES(3,250)");
int set4 = sta.executeUpdate("INSERT INTO DataInventory VALUES(4,500)");
int set5 = sta.executeUpdate("INSERT INTO DataInventory VALUES(5,750)");
}
public void consultTableSales()throws SQLException{
ResultSet query = sta.executeQuery("SELECT cof_id,sup_id,price FROM Sales WHERE cof_id=3 OR cof_id=5");
while(query.next()){
System.out.println( query.getInt(1)+", "+query.getInt(2)+", "+query.getInt(3));
}
}
public static void main (String args[]) {
try{
GestionCafe gc = new GestionCafe();
//gc.createTables();
//gc.fillSales();
//gc.fillSuppliers();
//gc.fillCoffees();
//gc.fillInventory();
gc.consultTableSales();
con.close();
}
catch(ClassNotFoundException ce){
System.out.println("Class not found exception "+ ce.getMessage());
}
catch(SQLException se){
System.out.println("SQL Exception occured "+ se.getMessage());
System.out.println("SQL Exception occured "+ se.getSQLState());
}
}
}
import java.sql.*;
import java.util.*;
import java.sql.Connection;
import java.sql.DriverManager;
public class Connect {
public static Connection Conn;
public static int Choix;
public static void afficherDrivers()
{
//affichage de la liste des drivers charges
Enumeration drivers=DriverManager.getDrivers();
System.out.println("Liste des drivers charges");
while(drivers.hasMoreElements())
{
System.out.println(drivers.nextElement());
}
}
/*----------------------------------------------------
Cette methode permet de charger le driver necessaire
pour acceder a la base de donnée MySQL
----------------------------------------------------*/
public static void ChargerDriver()
{
//chargement du driver
try
{
Class.forName("org.gjt.mm.mysql.Driver");
}
catch(ClassNotFoundException e)
{
System.out.println("erreur: "+e);
}
}
/*----------------------------------------------------
Cette methode nous permet de nous connecter
a la base de donnée
----------------------------------------------------*/
public static Connection ConnexionBase()
{
try
{//Connection ConnAccess=DriverManager.getConnection("jdbc:odbc:BD1");
Conn=DriverManager.getConnection("jdbc:odbc:xindoc");
System.out.println("Connection MySQL effectuee\n\n\n");
Conn.setAutoCommit(false);
Conn.close();
}
catch(SQLException e)
{
System.out.println("erreur ConnexionBase"+e);
}
return Conn;
}
}
Combien cela coûte-t-il au total ? Quelles aides apportent l'état et les acteurs du marché pour alléger cette charge non choisie ? Tous les détails sur Commentçamarche.net.