ajout BD pour recuperer les score, ajouter son score dans la BD et recuperer les tuile
This commit is contained in:
parent
bddb013f02
commit
dfa6ce6558
54
AllScore.java
Normal file
54
AllScore.java
Normal file
@ -0,0 +1,54 @@
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class AllScore {
|
||||
|
||||
public static ArrayList<Integer> getScoresForSeries(int idSerie) {
|
||||
ArrayList<Integer> scores = new ArrayList<>();
|
||||
|
||||
try { Class.forName("org.mariadb.jdbc.Driver");
|
||||
}catch(ClassNotFoundException e ){
|
||||
System.err.println("erreur db");
|
||||
System.exit(1);
|
||||
|
||||
};
|
||||
|
||||
try { Connection cnx = DriverManager.getConnection(
|
||||
"jdbc:mariadb://dwarves.iut-fbleau.fr/akagundu",
|
||||
"akagundu", "dersim62Lodek");
|
||||
try{
|
||||
PreparedStatement pst = cnx.prepareStatement("SELECT score from score where id_serie=?;");
|
||||
pst.setInt(1, idSerie);
|
||||
ResultSet rs = pst.executeQuery();
|
||||
while(rs.next()) {
|
||||
scores.add(rs.getInt(1));
|
||||
}
|
||||
rs.close();
|
||||
pst.close();
|
||||
cnx.close();
|
||||
|
||||
}
|
||||
catch(SQLException e ){
|
||||
System.err.println("erreur aff");
|
||||
System.exit(2);
|
||||
}
|
||||
}catch(SQLException e ){
|
||||
System.err.println("erreur cn");
|
||||
System.exit(2);
|
||||
};
|
||||
|
||||
|
||||
return scores;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int idSerie = 1;
|
||||
ArrayList<Integer> scores = getScoresForSeries(idSerie);
|
||||
|
||||
System.out.println("Scores for series " + idSerie + ": " + scores);
|
||||
}
|
||||
}
|
64
RecupTuile.java
Normal file
64
RecupTuile.java
Normal file
@ -0,0 +1,64 @@
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class RecupTuile {
|
||||
|
||||
public String[][] recup(int idSerie) {
|
||||
String[][] tuiles = new String[3][50];
|
||||
int index = 0;
|
||||
|
||||
try {
|
||||
Class.forName("org.mariadb.jdbc.Driver");
|
||||
} catch (ClassNotFoundException e) {
|
||||
System.err.println("erreur db");
|
||||
return tuiles;
|
||||
}
|
||||
|
||||
try (Connection cnx = DriverManager.getConnection(
|
||||
"jdbc:mariadb://dwarves.iut-fbleau.fr/akagundu",
|
||||
"akagundu", "dersim62Lodek")) {
|
||||
|
||||
String query = "SELECT couleur1, couleur2, nombre FROM Tuile WHERE id_serie = ?";
|
||||
|
||||
try (PreparedStatement pst = cnx.prepareStatement(query)) {
|
||||
pst.setInt(1, idSerie);
|
||||
|
||||
try (ResultSet rs = pst.executeQuery()) {
|
||||
while (rs.next() && index < 50) {
|
||||
String couleur1 = rs.getString(1);
|
||||
String couleur2 = rs.getString(2);
|
||||
int nombre = rs.getInt(3);
|
||||
|
||||
// Remplir le tableau
|
||||
tuiles[0][index] = couleur1 != null ? couleur1 : "null";
|
||||
tuiles[1][index] = couleur2 != null ? couleur2 : "null";
|
||||
tuiles[2][index] = String.valueOf(nombre);
|
||||
|
||||
index++;
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
System.err.println("erreur aff " + e.getMessage());
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
System.err.println("erreur cn " + e.getMessage());
|
||||
}
|
||||
|
||||
return tuiles;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
RecupTuile recupTuile = new RecupTuile();
|
||||
int idSerie = 1;
|
||||
String[][] tuiles = recupTuile.recup(idSerie);
|
||||
|
||||
for (int i = 0; i < 50; i++) {
|
||||
if (tuiles[0][i] != null) {
|
||||
System.out.println("Tuile " + (i + 1) + ": Couleur 1 = " + tuiles[0][i] + ", Couleur 2 = " + tuiles[1][i] + ", Nombre = " + tuiles[2][i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
46
SendScore.java
Normal file
46
SendScore.java
Normal file
@ -0,0 +1,46 @@
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class SendScore {
|
||||
public void insertscore(int idSerie,int score) {
|
||||
try { Class.forName("org.mariadb.jdbc.Driver");
|
||||
}catch(ClassNotFoundException e ){
|
||||
System.err.println("erreur db");
|
||||
System.exit(1);
|
||||
|
||||
};
|
||||
|
||||
try { Connection cnx = DriverManager.getConnection(
|
||||
"jdbc:mariadb://dwarves.iut-fbleau.fr/akagundu",
|
||||
"akagundu", "dersim62Lodek");
|
||||
try{
|
||||
PreparedStatement pst = cnx.prepareStatement("INSERT INTO score (id_serie, score) VALUES (?, ?);");
|
||||
pst.setInt(1, idSerie);
|
||||
pst.setInt(2, score);
|
||||
pst.executeUpdate();
|
||||
pst.close();
|
||||
cnx.close();
|
||||
|
||||
}
|
||||
catch(SQLException e ){
|
||||
System.err.println("erreur aff");
|
||||
System.exit(2);
|
||||
}
|
||||
}catch(SQLException e ){
|
||||
System.err.println("erreur cn");
|
||||
System.exit(2);
|
||||
};
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int idSerie = 1;
|
||||
int score=111;
|
||||
SendScore sendScore = new SendScore();
|
||||
sendScore.insertscore(idSerie, score);
|
||||
System.out.println("Scores for series " + idSerie + ": " + score);
|
||||
}
|
||||
}
|
BIN
mariadb-java-client-3.4.1.jar
Normal file
BIN
mariadb-java-client-3.4.1.jar
Normal file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user