Guest User

Untitled

a guest
Jul 25th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.22 KB | None | 0 0
  1.  
  2. import java.sql.*;
  3. import java.awt.*;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import javax.swing.*;
  7.  
  8. public class week4 extends JFrame implements ActionListener{
  9.  
  10. JButton btnIleri = new JButton(">");
  11. JButton btnGeri = new JButton("<");
  12. JButton btnBas = new JButton("<<");
  13. JButton btnSon = new JButton(">>");
  14. JButton btnSil = new JButton("SIL");
  15. JButton btnUpdate = new JButton("DUZENLE");
  16. JButton btnEkle = new JButton("EKLE");
  17.  
  18. static JTextField txtEMPLOYEEID = new JTextField();
  19. static JTextField txtFIRSTNAME = new JTextField();
  20. static JTextField txtTITLE = new JTextField();
  21. static JTextField txtLASTNAME = new JTextField();
  22. static JTextField txtHOMEPHONE = new JTextField();
  23. JLabel lblEMPLOYEEID = new JLabel("EMPLOYEEID:");
  24. JLabel lblFIRSTNAME = new JLabel("FIRSTNAME:");
  25. JLabel lblTITLE = new JLabel("TITLE:");
  26. JLabel lblLASTNAME = new JLabel("LASTNAME:");
  27. JLabel lblHOMEPHONE = new JLabel("HOMEPHONE:");
  28. JLabel lblbos = new JLabel();
  29.  
  30. static Connection connection = null;
  31. static Statement st = null;
  32. static ResultSet rs = null;
  33.  
  34. week4()
  35. {
  36. super("Employee Info");
  37. setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  38. setLayout(new GridLayout(9,2)); // satir, sutun
  39.  
  40. btnIleri.addActionListener(this);
  41. btnBas.addActionListener(this);
  42. btnSon.addActionListener(this);
  43. btnGeri.addActionListener(this);
  44. btnSil.addActionListener(this);
  45. btnUpdate.addActionListener(this);
  46. btnEkle.addActionListener(this);
  47.  
  48. add(btnBas);
  49. add(btnSon);
  50. add(btnGeri);
  51. add(btnIleri);
  52. add(btnSil);
  53. add(btnUpdate);
  54. add(btnEkle);
  55. add(lblbos);
  56.  
  57. add(lblEMPLOYEEID);
  58. txtEMPLOYEEID.setEditable(false);
  59. add(txtEMPLOYEEID);
  60. add(lblFIRSTNAME);
  61. add(txtFIRSTNAME);
  62. add(lblLASTNAME);
  63. add(txtLASTNAME);
  64. add(lblTITLE);
  65. add(txtTITLE);
  66. add(lblHOMEPHONE);
  67. add(txtHOMEPHONE);
  68.  
  69. setSize(450, 200);
  70. setVisible(true);
  71. ConnectionStuff();
  72. }
  73.  
  74. static void ConnectionStuff(){
  75.  
  76. try {
  77.  
  78. Class.forName("oracle.jdbc.driver.OracleDriver");
  79.  
  80. String serverName = "//193.255.85.26";
  81. String portNumber = "1521";
  82. String dbName = "ORCL";
  83. String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber
  84. + "/" + dbName;
  85. String username = "ozge";
  86. String password = "ozge";
  87.  
  88. connection = DriverManager.getConnection(url, username, password);
  89. System.out.println("TADA!!");
  90. st=connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
  91. �lkBilgiler();
  92.  
  93. }
  94.  
  95. catch (Exception e) {
  96. e.printStackTrace();
  97. }
  98. }
  99.  
  100. static void �lkBilgiler(){
  101.  
  102. try {
  103. rs=st.executeQuery("select * from employees");
  104. rs.next();
  105. VerileriGoster();
  106. } catch (Exception e) {
  107. e.printStackTrace();
  108. }
  109. }
  110.  
  111. static void VerileriGoster(){
  112.  
  113. try {
  114. txtEMPLOYEEID.setText(rs.getString("EMPLOYEEID"));
  115. txtFIRSTNAME.setText(rs.getString("FIRSTNAME"));
  116. txtTITLE.setText(rs.getString("TITLE"));
  117. txtLASTNAME.setText(rs.getString("LASTNAME"));
  118. txtHOMEPHONE.setText(rs.getString("HOMEPHONE"));
  119.  
  120. } catch (Exception e) {
  121. e.printStackTrace();
  122. }
  123. }
  124.  
  125. public static void main(String[] args) {
  126. new week4();
  127. }
  128.  
  129. public void actionPerformed(ActionEvent e) {
  130. if (e.getActionCommand().equals(">")) {
  131. try {
  132. rs.next();
  133. VerileriGoster();
  134. } catch (Exception e2) {
  135. e2.printStackTrace();
  136. }
  137. }
  138.  
  139. else if (e.getActionCommand().equals("<")) {
  140. try
  141. {
  142. rs.previous();
  143. VerileriGoster();
  144. } catch (Exception e1) {
  145. e1.printStackTrace();
  146. }
  147. }
  148.  
  149. else if (e.getActionCommand().equals("<<")) {
  150. try
  151. {
  152. rs.first(); //en basa gider
  153. VerileriGoster();
  154. } catch (Exception e1) {
  155. // TODO Auto-generated catch block
  156. e1.printStackTrace();
  157. }
  158. }
  159.  
  160. else if (e.getActionCommand().equals(">>")) {
  161. try
  162. {
  163. rs.last();
  164. VerileriGoster();
  165. } catch (Exception e1) {
  166. // TODO Auto-generated catch block
  167. e1.printStackTrace();
  168. }
  169. }
  170. else if (e.getActionCommand().equals("SIL")) {
  171. String query = "DELETE FROM EMPLOYEES WHERE EMPLOYEEID = ?";
  172. try
  173. {
  174. PreparedStatement ps = connection.prepareStatement(query);
  175. ps.setInt(1, Integer.parseInt(txtEMPLOYEEID.getText()));
  176. ps.executeUpdate();
  177. ps.close();
  178. JOptionPane.showMessageDialog(null, "Silme i�lemi ba�arili.");
  179. VerileriGoster();
  180. }
  181. catch (SQLException e1) {
  182. // TODO Auto-generated catch block
  183. e1.printStackTrace();
  184. }
  185. }
  186.  
  187. else if (e.getActionCommand().equals("DUZENLE")) {
  188.  
  189. String query = "UPDATE EMPLOYEES SET FIRSTNAME=?, LASTNAME=?, TITLE=?, HOMEPHONE=? WHERE EMPLOYEEID=?";
  190. try
  191. {
  192. PreparedStatement ps = connection.prepareStatement(query);
  193. ps.setString(1, txtFIRSTNAME.getText());
  194. ps.setString(2, txtLASTNAME.getText());
  195. ps.setString(3, txtTITLE.getText());
  196. ps.setString(4, txtHOMEPHONE.getText());
  197. ps.setInt(5, Integer.parseInt(txtEMPLOYEEID.getText()));
  198. ps.executeUpdate();
  199. ps.close();
  200. JOptionPane.showMessageDialog(null, "Update ba�arili");
  201. VerileriGoster();
  202. }
  203. catch (SQLException e1) {
  204. // TODO Auto-generated catch block
  205. e1.printStackTrace();
  206. }
  207. }
  208.  
  209. else if (e.getActionCommand().equals("EKLE")) {
  210.  
  211. if(txtEMPLOYEEID.isEditable()) //ikinci tiklandiginda database'e insert eder
  212. {
  213. String query = "INSERT INTO EMPLOYEES (EMPLOYEEID, FIRSTNAME, LASTNAME, TITLE, HOMEPHONE) VALUES(?,?,?,?,?)";
  214.  
  215. try
  216. {
  217. PreparedStatement ps = connection.prepareStatement(query);
  218. ps.setInt(1, Integer.parseInt(txtEMPLOYEEID.getText()));
  219. ps.setString(2, txtFIRSTNAME.getText());
  220. ps.setString(3, txtLASTNAME.getText());
  221. ps.setString(4, txtTITLE.getText());
  222. ps.setString(5, txtHOMEPHONE.getText());
  223. ps.executeUpdate();
  224. ps.close();
  225. JOptionPane.showMessageDialog(null, "Insert i�lemi� ba�arili");
  226. }
  227. catch (SQLException e1) {
  228. // TODO Auto-generated catch block
  229. e1.printStackTrace();
  230. }
  231.  
  232. VerileriGoster();
  233. txtEMPLOYEEID.setEditable(false);
  234. }
  235. else //ilk kez basildi�inda, txtEMPLOYEEID'yi acar ve textfield'larin hepsini bosaltir.
  236. {
  237. txtEMPLOYEEID.setEditable(true);
  238. txtEMPLOYEEID.setText("");
  239. txtFIRSTNAME.setText("");
  240. txtLASTNAME.setText("");
  241. txtTITLE.setText("");
  242. txtHOMEPHONE.setText("");
  243. }
  244.  
  245. }
  246.  
  247. }
  248. }
Add Comment
Please, Sign In to add comment