Advertisement
Guest User

KOd

a guest
Nov 21st, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. import javax.swing.JFrame;
  2. import javax.swing.JButton;
  3. import java.awt.BorderLayout;
  4. import java.awt.event.*;
  5. import javax.swing.JTextField;
  6. import javax.swing.JLabel;
  7. import javax.swing.JOptionPane;
  8. import javax.swing.JScrollPane;
  9. import javax.swing.JTextArea;
  10. import java.sql.*;
  11.  
  12. public class DatabaseViewer extends JFrame implements ActionListener, MouseMotionListener,KeyListener {
  13.  
  14. public void Połączenie()
  15. {
  16.  
  17. Connection connection = null;
  18. String connectionString = "jdbc:sqlserver://localhost:1433;databaseName=Northwind;user=Baza_Testowa;password=Testbazy123;";
  19.  
  20. try {
  21.  
  22. connection = DriverManager.getConnection(connectionString);
  23. String SQL = Polecenie.getText();
  24. Statement stmt = connection.createStatement();
  25. ResultSet rs = stmt.executeQuery(SQL);
  26. String temp=new String();
  27.  
  28. ResultSetMetaData rsmd=rs.getMetaData();
  29. int liczba_kolumn=rsmd.getColumnCount();
  30. while (rs.next()) {
  31. for (int i=1;i<=liczba_kolumn;i++)
  32. {
  33. if(i!=liczba_kolumn)
  34. {
  35. temp+=rs.getString(i)+" ";
  36. }
  37. if(i==liczba_kolumn)
  38. {
  39. temp+=rs.getString(i)+"\n";
  40. }
  41. }
  42. }
  43. Tekst.setText(temp);
  44.  
  45. }
  46. catch (SQLException e) {
  47. JOptionPane.showConfirmDialog(this,e.getMessage(),"Error", JOptionPane.OK_OPTION);
  48.  
  49.  
  50.  
  51. }
  52. finally {
  53. if (connection != null) try { connection.close(); } catch(Exception e) {}
  54. }
  55. }
  56. JButton Wyczysc;
  57. JButton Wykonaj;
  58. JTextArea Tekst;
  59. JLabel Pozycja;
  60. JScrollPane Pasek;
  61. int mX,mY;
  62. JTextField Polecenie;
  63. public DatabaseViewer() {
  64. super("Hello World");
  65. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  66. this.setVisible(true);}
  67. public void initComponents ()
  68. {
  69. Pozycja=new JLabel();
  70. Pozycja.setSize(200, 150);
  71. Pozycja.setText("Tekst");
  72. this.getContentPane().add(BorderLayout.NORTH, Pozycja);
  73. Wyczysc = new JButton("Wyczyść");
  74. Wyczysc.setSize(200, 150);
  75. this.getContentPane().add(BorderLayout.WEST, Wyczysc);
  76. Wyczysc.addActionListener(this);
  77. Wykonaj = new JButton("Wykonaj");
  78. Wykonaj.setSize(200,150);
  79. Wykonaj.addActionListener(this);
  80. this.getContentPane().add(BorderLayout.EAST, Wykonaj);
  81. Tekst = new JTextArea();
  82. this.getContentPane().add(BorderLayout.CENTER, Tekst);
  83.  
  84. Tekst.setEditable(false);
  85. Polecenie=new JTextField();
  86. this.getContentPane().add(BorderLayout.SOUTH, Polecenie);
  87. addMouseMotionListener(this);
  88. Polecenie.addActionListener(this);
  89. Polecenie.addKeyListener(this);
  90. Wyczysc.addMouseMotionListener(this);
  91. Wykonaj.addMouseMotionListener(this);
  92. Tekst.addMouseMotionListener(this);
  93. Pozycja.addMouseMotionListener(this);
  94. }
  95. public void actionPerformed(ActionEvent akcja) {
  96. if( akcja.getSource()==Wyczysc )
  97. {
  98. Tekst.setText("");
  99. Polecenie.setText("");
  100. }
  101. if( akcja.getSource()==Wykonaj )
  102. {
  103. Połączenie();
  104. }
  105.  
  106. }
  107.  
  108. @Override
  109. public void mouseMoved(MouseEvent me) {
  110. mX = (int) me.getPoint().getX();
  111. mY = (int) me.getPoint().getY();
  112. Pozycja.setText("Współrzędne myszy: x="+mX+" y="+mY+" .");
  113. }
  114. @Override
  115. public void mouseDragged(MouseEvent e) {
  116.  
  117. }
  118. public void keyPressed(KeyEvent e) {
  119. int key = e.getKeyCode();
  120. if (key == KeyEvent.VK_ENTER)
  121. {
  122. Połączenie();
  123. }
  124. }
  125. @Override
  126. public void keyReleased(KeyEvent arg0) {
  127. // TODO Auto-generated method stub
  128.  
  129. }
  130. @Override
  131. public void keyTyped(KeyEvent arg0) {
  132. // TODO Auto-generated method stub
  133.  
  134. }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement