First you must have the connector for the java database.
Second load the driver
and then perform the consultations
Here I give you an example to guide you
The driver
public Connection getConexion() {
Connection conexion = null;
String url=null;
try {
Class.forName("com.mysql.jdbc.Driver");
if(this.getDireccion().equalsIgnoreCase("localhost")){//se conecta en modo local
url = "jdbc:mysql://localhost"; //personal
conexion = DriverManager.getConnection(url,"root","clave");//conexion normal superusuario
}else{//se conecta en modo remoto
url="jdbc:mysql://"+this.getDireccion();
conexion = DriverManager.getConnection(url,"usuario","clave");//conexion Servidor remoto
}
System.out.println("Conexion establecida con exito!");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(
new JFrame(),e.getLocalizedMessage()+
"Base de Datos no encontrada",
"ADVERTENCIA",
JOptionPane.INFORMATION_MESSAGE);
System.out.println(e.getLocalizedMessage()+"Base de Datos no encontrada");
}
return conexion;
}
Inquiry
public void buscar_materias_alternativo(Connection con, String ci,String tabla) {
this.getRecord().clear();//limpiando el LinkedList
System.out.println("TABLA: "+tabla+" - CEDULA: "+ci);
this.setReconocimiento(0);
Statement sentencia = null;
ResultSet buscar = null;
try {
sentencia = con.createStatement();
buscar = sentencia.executeQuery("SELECT * FROM control_de_estudio."+tabla+" WHERE CEDULA='"+ ci + "' ORDER BY TERMINO;");
while(buscar.next()){
// System.out.println(buscar.getString("CEDULA"));
//cargando todas las materias del alumno en memoria
this.getRecord().add(buscar.getString("CODMAT")); //0
this.getRecord().add(buscar.getString("CODMAT")); //1
this.getRecord().add(String.valueOf(buscar.getInt("NOTDEF"))); //2
this.getRecord().add(String.valueOf(buscar.getInt("NOTREP"))); //3
this.getRecord().add(buscar.getString("CONDIC")); //4
this.getRecord().add(buscar.getString("PERACA")); //5
this.getRecord().add(String.valueOf(buscar.getInt("DEFREP"))); //6
this.getRecord().add(String.valueOf(buscar.getInt("PORINA"))); //7
this.getRecord().add(buscar.getString("CODESP")); //8
this.getRecord().add(String.valueOf(buscar.getInt("NOTLAB"))); //9
this.getRecord().add(String.valueOf(buscar.getInt("TERMINO"))); //10
}
sentencia.close();
buscar.close();
con.close();
}catch (SQLException ex) {
this.setReconocimiento(1);
Logger.getLogger(registro_ingenieria.class.getName()).log(Level.SEVERE, null, ex);
System.out.println("metodo buscar materia alternativo ");
}
}