Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.79 KB | None | 0 0
  1. package com.intech.gui;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.EventQueue;
  5.  
  6. import javax.swing.JFrame;
  7. import javax.swing.JPanel;
  8. import javax.swing.JTextField;
  9. import javax.swing.border.EmptyBorder;
  10.  
  11. import com.intech.engine.dbprocs;
  12. import com.intech.gui.threadtest.mythread;
  13.  
  14. import java.awt.Panel;
  15. import java.awt.Color;
  16. import javax.swing.JButton;
  17. import javax.swing.JLabel;
  18. import java.awt.event.ActionListener;
  19. import java.sql.Connection;
  20. import java.sql.DriverManager;
  21. import java.sql.PreparedStatement;
  22. import java.sql.SQLException;
  23. import java.awt.event.ActionEvent;
  24.  
  25. public class dbthread extends JFrame {
  26.  
  27. private JPanel contentPane;
  28.  
  29. /**
  30. * Launch the application.
  31. */
  32. public static void main(String[] args) {
  33. EventQueue.invokeLater(new Runnable() {
  34. public void run() {
  35. try {
  36. dbthread frame = new dbthread();
  37. frame.setVisible(true);
  38. } catch (Exception e) {
  39. e.printStackTrace();
  40. }
  41. }
  42. });
  43. }
  44.  
  45. /**
  46. * Create the frame.
  47. */
  48. public dbthread() {
  49. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  50. setBounds(100, 100, 450, 300);
  51. contentPane = new JPanel();
  52. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  53. setContentPane(contentPane);
  54. contentPane.setLayout(null);
  55.  
  56. Panel panel = new Panel();
  57. panel.setBackground(Color.LIGHT_GRAY);
  58. panel.setBounds(10, 29, 114, 171);
  59. contentPane.add(panel);
  60.  
  61. JLabel msg1 = new JLabel("");
  62. panel.add(msg1);
  63.  
  64. JButton btnthread1 = new JButton("Insert");
  65. btnthread1.addActionListener(new ActionListener() {
  66. public void actionPerformed(ActionEvent e) {
  67. mythread thread1=new mythread(msg1,"table1");
  68. thread1.start();
  69. }
  70. });
  71. panel.add(btnthread1);
  72.  
  73.  
  74.  
  75. Panel panel_1 = new Panel();
  76. panel_1.setBackground(Color.LIGHT_GRAY);
  77. panel_1.setBounds(160, 29, 114, 171);
  78. contentPane.add(panel_1);
  79.  
  80. JLabel msg2 = new JLabel("");
  81. panel_1.add(msg2);
  82.  
  83. JButton btnthread2 = new JButton("Insert");
  84. btnthread2.addActionListener(new ActionListener() {
  85. public void actionPerformed(ActionEvent e) {
  86. mythread thread2=new mythread(msg2,"table2");
  87. thread2.start();
  88. }
  89. });
  90. panel_1.add(btnthread2);
  91.  
  92.  
  93.  
  94. Panel panel_2 = new Panel();
  95. panel_2.setBackground(Color.LIGHT_GRAY);
  96. panel_2.setBounds(310, 29, 114, 171);
  97. contentPane.add(panel_2);
  98.  
  99. JLabel msg3 = new JLabel("");
  100. panel_2.add(msg3);
  101.  
  102. JButton btnthread3 = new JButton("Insert");
  103. btnthread3.addActionListener(new ActionListener() {
  104. public void actionPerformed(ActionEvent e) {
  105. mythread thread3=new mythread(msg3,"table3");
  106. thread3.start();
  107. }
  108. });
  109. panel_2.add(btnthread3);
  110.  
  111. JButton btnNewButton = new JButton("New button");
  112. btnNewButton.addActionListener(new ActionListener() {
  113. public void actionPerformed(ActionEvent e) {
  114. }
  115. });
  116. btnNewButton.setBounds(28, 227, 89, 23);
  117. contentPane.add(btnNewButton);
  118.  
  119.  
  120. }
  121. class mythread extends Thread implements dbprocs
  122. {
  123. private String classname;
  124. private String constring;
  125. protected Connection con;
  126. String sql;
  127. PreparedStatement st;
  128. private JLabel t;
  129. String tbname;
  130. mythread(JLabel t,String tbname)
  131. {
  132. this.t=t;
  133. this.tbname=tbname;
  134. classname="com.mysql.jdbc.Driver";
  135. constring="jdbc:mysql://192.168.1.151/intech?user=root&password=password";
  136. sql=null;
  137. st=null;
  138. }
  139. public void run()
  140. {
  141. t.setText("starting..");
  142. this.init();
  143. if(this.opencon())
  144. {
  145. try
  146. {
  147. for(int i = 0; i <500000; i++)
  148. {
  149. this.insert(tbname, Integer.toString(i));
  150. Thread.sleep(50);
  151. }
  152. }
  153. catch (InterruptedException e)
  154. {
  155. System.out.println("error");
  156. }
  157. }
  158. t.setText("done..");
  159.  
  160. }
  161. @Override
  162. public void init() {
  163. try {
  164. Class.forName(classname);
  165. } catch (ClassNotFoundException e) {
  166. // TODO Auto-generated catch block
  167. e.printStackTrace();
  168. }
  169.  
  170. }
  171. @Override
  172. public boolean opencon() {
  173. try {
  174. con=DriverManager.getConnection(constring);
  175. //System.out.println("Connection to mysql has been established");
  176. return true;
  177. } catch (SQLException e) {
  178. // TODO Auto-generated catch block
  179. e.printStackTrace();
  180. return false;
  181. }
  182. }
  183. @Override
  184. public void closecon() {
  185. try {
  186. con.close();
  187. //System.out.println("Connection to mysql has been closed");
  188. } catch (SQLException e) {
  189. // TODO Auto-generated catch block
  190. e.printStackTrace();
  191. }
  192.  
  193. }
  194. public void insert(String tbname,String value)
  195. {
  196. try
  197. {
  198. sql="insert into "+tbname+"(value)";
  199. sql=sql+" values(?)";
  200. st=con.prepareStatement(sql);
  201. st.setString(1,value);
  202. st.executeUpdate();
  203. }
  204. catch(Exception e)
  205. {
  206. e.printStackTrace();
  207. }
  208. }
  209. }
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement