Ver Mensaje Individual
  #4  
Viejo 20/09/11, 17:31:20
Array

[xs_avatar]
RubenGM85 RubenGM85 no está en línea
Miembro del foro
 
Fecha de registro: mar 2010
Localización: Girona
Mensajes: 435
Modelo de smartphone: LG Nexus 5
Tu operador: Vodafone
Descarga el conector oficial y gratuito de MySQL para Java (link) y haz algo tal que así:

Código:
	private Connection conecta() {
		try {
			String url = "jdbc:mysql://IPdelServidor:3306/nombre_de_base_de_datos";
			con = (Connection) DriverManager.getConnection( url,"usuario", "password");
		} catch (Exception e) {
		}
	}
Para hacer una query:

Código:
		try {
			PreparedStatement st = (PreparedStatement)con.prepareStatement("SELECT * FROM tabla WHERE algo LIKE ?");
			st.setString(1, "algunString");
			ResultSet res = st.executeQuery();
			res.first();
			while(!res.isAfterLast()) {
				int id = res.getInt("id");
				String nombre = res.getString("campoNombre");
				//etc
				res.next();
			}
		} catch (Exception e) {
			Log(e);
		}
¿Quieres hacer un UPDATE o DELETE?
Código:
			try {
				//Si tienes un String con la query entera, usa Statement. Si tienes que meterle variables usa PreparedStatement
				st = (Statement)con.createStatement();
				st.execute("DELETE FROM tabla WHERE id = 800"); //o p.ej.: UPDATE tabla SET meh = 'asd' WHERE id = 2
				st.close();
			} catch (Exception e) {
				Log(e);
			}
Recuerda acabar siempre haciendo .close() de las conexiones, los results y los statements.
Responder Con Cita