Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package put.pp.jse.mo;
- import com.sun.rowset.CachedRowSetImpl;
- import java.awt.event.KeyEvent;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import javax.sql.rowset.CachedRowSet;
- /**
- *
- * @author student
- */
- public class DatabaseViewer extends javax.swing.JFrame {
- /**
- * Creates new form DatabaseViewer
- */
- public DatabaseViewer() {
- initComponents();
- }
- /**
- * This method is called from within the constructor to initialize the form.
- * WARNING: Do NOT modify this code. The content of this method is always
- * regenerated by the Form Editor.
- */
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- private void initComponents() {
- jOptionPane1 = new javax.swing.JOptionPane();
- jScrollPane1 = new javax.swing.JScrollPane();
- jTextArea1 = new javax.swing.JTextArea();
- jButton1 = new javax.swing.JButton();
- jButton2 = new javax.swing.JButton();
- jLabel1 = new javax.swing.JLabel();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
- public void mouseMoved(java.awt.event.MouseEvent evt) {
- formMouseMoved(evt);
- }
- });
- jTextArea1.setColumns(20);
- jTextArea1.setRows(5);
- jTextArea1.addKeyListener(new java.awt.event.KeyAdapter() {
- public void keyPressed(java.awt.event.KeyEvent evt) {
- jTextArea1KeyPressed(evt);
- }
- });
- jScrollPane1.setViewportView(jTextArea1);
- jTextArea1.getAccessibleContext().setAccessibleName("poletekstowe");
- jButton1.setText("Wyczysc");
- jButton1.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton1ActionPerformed(evt);
- }
- });
- jButton2.setText("Wykonaj");
- jButton2.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton2ActionPerformed(evt);
- }
- });
- jLabel1.setText("Pozycja myszy: x=100 y=100");
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 413, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- .addGroup(layout.createSequentialGroup()
- .addGap(23, 23, 23)
- .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 590, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addContainerGap(32, Short.MAX_VALUE))
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
- .addGap(27, 27, 27)
- .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 28, Short.MAX_VALUE)
- .addGap(18, 18, 18)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 348, Short.MAX_VALUE)
- .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
- );
- pack();
- }// </editor-fold>
- private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
- jTextArea1.setText("");
- }
- private void formMouseMoved(java.awt.event.MouseEvent evt) {
- jLabel1.setText("Pozycja myszy: x=" + evt.getX() + " y=" + evt.getY());
- }
- private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
- this.dbCon();
- }
- private void jTextArea1KeyPressed(java.awt.event.KeyEvent evt) {
- if(evt.getKeyCode() == KeyEvent.VK_ENTER)
- {
- this.dbCon();
- }
- }
- private void dbCon()
- {
- String connectionUrl = "jdbc:sqlserver://localhost:1433;" +
- "databaseName=NORTHWND;user=maciek;password=maciekmaciek";
- // Declare the JDBC objects.
- Connection con = null;
- Statement stmt = null;
- ResultSet rs = null;
- try {
- // Establish the connection.
- Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
- con = DriverManager.getConnection(connectionUrl);
- // Create and execute an SQL statement that returns some data.
- String SQL = jTextArea1.getText();
- stmt = con.createStatement();
- rs = stmt.executeQuery(SQL);
- String result = "";
- while (rs.next()) {
- result += "\n " + rs.getString("ContactName");
- }
- jTextArea1.setText(result);
- con.close();
- }
- // Handle any errors that may have occurred.
- catch (Exception e) {
- e.printStackTrace();
- jOptionPane1.showMessageDialog (null, e.getMessage(), "Error", jOptionPane1.INFORMATION_MESSAGE);
- }
- finally {
- if (rs != null) try { rs.close(); } catch(Exception e) {}
- if (stmt != null) try { stmt.close(); } catch(Exception e) {}
- if (con != null) try { con.close(); } catch(Exception e) {}
- }
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String args[]) {
- /* Set the Nimbus look and feel */
- //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
- /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
- * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
- */
- try {
- for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
- if ("Nimbus".equals(info.getName())) {
- javax.swing.UIManager.setLookAndFeel(info.getClassName());
- break;
- }
- }
- } catch (ClassNotFoundException ex) {
- java.util.logging.Logger.getLogger(DatabaseViewer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(DatabaseViewer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(DatabaseViewer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(DatabaseViewer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- }
- //</editor-fold>
- /* Create and display the form */
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new DatabaseViewer().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JButton jButton1;
- private javax.swing.JButton jButton2;
- private javax.swing.JLabel jLabel1;
- private javax.swing.JOptionPane jOptionPane1;
- private javax.swing.JScrollPane jScrollPane1;
- private javax.swing.JTextArea jTextArea1;
- // End of variables declaration
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement