1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
package connexiondb;
import java.sql.*;
/**
*
* @author guill
*/
public class ConnexionDB {
static Connection cnx;
static Statement st;
static ResultSet rst;
public static Connection connecterDB() throws ClassNotFoundException, SQLException{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String dataSourceName = "BaseDeDonnee" + "";
String dbURL = "jdbc:odbc:" + dataSourceName;
Connection cnx = DriverManager.getConnection(dbURL, "","");
System.out.println("OK");
return cnx;
}
catch (Exception err) {
System.out.println( "Error: " + err );
return null;
}
}
public static void Ajouter_Image(String file_path,int color_percentage,String centered, int edge_complexity, int level){
try{
String query = "INSERT INTO Images VALUES('"+file_path+"',"+color_percentage+",'"+centered+"',"+edge_complexity+","+level+")";
cnx = connecterDB();
st=cnx.createStatement();
st.executeUpdate(query);
System.out.println("Ajouté");
st.close();
cnx.close();
}
catch (Exception err) {
System.out.println( "Error: " + err );
}
}
public static void Supprimer_Image(String path_file){
try{
String query = "DELETE FROM Images WHERE path_file=?";
cnx = connecterDB();
PreparedStatement ps =cnx.prepareStatement(query);
ps.setString(1,path_file);
ps.executeUpdate();
System.out.println("Bien supprimé !");
st.close();
cnx.close();
}
catch (Exception err) {
err.printStackTrace();
}
}
public static void main(String[] args) throws SQLException{
//Ajouter_Image("C:\\Users\\guill\\OneDrive\\Documents\\Stage Italie\\Photos_Java\\hello.jpg",65,"non", 80, 3);
//Supprimer_Image("C:/Users/guill/OneDrive/Documents/Stage Italie/Photos_Java/CIAO.jpg");
try{
cnx = connecterDB();
st=cnx.createStatement();
rst=st.executeQuery("SELECT * FROM Images");
while(rst.next()){
System.out.println(rst.getString("file_path"));
System.out.println(rst.getInt("color_percentage"));
System.out.println(rst.getString("centered"));
System.out.println(rst.getInt("edge_complexity"));
System.out.println(rst.getInt("level"));
}
rst.close();
st.close();
cnx.close();
}
catch (Exception err) {
System.out.println( "Error: " + err );
}
}
} |
Partager