lombate escribió
En internet solo encuentro conexiones a servidores y me estoy volviendo loco.
Pues cambia el nombre del servidor por la ip local, 127.0.0.1
A ver si te vale con este ejemplo:
public class PruebaMySql {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(
"jdbc:mysql://127.0.0.1:3306/prueba", "root", "root");
//prueba es el nombre de la base de datos, root el usuario y root la contraseña
Statement stmt = con.createStatement();
String SQL = "SELECT * FROM alumnos";
//alumnos es una tabla
ResultSet rs = stmt.executeQuery(SQL);
try {
System.out.println("Número de filas: " + numeroFilas(rs)
+ System.lineSeparator());
System.out.println(muestraData(rs));
} catch (Exception e) {
System.out.println("No se puede realizar la consulta");
}
rs.close();
stmt.close();
con.close();
} catch (SQLException e) {
System.out.println("SQL Exception: " + e.toString());
} catch (ClassNotFoundException cE) {
System.out.println("Class Not Found Exception: " + cE.toString());
}
}
public static int numeroFilas(ResultSet r) throws SQLException {
r.last();
return r.getRow();
}
public static String muestraData(ResultSet r) throws Exception {
r.beforeFirst();
String s = "";
ResultSetMetaData rmeta = r.getMetaData();
int numColumnas = rmeta.getColumnCount();//Cuántas columnas
for (int i = 1; i <= numColumnas; ++i) //obtiene nombre de columna
{
s += rmeta.getColumnName(i) + "\t";
}
s += "\n";
while (r.next()) { // hasta fin de archivo
for (int i = 1; i <= numColumnas; ++i) {
s += r.getString(i) + "\t";
}
s += "\n";
}
return s;
}
}