Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.76 KB | None | 0 0
  1. package put.pp.jse.bk;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.FlowLayout;
  5. import java.awt.Frame;
  6. import java.awt.Label;
  7. import java.awt.MouseInfo;
  8. import java.awt.Point;
  9. import java.awt.PointerInfo;
  10. import java.awt.event.ActionEvent;
  11. import java.awt.event.ActionListener;
  12. import java.awt.event.MouseAdapter;
  13. import java.awt.event.MouseEvent;
  14. import java.awt.event.MouseListener;
  15. import java.awt.event.MouseMotionListener;
  16. import java.awt.event.WindowAdapter;
  17. import java.awt.event.WindowEvent;
  18. import java.awt.image.ComponentColorModel;
  19. import java.sql.Connection;
  20. import java.sql.DriverManager;
  21. import java.sql.PreparedStatement;
  22. import java.sql.ResultSet;
  23. import java.sql.SQLException;
  24.  
  25. import javax.swing.JButton;
  26. import javax.swing.JFrame;
  27. import javax.swing.JLabel;
  28. import javax.swing.JPanel;
  29. import javax.swing.JTextField;
  30. import javax.swing.border.Border;
  31.  
  32. public class DatabaseViewer extends Frame implements MouseMotionListener
  33. {
  34.  
  35. public JFrame f;
  36. public JLabel j;
  37. public JButton b;
  38. public JButton b1;
  39. public JTextField t1;
  40. public JTextField t2;
  41.  
  42. public int pX;
  43. public int pY;
  44.  
  45. public void initComponents()
  46. {
  47.  
  48. f = new JFrame("DatabaseViewer");
  49.  
  50. f.setLayout(new BorderLayout());
  51.  
  52. f.setSize(800, 600);
  53.  
  54. b = new JButton("Wyczysc");
  55. b.setBounds(0, 50, 100, 400);
  56. b1 = new JButton("Wykonaj");
  57. b1.setBounds(690, 50, 100, 400);
  58.  
  59. t1 = new JTextField("wprowadz sql statement");
  60. t1.setBounds(100, 50, 590, 400);
  61.  
  62.  
  63. t2 = new JTextField("lol");
  64. t2.setBounds(100,400, 590, 600);
  65.  
  66.  
  67.  
  68. j=new JLabel("cos");
  69. j.setBounds(20,20,100,20);
  70.  
  71. f.add(b1,BorderLayout.PAGE_START);
  72. f.add(b,BorderLayout.WEST);
  73. f.add(t1,BorderLayout.CENTER);
  74.  
  75. //f.add(t2,BorderLayout.SOUTH);
  76.  
  77. f.setLayout(null);
  78. f.setVisible(true);
  79.  
  80. f.add(j);
  81.  
  82.  
  83.  
  84. f.addMouseMotionListener(new MouseMotionListener()
  85. {
  86.  
  87. @Override
  88. public void mouseDragged(MouseEvent e) {
  89. // TODO Auto-generated method stub
  90.  
  91. }
  92.  
  93. @Override
  94. public void mouseMoved(MouseEvent e) {
  95. j.setText(e.getX() + " " + e.getY());
  96.  
  97. }
  98. });
  99. t1.addMouseMotionListener(new MouseMotionListener()
  100. {
  101.  
  102. @Override
  103. public void mouseDragged(MouseEvent e) {
  104. // TODO Auto-generated method stub
  105.  
  106. }
  107.  
  108. @Override
  109. public void mouseMoved(MouseEvent e) {
  110. j.setText("x: " + e.getX() + " y: " + e.getY());
  111.  
  112. }
  113. });
  114.  
  115. b.addMouseMotionListener(new MouseMotionListener()
  116. {
  117.  
  118. @Override
  119. public void mouseDragged(MouseEvent e) {
  120. // TODO Auto-generated method stub
  121.  
  122. }
  123.  
  124. @Override
  125. public void mouseMoved(MouseEvent e) {
  126. j.setText(e.getX() + " " + e.getY());
  127.  
  128. }
  129. });
  130. b1.addMouseMotionListener(new MouseMotionListener()
  131. {
  132.  
  133. @Override
  134. public void mouseDragged(MouseEvent e) {
  135. // TODO Auto-generated method stub
  136.  
  137. }
  138.  
  139. @Override
  140. public void mouseMoved(MouseEvent e) {
  141. j.setText(e.getX() + " " + e.getY());
  142.  
  143. }
  144. });
  145.  
  146.  
  147.  
  148. b.addActionListener(new ActionListener() {
  149.  
  150. @Override
  151. public void actionPerformed(ActionEvent e) {
  152. t1.setText("");
  153. //your actions
  154. }
  155. });
  156.  
  157. t1.addActionListener(new ActionListener() {
  158.  
  159. @Override
  160. public void actionPerformed(ActionEvent e) {
  161.  
  162. try {
  163. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  164. } catch (ClassNotFoundException e1) {
  165. // TODO Auto-generated catch block
  166. e1.printStackTrace();
  167. }
  168.  
  169. String connectionUrl = "jdbc:sqlserver://localhost:1433;"
  170. + "databaseName=NORTHWND;user=Bartek;password=q1w2e3r4;";
  171. try
  172. {
  173. Connection con = DriverManager.getConnection(connectionUrl);
  174. //PreparedStatement statement = con.prepareStatement("select * from Orders");
  175. //String nowy = t1.getText();
  176. PreparedStatement statement=con.prepareStatement("select * from Orders");
  177.  
  178.  
  179. //statement.setString(1, "Ms.");
  180.  
  181. ResultSet myRes = statement.executeQuery();
  182. String pom = "";
  183. while (myRes.next())
  184. {
  185. System.out.println(myRes.getString("FirstName"));
  186. pom+=myRes.getString("FirstName");
  187. pom+="\n";
  188. }
  189. t1.setText(pom);
  190. myRes.close();
  191. con.close();
  192.  
  193. }
  194. catch (SQLException z)
  195. {
  196. // TODO Auto-generated catch block
  197. z.printStackTrace();
  198. }
  199. }
  200. });
  201. //bazadanych
  202.  
  203. b1.addActionListener(new ActionListener() {
  204.  
  205. @Override
  206. public void actionPerformed(ActionEvent e) {
  207.  
  208. try {
  209. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  210. } catch (ClassNotFoundException e1) {
  211. // TODO Auto-generated catch block
  212. e1.printStackTrace();
  213. }
  214.  
  215. String connectionUrl = "jdbc:sqlserver://localhost:1433;"
  216. + "databaseName=NORTHWND;user=Bartek;password=q1w2e3r4;";
  217. try
  218. {
  219. Connection con = DriverManager.getConnection(connectionUrl);
  220. //PreparedStatement statement = con.prepareStatement("select * from Orders");
  221. String nowy = t1.getText();
  222. PreparedStatement statement=con.prepareStatement(nowy);
  223.  
  224.  
  225. //statement.setString(1, "Ms.");
  226.  
  227. ResultSet myRes = statement.executeQuery();
  228. String pom = "";
  229. while (myRes.next())
  230. {
  231. System.out.println(myRes.getString("FirstName"));
  232. pom+=myRes.getString("FirstName");
  233. pom+="\n";
  234. }
  235. t1.setText(pom);
  236. myRes.close();
  237. con.close();
  238.  
  239. }
  240. catch (SQLException z)
  241. {
  242. // TODO Auto-generated catch block
  243. z.printStackTrace();
  244. }
  245.  
  246.  
  247. //your actions
  248. }
  249. });
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256. }
  257.  
  258.  
  259.  
  260. public static void main(String[] args)
  261. {
  262. DatabaseViewer b = new DatabaseViewer();
  263. b.initComponents();
  264.  
  265. // java.awt.EventQueue.invokeLater(new Runnable()
  266. // {
  267. // @Override
  268. // public void run()
  269. // {
  270. // b.initComponents();
  271. // new DatabaseViewer().setVisible(true);
  272. // }
  273. // });
  274.  
  275. }
  276.  
  277.  
  278.  
  279. @Override
  280. public void mouseDragged(MouseEvent e) {
  281. // TODO Auto-generated method stub
  282.  
  283. }
  284.  
  285.  
  286.  
  287. @Override
  288. public void mouseMoved(MouseEvent e) {
  289. System.out.println("X : " + e.getX());
  290. System.out.println("Y : " + e.getY());
  291.  
  292. }
  293.  
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement