Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 KB | None | 0 0
  1. package put.pp.jse.pg;
  2. import java.awt.BorderLayout;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.awt.event.MouseEvent;
  6. import java.awt.event.MouseListener;
  7. import java.awt.event.MouseMotionListener;
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.ResultSet;
  11. import java.sql.SQLException;
  12. import java.sql.Statement;
  13.  
  14. import javax.swing.JButton;
  15. import javax.swing.JFrame;
  16. import javax.swing.JLabel;
  17. import javax.swing.JPanel;
  18. import javax.swing.JTextField;
  19.  
  20. public class DatabaseViewer extends JFrame{
  21. static int x = 800, y=600;
  22. static JFrame frame;
  23. static JButton button1, button2;
  24. static JLabel j;
  25. static JTextField text;
  26. static MouseMotionListener MML = new MouseMotionListener(){
  27.  
  28. @Override
  29. public void mouseDragged(MouseEvent e) {
  30. // TODO Auto-generated method stub
  31.  
  32. }
  33.  
  34. @Override
  35. public void mouseMoved(MouseEvent e) {
  36. // TODO Auto-generated method stub
  37. String tmp = "Pozycja myszki: x = "+e.getX()+" y = "+e.getY();
  38. j.setText(tmp);
  39. }
  40.  
  41.  
  42. };
  43.  
  44.  
  45.  
  46.  
  47. public static void main(String[] args) {
  48. java.awt.EventQueue.invokeLater(new Runnable() {
  49. @Override
  50. public void run() {
  51. new DatabaseViewer().initComponents();
  52. }
  53. }) ;
  54. }
  55.  
  56. public void initComponents(){
  57. frame = new JFrame("SuperBaza");
  58. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  59. //frame.getContentPane().add(, BorderLayout.CENTER);
  60. frame.setSize(x, y);
  61.  
  62. frame.setLayout(new BorderLayout());
  63.  
  64. button1 = new JButton("Wyczysc");
  65. button1.setBounds(0, 50, 100, y-50);
  66. button1.addMouseMotionListener(MML);
  67. button1.addActionListener(new ActionListener(){
  68.  
  69. @Override
  70. public void actionPerformed(ActionEvent e) {
  71. text.setText("");
  72.  
  73. }
  74.  
  75. });
  76. button2 = new JButton("Wykonaj");
  77. button2.setBounds(x-100, 50, 100, y-50);
  78. button2.addMouseMotionListener(MML);
  79.  
  80. button2.addActionListener(new ActionListener(){
  81.  
  82. @Override
  83. public void actionPerformed(ActionEvent e) {
  84.  
  85.  
  86. final String DB_URL = "jdbc:sqlserver://localhost:1433;" +
  87. "databaseName=NORTHWND;user=nag;password=admin1234";
  88.  
  89. Connection conn = null;
  90. Statement stmt = null;
  91. try{
  92.  
  93. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  94.  
  95.  
  96. System.out.println("Connecting to database...");
  97. conn = DriverManager.getConnection(DB_URL);
  98.  
  99.  
  100. System.out.println("Creating statement...");
  101. stmt = conn.createStatement();
  102. String sql ="USE NORTHWND;";
  103. sql = text.getText();
  104. ResultSet rs = stmt.executeQuery(sql);
  105.  
  106. String all="";
  107. while(rs.next()){
  108. //Retrieve by column name
  109. all += rs.getString("ContactName");
  110. all+='\n';
  111. //int age = rs.getInt("age");
  112. //String first = rs.getString("first");
  113. //String last = rs.getString("last");
  114.  
  115. //Display values
  116. //System.out.print(", Age: " + age);
  117. //System.out.print(", First: " + first);
  118. //System.out.println(", Last: " + last);
  119. }
  120. text.setText(all);
  121. //STEP 6: Clean-up environment
  122. rs.close();
  123. stmt.close();
  124. conn.close();
  125. }catch(SQLException se){
  126. //Handle errors for JDBC
  127. se.printStackTrace();
  128. }catch(Exception e1){
  129. //Handle errors for Class.forName
  130. e1.printStackTrace();
  131. }finally{
  132. //finally block used to close resources
  133. try{
  134. if(stmt!=null)
  135. stmt.close();
  136. }catch(SQLException se2){
  137. }// nothing we can do
  138. try{
  139. if(conn!=null)
  140. conn.close();
  141. }catch(SQLException se){
  142. se.printStackTrace();
  143. }//end finally try
  144. }//end try
  145. System.out.println("Goodbye!");
  146. }});
  147.  
  148.  
  149. text = new JTextField();
  150. text.setBounds(100, 50, x-200, y-50);
  151. text.addMouseMotionListener(MML);
  152.  
  153. j=new JLabel("");
  154. j.setBounds(10,5,1000,20);
  155. j.addMouseMotionListener(MML);
  156. frame.addMouseMotionListener(MML);
  157.  
  158.  
  159.  
  160.  
  161.  
  162. frame.add(j, BorderLayout.NORTH);
  163. frame.add(button1, BorderLayout.EAST);
  164. frame.add(button2, BorderLayout.WEST);
  165. frame.add(text);
  166. frame.setVisible(true);
  167.  
  168. }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement