Advertisement
Guest User

Untitled

a guest
Oct 21st, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.25 KB | None | 0 0
  1. public class DBC extends JFrame{
  2.  
  3. static String tablo;
  4. static JTextField tf = new JTextField(20);
  5. static int columnCount;
  6. static JPanel tfPanel = new JPanel();
  7. static String[] sutunlar;
  8. static JLabel sutunLabel;
  9. static JPanel sutunPanel = new JPanel(new BorderLayout());
  10. static JTable table;
  11. static JScrollPane scrollPane;
  12. static DefaultTableModel tableModel;
  13. static Object[] columnNames;
  14.  
  15. public static void main(String[] args) throws Exception {
  16.  
  17. Class.forName("com.mysql.jdbc.Driver");
  18. Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/project"
  19. ,"root","123456789");
  20.  
  21. final Statement statement = connect.createStatement();
  22.  
  23. JLabel tabloSec = new JLabel("Tablo Seçin:");
  24. final JComboBox<String> tablolar = new JComboBox<String>();
  25. final DatabaseMetaData md = connect.getMetaData();
  26. final ResultSet rs = md.getTables(null, null, "%", null);
  27.  
  28. while (rs.next()) {
  29. tablolar.addItem(rs.getString(3));
  30. }
  31.  
  32. tablolar.addActionListener(new ActionListener(){
  33.  
  34. @Override
  35. public void actionPerformed(ActionEvent arg0) {
  36.  
  37. tablo = tablolar.getSelectedItem().toString();
  38.  
  39. try {
  40.  
  41. ResultSet rs2 = statement.executeQuery("SELECT * FROM "+tablo);
  42. ResultSetMetaData rsmd = rs2.getMetaData();
  43. columnCount = rsmd.getColumnCount();
  44. List<String> columnNames = new ArrayList<String>();
  45. sutunlar = new String[columnCount];
  46.  
  47. Object rowData[][] = {{""}};
  48.  
  49.  
  50. for(int i=0;i<columnCount;i++){
  51.  
  52. sutunlar[i] = rsmd.getColumnLabel(i+1);
  53. columnNames.add(sutunlar[i]);
  54. tableModel.addColumn(columnNames.toArray());
  55. tableModel.addRow(rowData);
  56.  
  57. }
  58.  
  59. tableModel = new DefaultTableModel(rowData, columnNames.toArray());
  60. table = new JTable();
  61. table.setModel(tableModel);
  62. table.repaint();
  63. scrollPane = new JScrollPane(table);
  64. sutunPanel.add(scrollPane);
  65. sutunPanel.revalidate();
  66. sutunPanel.repaint();
  67.  
  68.  
  69. } catch (SQLException e) {
  70. // TODO Auto-generated catch block
  71. e.printStackTrace();
  72. }
  73. }
  74. });
  75.  
  76. JButton ekle = new JButton("Ekle");
  77. ekle.addActionListener(new ActionListener(){
  78.  
  79. @Override
  80. public void actionPerformed(ActionEvent e) {
  81.  
  82. try {
  83. switch(tablo){
  84. case "department":
  85.  
  86. statement.executeUpdate("INSERT INTO department(Name,Location) VALUES('"+tf.getText()+"')");
  87. case "employee":
  88.  
  89. statement.executeUpdate("INSERT INTO employee(Id,FirstName,LastName,Sex,Address,Email,Salary,BirthDate,JoinDate) VALUES('"+tf.getText()+"')");
  90. case "engineer":
  91.  
  92. statement.executeUpdate("INSERT INTO engineer(EngineerType) VALUES('"+tf.getText()+"')");
  93. case "manager":
  94.  
  95. statement.executeUpdate("INSERT INTO manager(Department) VALUES('"+tf.getText()+"')");
  96. case "project":
  97.  
  98. statement.executeUpdate("INSERT INTO project(Name,Number,Value) VALUES('"+tf.getText()+"')");
  99. case "secretary":
  100.  
  101. statement.executeUpdate("INSERT INTO secretary(TypingSpeed) VALUES('"+tf.getText()+"')");
  102. }
  103.  
  104. } catch (SQLException e1) {
  105. // TODO Auto-generated catch block
  106. e1.printStackTrace();
  107. }
  108. }
  109. });
  110.  
  111. JButton cik = new JButton("Çık");
  112. cik.addActionListener(new ActionListener() {
  113.  
  114. @Override
  115. public void actionPerformed(ActionEvent e) {
  116.  
  117. System.exit(0);
  118. }
  119. });
  120.  
  121. JPanel panel = new JPanel(new FlowLayout());
  122.  
  123. panel.add(tabloSec);
  124. panel.add(tablolar);
  125. panel.add(sutunPanel);
  126. panel.revalidate();
  127. panel.add(ekle);
  128. panel.add(cik);
  129.  
  130. JFrame frame = new JFrame("Deneme");
  131. frame.setSize(600,600);
  132. frame.setLocationRelativeTo(null);
  133. frame.add(panel);
  134. frame.setVisible(true);
  135. }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement