Advertisement
Guest User

Untitled

a guest
May 4th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.49 KB | None | 0 0
  1. package Main;
  2.  
  3. import java.sql.*;
  4. import javax.imageio.ImageIO;
  5. import javax.swing.*;
  6. import java.io.*;
  7. import java.awt.*;
  8.  
  9.  
  10. /**
  11. * Basic connection to MySQL (MariaDB) database.
  12. * Conexión básica a la base de datos MySQL (MariaDB).
  13. */
  14. public class JavaMySQLBasic {
  15.  
  16. /**
  17. * We establish the connection with the database <b>customerdb</b>.
  18. * Establecemos la conexión con la base de datos <b>customerdb</b>.
  19. */
  20. public void connectDatabase() {
  21. Connection connection = null;
  22. try {
  23. // We register the MySQL (MariaDB) driver
  24. // Registramos el driver de MySQL (MariaDB)
  25. try {
  26. Class.forName("org.mariadb.jdbc.Driver");
  27. } catch (ClassNotFoundException ex) {
  28. System.out.println("Error al registrar el driver de MySQL: " + ex);
  29. }
  30.  
  31. // Conectamos con la base de datos
  32. connection = DriverManager.getConnection(
  33. "jdbc:mariadb://esp.uem.es:3306/test","test", "test");
  34. boolean valid = connection.isValid(50000);
  35. System.out.println(valid ? "TEST OK" : "TEST FAIL");
  36. } catch (java.sql.SQLException sqle) {
  37. System.out.println("Error: " + sqle);
  38. }
  39. try {
  40. // our SQL SELECT query.
  41. // if you only need a few columns, specify them by name instead of using "*"
  42. String query = "SELECT * FROM personas";
  43.  
  44. // create the java statement
  45. Statement st = connection.createStatement();
  46.  
  47. // execute the query, and get a java resultset
  48. ResultSet rs = st.executeQuery(query);
  49.  
  50. // iterate through the java resultset
  51. while (rs.next())
  52. {
  53. String nombre = rs.getString("Nombre");
  54. int edad = rs.getInt("edad");
  55.  
  56.  
  57. // print the results
  58. System.out.format("%s, %d\n", nombre, edad);
  59. }
  60.  
  61. }catch(java.sql.SQLException sqle) {
  62. System.out.println("Error: " + sqle);
  63. }
  64. try {
  65. File image = new File("c:/foto.jpg");
  66. FileInputStream fis = new FileInputStream ( image );
  67.  
  68. String sql="update personas set Img=? where Nombre='diego'";
  69. PreparedStatement pst;
  70. pst = connection.prepareStatement(sql);
  71.  
  72. //pst.setInt(1, 10);
  73. pst.setBinaryStream(1, fis);
  74.  
  75. pst.executeUpdate();
  76.  
  77. }catch(Exception e) {
  78. System.out.println("Error: " + e);
  79. }
  80. }
  81.  
  82. public void showImag(){
  83. Image image = null;
  84. Connection connection = null;
  85.  
  86. try {
  87. Class.forName("org.mariadb.jdbc.Driver");
  88.  
  89. // Conectamos con la base de datos
  90. connection = DriverManager.getConnection(
  91. "jdbc:mariadb://esp.uem.es:3306/test","test", "test");
  92. boolean valid = connection.isValid(50000);
  93. System.out.println(valid ? "TEST OK" : "TEST FAIL");
  94.  
  95. // our SQL SELECT query.
  96. // if you only need a few columns, specify them by name instead of using "*"
  97. String query = "SELECT Img FROM personas where Nombre='diego'";
  98.  
  99. // create the java statement
  100. Statement st = connection.createStatement();
  101.  
  102. // execute the query, and get a java resultset
  103. ResultSet rs = st.executeQuery(query);
  104.  
  105. // iterate through the java resultset
  106. rs.next();
  107.  
  108. InputStream is = rs.getBinaryStream("Img");
  109. image = ImageIO.read(is);
  110. } catch (Exception e) {
  111. System.out.println("Exception: " + e);
  112. }
  113.  
  114. // Use a label to display the image
  115. JFrame frame = new JFrame();
  116. JLabel label = new JLabel(new ImageIcon(image));
  117. frame.getContentPane().add(label, BorderLayout.CENTER);
  118. frame.pack();
  119. frame.setVisible(true);
  120. }
  121.  
  122. /**
  123. * Testing Java MySQL connection with host and port
  124. * Probando la conexión en Java a MySQL especificando el host y el puerto.
  125. * @param args the command line arguments
  126. */
  127. public static void main(String[] args) {
  128. JavaMySQLBasic javaMySQLBasic = new JavaMySQLBasic();
  129. javaMySQLBasic.connectDatabase();
  130. javaMySQLBasic.showImag();
  131. }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement