Advertisement
Guest User

Untitled

a guest
Nov 7th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.10 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package put.pp.jse.mo;
  7.  
  8. import com.sun.rowset.CachedRowSetImpl;
  9. import java.awt.event.KeyEvent;
  10. import java.sql.Connection;
  11. import java.sql.DriverManager;
  12. import java.sql.PreparedStatement;
  13. import java.sql.ResultSet;
  14. import java.sql.SQLException;
  15. import java.sql.Statement;
  16. import javax.sql.rowset.CachedRowSet;
  17.  
  18. /**
  19. *
  20. * @author student
  21. */
  22. public class DatabaseViewer extends javax.swing.JFrame {
  23.  
  24. /**
  25. * Creates new form DatabaseViewer
  26. */
  27. public DatabaseViewer() {
  28. initComponents();
  29. }
  30.  
  31. /**
  32. * This method is called from within the constructor to initialize the form.
  33. * WARNING: Do NOT modify this code. The content of this method is always
  34. * regenerated by the Form Editor.
  35. */
  36. @SuppressWarnings("unchecked")
  37. // <editor-fold defaultstate="collapsed" desc="Generated Code">
  38. private void initComponents() {
  39.  
  40. jOptionPane1 = new javax.swing.JOptionPane();
  41. jScrollPane1 = new javax.swing.JScrollPane();
  42. jTextArea1 = new javax.swing.JTextArea();
  43. jButton1 = new javax.swing.JButton();
  44. jButton2 = new javax.swing.JButton();
  45. jLabel1 = new javax.swing.JLabel();
  46.  
  47. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  48. addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
  49. public void mouseMoved(java.awt.event.MouseEvent evt) {
  50. formMouseMoved(evt);
  51. }
  52. });
  53.  
  54. jTextArea1.setColumns(20);
  55. jTextArea1.setRows(5);
  56. jTextArea1.addKeyListener(new java.awt.event.KeyAdapter() {
  57. public void keyPressed(java.awt.event.KeyEvent evt) {
  58. jTextArea1KeyPressed(evt);
  59. }
  60. });
  61. jScrollPane1.setViewportView(jTextArea1);
  62. jTextArea1.getAccessibleContext().setAccessibleName("poletekstowe");
  63.  
  64. jButton1.setText("Wyczysc");
  65. jButton1.addActionListener(new java.awt.event.ActionListener() {
  66. public void actionPerformed(java.awt.event.ActionEvent evt) {
  67. jButton1ActionPerformed(evt);
  68. }
  69. });
  70.  
  71. jButton2.setText("Wykonaj");
  72. jButton2.addActionListener(new java.awt.event.ActionListener() {
  73. public void actionPerformed(java.awt.event.ActionEvent evt) {
  74. jButton2ActionPerformed(evt);
  75. }
  76. });
  77.  
  78. jLabel1.setText("Pozycja myszy: x=100 y=100");
  79.  
  80. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  81. getContentPane().setLayout(layout);
  82. layout.setHorizontalGroup(
  83. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  84. .addGroup(layout.createSequentialGroup()
  85. .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
  86. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  87. .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 413, javax.swing.GroupLayout.PREFERRED_SIZE)
  88. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  89. .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  90. .addGroup(layout.createSequentialGroup()
  91. .addGap(23, 23, 23)
  92. .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 590, javax.swing.GroupLayout.PREFERRED_SIZE)
  93. .addContainerGap(32, Short.MAX_VALUE))
  94. );
  95. layout.setVerticalGroup(
  96. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  97. .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  98. .addGap(27, 27, 27)
  99. .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 28, Short.MAX_VALUE)
  100. .addGap(18, 18, 18)
  101. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  102. .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  103. .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 348, Short.MAX_VALUE)
  104. .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
  105. );
  106.  
  107. pack();
  108. }// </editor-fold>
  109.  
  110. private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
  111. jTextArea1.setText("");
  112. }
  113.  
  114. private void formMouseMoved(java.awt.event.MouseEvent evt) {
  115. jLabel1.setText("Pozycja myszy: x=" + evt.getX() + " y=" + evt.getY());
  116. }
  117.  
  118. private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
  119.  
  120. this.dbCon();
  121.  
  122. }
  123.  
  124. private void jTextArea1KeyPressed(java.awt.event.KeyEvent evt) {
  125. if(evt.getKeyCode() == KeyEvent.VK_ENTER)
  126. {
  127. this.dbCon();
  128. }
  129. }
  130.  
  131. private void dbCon()
  132. {
  133. String connectionUrl = "jdbc:sqlserver://localhost:1433;" +
  134. "databaseName=NORTHWND;user=maciek;password=maciekmaciek";
  135.  
  136. // Declare the JDBC objects.
  137. Connection con = null;
  138. Statement stmt = null;
  139. ResultSet rs = null;
  140.  
  141. try {
  142. // Establish the connection.
  143. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  144. con = DriverManager.getConnection(connectionUrl);
  145.  
  146. // Create and execute an SQL statement that returns some data.
  147. String SQL = jTextArea1.getText();
  148. stmt = con.createStatement();
  149. rs = stmt.executeQuery(SQL);
  150.  
  151. String result = "";
  152. while (rs.next()) {
  153. result += "\n " + rs.getString("ContactName");
  154. }
  155.  
  156. jTextArea1.setText(result);
  157.  
  158. con.close();
  159. }
  160.  
  161. // Handle any errors that may have occurred.
  162. catch (Exception e) {
  163. e.printStackTrace();
  164. jOptionPane1.showMessageDialog (null, e.getMessage(), "Error", jOptionPane1.INFORMATION_MESSAGE);
  165. }
  166. finally {
  167. if (rs != null) try { rs.close(); } catch(Exception e) {}
  168. if (stmt != null) try { stmt.close(); } catch(Exception e) {}
  169. if (con != null) try { con.close(); } catch(Exception e) {}
  170. }
  171. }
  172.  
  173. /**
  174. * @param args the command line arguments
  175. */
  176. public static void main(String args[]) {
  177. /* Set the Nimbus look and feel */
  178. //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  179. /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  180. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  181. */
  182. try {
  183. for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  184. if ("Nimbus".equals(info.getName())) {
  185. javax.swing.UIManager.setLookAndFeel(info.getClassName());
  186. break;
  187. }
  188. }
  189. } catch (ClassNotFoundException ex) {
  190. java.util.logging.Logger.getLogger(DatabaseViewer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  191. } catch (InstantiationException ex) {
  192. java.util.logging.Logger.getLogger(DatabaseViewer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  193. } catch (IllegalAccessException ex) {
  194. java.util.logging.Logger.getLogger(DatabaseViewer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  195. } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  196. java.util.logging.Logger.getLogger(DatabaseViewer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  197. }
  198. //</editor-fold>
  199.  
  200. /* Create and display the form */
  201. java.awt.EventQueue.invokeLater(new Runnable() {
  202. public void run() {
  203. new DatabaseViewer().setVisible(true);
  204. }
  205. });
  206. }
  207.  
  208. // Variables declaration - do not modify
  209. private javax.swing.JButton jButton1;
  210. private javax.swing.JButton jButton2;
  211. private javax.swing.JLabel jLabel1;
  212. private javax.swing.JOptionPane jOptionPane1;
  213. private javax.swing.JScrollPane jScrollPane1;
  214. private javax.swing.JTextArea jTextArea1;
  215. // End of variables declaration
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement