Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.intech.gui;
- import java.awt.BorderLayout;
- import java.awt.EventQueue;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- import javax.swing.border.EmptyBorder;
- import com.intech.engine.dbprocs;
- import com.intech.gui.threadtest.mythread;
- import java.awt.Panel;
- import java.awt.Color;
- import javax.swing.JButton;
- import javax.swing.JLabel;
- import java.awt.event.ActionListener;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- import java.awt.event.ActionEvent;
- public class dbthread extends JFrame {
- private JPanel contentPane;
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- dbthread frame = new dbthread();
- frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- /**
- * Create the frame.
- */
- public dbthread() {
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(100, 100, 450, 300);
- contentPane = new JPanel();
- contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
- setContentPane(contentPane);
- contentPane.setLayout(null);
- Panel panel = new Panel();
- panel.setBackground(Color.LIGHT_GRAY);
- panel.setBounds(10, 29, 114, 171);
- contentPane.add(panel);
- JLabel msg1 = new JLabel("");
- panel.add(msg1);
- JButton btnthread1 = new JButton("Insert");
- btnthread1.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- mythread thread1=new mythread(msg1,"table1");
- thread1.start();
- }
- });
- panel.add(btnthread1);
- Panel panel_1 = new Panel();
- panel_1.setBackground(Color.LIGHT_GRAY);
- panel_1.setBounds(160, 29, 114, 171);
- contentPane.add(panel_1);
- JLabel msg2 = new JLabel("");
- panel_1.add(msg2);
- JButton btnthread2 = new JButton("Insert");
- btnthread2.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- mythread thread2=new mythread(msg2,"table2");
- thread2.start();
- }
- });
- panel_1.add(btnthread2);
- Panel panel_2 = new Panel();
- panel_2.setBackground(Color.LIGHT_GRAY);
- panel_2.setBounds(310, 29, 114, 171);
- contentPane.add(panel_2);
- JLabel msg3 = new JLabel("");
- panel_2.add(msg3);
- JButton btnthread3 = new JButton("Insert");
- btnthread3.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- mythread thread3=new mythread(msg3,"table3");
- thread3.start();
- }
- });
- panel_2.add(btnthread3);
- JButton btnNewButton = new JButton("New button");
- btnNewButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- }
- });
- btnNewButton.setBounds(28, 227, 89, 23);
- contentPane.add(btnNewButton);
- }
- class mythread extends Thread implements dbprocs
- {
- private String classname;
- private String constring;
- protected Connection con;
- String sql;
- PreparedStatement st;
- private JLabel t;
- String tbname;
- mythread(JLabel t,String tbname)
- {
- this.t=t;
- this.tbname=tbname;
- classname="com.mysql.jdbc.Driver";
- constring="jdbc:mysql://192.168.1.151/intech?user=root&password=password";
- sql=null;
- st=null;
- }
- public void run()
- {
- t.setText("starting..");
- this.init();
- if(this.opencon())
- {
- try
- {
- for(int i = 0; i <500000; i++)
- {
- this.insert(tbname, Integer.toString(i));
- Thread.sleep(50);
- }
- }
- catch (InterruptedException e)
- {
- System.out.println("error");
- }
- }
- t.setText("done..");
- }
- @Override
- public void init() {
- try {
- Class.forName(classname);
- } catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- @Override
- public boolean opencon() {
- try {
- con=DriverManager.getConnection(constring);
- //System.out.println("Connection to mysql has been established");
- return true;
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- return false;
- }
- }
- @Override
- public void closecon() {
- try {
- con.close();
- //System.out.println("Connection to mysql has been closed");
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public void insert(String tbname,String value)
- {
- try
- {
- sql="insert into "+tbname+"(value)";
- sql=sql+" values(?)";
- st=con.prepareStatement(sql);
- st.setString(1,value);
- st.executeUpdate();
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement