Il me semble, voici un exemple d'un exercice
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());
}
}
}
Chouba
Casque Bleu forumique