bonjour,
ce code permet de faire une connection a une base de donnes et la création d'une requête Select
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
//import java.sql.Timestamp;
//import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
 
import java.util.HashMap;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.Map;
import java.sql.*; 
import javax.swing.table.*;// Contient la classe AbstractTableModel 
import java.util.*;// pour utiliser la classe Vector
 
 
import org.cvisproject.cint.eda.provider.digitalmap.db.reader.DBReader;
import org.cvisproject.cint.eda.sdk.gis.coordinatesystem.positionning.RAWPosition;
import org.cvisproject.cint.eda.sdk.gis.interfaces.IMapPOI;
import org.cvisproject.cint.eda.sdk.gis.interfaces.IRoadElement;
import org.cvisproject.cint.eda.sdk.gis.interfaces.IMapArea;
import org.cvisproject.cint.eda.sdk.gis.interfaces.position.IRAWPosition;
import org.cvisproject.cint.eda.sdk.gis.mapformat.MapArea;
import org.cvisproject.cint.eda.sdk.gis.mapformat.MapPOI;
import org.cvisproject.cint.eda.sdk.gis.mapformat.RoadElement;
import org.cvisproject.cint.eda.sdk.gis.mapformat.RoadNode;
import org.cvisproject.cint.eda.sdk.logger.Logger;
 
import java.io.*;
import java.net.*;
public class basededonnes {
	protected IRAWPosition 					m_Pos;
	protected IRAWPosition 					m_BeginPos, m_EndPos;
 
	protected HashMap<String, IRoadElement> 		m_Elements;
	protected HashMap<String, IRoadElement> 		m_Elements_bis;
	private HashMap<Integer, IMapPOI> 		m_POI;
	//ouverture de la base de données
	static final private String driver = "com.mysql.jdbc.Driver";
	static final private String url_db = "jdbc:mysql://localhost/balicosal";
	static final private String user   = "root";
	static final private String passwd = "";
	Connection con=null;
	Statement s = null;	
	int i=0;
	//fin
 
	public basededonnes() {
		//ouverture de base
		ConnectBase(); 
 
		m_BeginPos    = null;
		m_EndPos      = null;
 
		m_Elements    = new HashMap<String, IRoadElement>();
		m_Elements_bis    = new HashMap<String, IRoadElement>();
	}
 
	public void ConnectBase() {
 
		try {
			/** Etape 1: charger le pilote */
			Class.forName(driver);
 
			DriverManager.registerDriver(new com.mysql.jdbc.Driver());
			/** Etape 2: Etablissement de la connexion à la base de données */
			con = DriverManager.getConnection(url_db, user, passwd);
 
			/** Etape 3: produire le Statement */
			s = con.createStatement();
 
			System.out.println("driver load successfully");
 
		} catch (Exception e) {
			System.out.println("echec pilote : " + e);
			System.exit(1);
		}		
	}
 
	public void DeConnectBase() {
 
		try {
			con.close();
 
		} catch (Exception e) {
 
		}
 
	}
public synchronized Collection<IMapPOI> getPOIVehiculeIn(IRAWPosition _beg, IRAWPosition _end) {
 
		String attributes;
		String table;
		String clause;
		String order;
		ResultSet res;
		double LATITUDE;
		double LONGITUDE;
		int VALUE;
		int ID_POI;
		RAWPosition rp = null;
 
		//récupération géomètrie
		String typePanel="Vehicule";
		attributes   = "id_poi,latitude,longitude,value";
		table   = "poi";
		clause = "(longitude BETWEEN "+ _beg.get_long() + " AND " + _end.get_long() + " AND " +
	    "latitude BETWEEN "+ _beg.get_lat()  + " AND "  + _end.get_lat() + ")" + " AND type = " + "\""+ typePanel + "\"" ;
		order="id_poi";
		try{
			this.s.executeQuery("SELECT " + attributes + " FROM " + table + " WHERE " + clause+ " ORDER BY " + order);
			res =s.getResultSet();
			res.beforeFirst();
			while (res.next()) {
				ID_POI=res.getInt("ID_POI");
				LATITUDE   = res.getDouble("latitude");
				LONGITUDE = res.getDouble("longitude");	
				VALUE = res.getInt("value");	
				MapPOI poi=new MapPOI(ID_POI, LATITUDE, LONGITUDE,VALUE);
				m_POI.put(ID_POI, poi);		
			} 
 
		} catch (Exception e) {
			System.out.println("Probleme lecture DB POI ");
		}
		setArrayPOI(m_POI);
 
 
		return m_POI.values();
}	
private void setArrayPOI(HashMap<Integer, IMapPOI> _m_POI){
	m_POI=_m_POI;
}
maintenant je veux envoyer cette requête via les Sockets vers un serveur distant.
si quelqu’un peut m'aider soit par code ou information merci d'avance.