Advertisement
Guest User

Untitled

a guest
Jan 11th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1.  
  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10. import java.util.Properties;
  11. import java.util.logging.Level;
  12. import java.util.logging.Logger;
  13. import javax.swing.JFrame;
  14. import javax.swing.JOptionPane;
  15. import javax.swing.JTextArea;
  16.  
  17. /*
  18. * To change this license header, choose License Headers in Project Properties.
  19. * To change this template file, choose Tools | Templates
  20. * and open the template in the editor.
  21. */
  22.  
  23. /**
  24. *
  25. * @author chrastek_martin
  26. */
  27. public class Okno extends JFrame{
  28.  
  29. JTextArea text = new JTextArea();
  30. Connection con = null;
  31.  
  32. public Okno() throws IOException{
  33. initComponents();
  34. pripojeni();
  35. //pridani();
  36. vypis();
  37. }
  38.  
  39.  
  40. private void initComponents(){
  41. this.setTitle("Databázový výpis");
  42. this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  43. this.setSize(275,120);
  44. this.setLocationRelativeTo(null);
  45. this.getContentPane().add(text);
  46.  
  47. }
  48.  
  49. /**
  50. * @param args the command line arguments
  51. */
  52. public static void main(String[] args) throws IOException {
  53. new Okno().setVisible(true);;
  54. }
  55.  
  56. private Connection pripojeni() throws FileNotFoundException, IOException {
  57. // Zkontrolujeme instalaci ovladače
  58. try {
  59. Class.forName("com.mysql.jdbc.Connection");
  60. } catch (ClassNotFoundException ex) {
  61. JOptionPane.showMessageDialog(
  62. null,
  63. "Ovladač databáze není správně zahrnut do projektu! Nenalezena třída: "+ ex.getMessage()+"!",
  64. "Chyba ovladače JDBC",
  65. JOptionPane.ERROR_MESSAGE);
  66. System.exit(1);
  67. }
  68.  
  69. try
  70. {
  71. Properties prop=new Properties();
  72. FileInputStream in = new FileInputStream("spojeni.properties");
  73. prop.load(in);
  74. in.close();
  75.  
  76. String host = prop.getProperty("host");
  77. String nazevDatabaze = prop.getProperty("nazevDatabaze");
  78. String userDB = prop.getProperty("userDB");
  79. String passDB = prop.getProperty("passDB");
  80. String url = "jdbc:mysql://" + host + "/" + nazevDatabaze;
  81.  
  82. con=DriverManager.getConnection(url,userDB,passDB);
  83.  
  84. System.out.println("Connection Successful");
  85. return con;
  86. }
  87. catch(SQLException ex)
  88. {
  89. JOptionPane.showMessageDialog(
  90. null,
  91. "Chyba při otevření spojení s s databází:"+ ex.getMessage(),
  92. "Chyba spojení s databází", JOptionPane.ERROR_MESSAGE);
  93. System.exit(1);
  94. }
  95. return null;
  96.  
  97. }
  98.  
  99. private void pridani() {
  100. Statement st = null;
  101.  
  102. try {
  103. st = con.createStatement();
  104.  
  105. st.executeUpdate("INSERT INTO Zaci VALUES ('Martin','Chrástek','4',2);");
  106.  
  107. } catch (SQLException ex) {
  108. Logger.getLogger(Okno.class.getName()).log(Level.SEVERE, null, ex);
  109. }
  110.  
  111. if (con != null) {
  112. try {
  113. con.close();
  114. } catch (SQLException ex) {
  115. Logger.getLogger(Okno.class.getName()).log(Level.SEVERE, null, ex);
  116. }
  117. }
  118. }
  119.  
  120. private void vypis() {
  121. Statement st = null;
  122.  
  123. try {
  124. st = con.createStatement();
  125. ResultSet rs = null;
  126.  
  127. rs = st.executeQuery("SELECT * FROM Zaci");
  128.  
  129. while (rs.next()){
  130. text.append(rs.getString(1)+ " ");
  131. text.append(rs.getString(2)+ ", ");
  132. text.append(rs.getString(3)+ ", ");
  133. text.append(rs.getString(4));
  134. text.append("\n");
  135. }
  136.  
  137. } catch (SQLException ex) {
  138. Logger.getLogger(Okno.class.getName()).log(Level.SEVERE, null, ex);
  139. }
  140.  
  141. if (con != null) {
  142. try {
  143. con.close();
  144. } catch (SQLException ex) {
  145. Logger.getLogger(Okno.class.getName()).log(Level.SEVERE, null, ex);
  146. }
  147. }
  148.  
  149. }
  150.  
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement