Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.42 KB | None | 0 0
  1.  
  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.border.EmptyBorder;
  9. import javax.swing.text.JTextComponent;
  10. import javax.swing.JComboBox;
  11. import javax.swing.JTextField;
  12. import javax.swing.JLabel;
  13. import java.awt.Font;
  14. import java.awt.Image;
  15. import java.awt.List;
  16. import java.awt.TextArea;
  17.  
  18. import javax.swing.JCheckBoxMenuItem;
  19. import javax.swing.JSplitPane;
  20. import javax.swing.JSeparator;
  21. import javax.swing.SwingConstants;
  22. import java.awt.Color;
  23. import javax.swing.JButton;
  24. import javax.swing.JTextPane;
  25. import javax.swing.JEditorPane;
  26. import javax.swing.JList;
  27. import javax.swing.JOptionPane;
  28. import javax.swing.JTextArea;
  29. import javax.swing.DefaultComboBoxModel;
  30. import javax.swing.ImageIcon;
  31.  
  32. import java.awt.event.ActionListener;
  33. import java.awt.geom.Area;
  34. import java.io.File;
  35. import java.io.FileInputStream;
  36. import java.io.FileNotFoundException;
  37. import java.sql.Connection;
  38. import java.sql.DriverManager;
  39. import java.sql.PreparedStatement;
  40. import java.sql.ResultSet;
  41. import java.sql.SQLException;
  42. import java.sql.Statement;
  43. import java.awt.event.ActionEvent;
  44.  
  45.  
  46. public class Dashboard extends JFrame {
  47.  
  48. private JPanel contentPane;
  49. private final JSeparator separator = new JSeparator();
  50. private JTextField titre_article;
  51. JTextArea test;
  52. private JTextField jtf;
  53.  
  54. /**
  55. * Launch the application.
  56. */
  57. public static void main(String[] args) {
  58. EventQueue.invokeLater(new Runnable() {
  59. public void run() {
  60. try {
  61. Dashboard frame = new Dashboard();
  62. frame.setVisible(true);
  63. } catch (Exception e) {
  64. e.printStackTrace();
  65. }
  66. }
  67. });
  68. }
  69.  
  70. /**
  71. * Create the frame.
  72. */
  73. public Dashboard() {
  74.  
  75. setForeground(Color.RED);
  76. setBackground(Color.BLUE);
  77. setTitle("SupNews");
  78. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  79. setBounds(100, 100, 531, 447);
  80. contentPane = new JPanel();
  81. contentPane.setForeground(Color.RED);
  82. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  83. setContentPane(contentPane);
  84. contentPane.setLayout(null);
  85.  
  86. JLabel lblNewArticle = new JLabel("New article");
  87. lblNewArticle.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 18));
  88. lblNewArticle.setBounds(181, 12, 109, 31);
  89. contentPane.add(lblNewArticle);
  90.  
  91. JLabel lblYour = new JLabel("Your articles");
  92. lblYour.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 18));
  93. lblYour.setBounds(26, 12, 122, 31);
  94. contentPane.add(lblYour);
  95. separator.setForeground(Color.BLACK);
  96. separator.setOrientation(SwingConstants.VERTICAL);
  97. separator.setBounds(161, 0, 27, 387);
  98. contentPane.add(separator);
  99.  
  100. JLabel lblTitle = new JLabel("Title");
  101. lblTitle.setFont(new Font("Times New Roman", Font.PLAIN, 17));
  102. lblTitle.setBounds(188, 57, 63, 19);
  103. contentPane.add(lblTitle);
  104.  
  105. titre_article = new JTextField();
  106. titre_article.setBounds(237, 54, 183, 22);
  107. contentPane.add(titre_article);
  108. titre_article.setColumns(10);
  109.  
  110. JComboBox mylist = new JComboBox();
  111. mylist.setBounds(12, 50, 136, 43);
  112. contentPane.add(mylist);
  113.  
  114. jtf = new JTextField();
  115. jtf.setBounds(198, 355, 72, 19);
  116. contentPane.add(jtf);
  117. jtf.setColumns(10);
  118.  
  119. JLabel label = new JLabel("Your image");
  120. label.setBounds(0, 136, 148, 111);
  121. contentPane.add(label);
  122.  
  123.  
  124. //*******************************************************************************//
  125. try {
  126. Class.forName("com.mysql.jdbc.Driver");
  127. Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/supnews?useSSL=true","root","");
  128. Statement st =con.createStatement();
  129. String sql = "select Title from article";
  130.  
  131. ResultSet current_data = st.executeQuery(sql);
  132.  
  133. while(current_data.next())
  134. {
  135. mylist.addItem(current_data.getString("Title"));
  136. }
  137. }catch(Exception e) {System.out.print(e);}
  138.  
  139.  
  140. //*********************************************************************************//
  141.  
  142.  
  143. JButton btnSave = new JButton("Save");
  144. btnSave.addActionListener(new ActionListener() {
  145. public void actionPerformed(ActionEvent arg0) {
  146.  
  147. //*******************************************************************************//
  148.  
  149. try {
  150. // init et insert
  151. String titre = titre_article.getText();
  152. String champ = test.getText();
  153.  
  154. Class.forName("com.mysql.jdbc.Driver");
  155. Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/supnews?useSSL=true","root","");
  156. Statement st =con.createStatement();
  157. st.executeUpdate("INSERT INTO article (Title, Content) VALUES (\'"+titre+"\', \'"+champ+"\')");
  158.  
  159. Dashboard.this.dispose();
  160. Dashboard Dashboard= new Dashboard();
  161. Dashboard.setVisible(true);
  162.  
  163. }catch(Exception e) {System.out.print(e);}
  164. }
  165.  
  166.  
  167. //*******************************************************************************//
  168.  
  169. });
  170. btnSave.setBounds(438, 329, 63, 25);
  171. contentPane.add(btnSave);
  172.  
  173. JButton btnNewButton = new JButton("Preview");
  174. btnNewButton.setBounds(330, 329, 90, 25);
  175. contentPane.add(btnNewButton);
  176.  
  177. //*******************************************************************************//
  178.  
  179. JButton btnNewButton_1 = new JButton("Add image...");
  180.  
  181. btnNewButton_1.addActionListener(new ActionListener() {
  182. public void actionPerformed(ActionEvent arg0) {
  183.  
  184. String location ="";
  185. String name ="";
  186. File monImage = new File(location);
  187.  
  188. try {
  189. Class.forName("com.mysql.jdbc.Driver");
  190. Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/supnews?useSSL=true","root","");
  191. Statement st = con.createStatement();
  192. ResultSet rs = st.executeQuery("SELECT * FROM Image WHERE ID = '"+ jtf.getText()+ "'");
  193. if(rs.next()) {
  194. byte [] img = rs.getBytes("Pic");
  195. ImageIcon image = new ImageIcon(img);
  196. Image im = image.getImage();
  197. Image myImg = im.getScaledInstance(label.getWidth(), label.getHeight() , Image.SCALE_SMOOTH);
  198. ImageIcon newImage = new ImageIcon(myImg);
  199. label.setIcon(newImage);
  200. }
  201.  
  202. else {
  203. JOptionPane.showMessageDialog(null, "non data");
  204. }
  205. FileInputStream istreamImage = new FileInputStream(monImage);
  206.  
  207.  
  208.  
  209. } catch (Exception error) {
  210. error.printStackTrace();
  211. System.out.println(error);
  212. }
  213.  
  214.  
  215. }});
  216. btnNewButton_1.setBounds(175, 329, 115, 25);
  217. contentPane.add(btnNewButton_1);
  218.  
  219. //*******************************************************************************//
  220.  
  221. JSeparator separator_1 = new JSeparator();
  222. separator_1.setForeground(Color.BLACK);
  223. separator_1.setBounds(-11, 385, 536, 15);
  224. contentPane.add(separator_1);
  225.  
  226.  
  227. JTextArea area_text = new JTextArea();
  228. area_text.setBounds(175, 89, 326, 225);
  229. test = area_text;
  230. contentPane.add(area_text);
  231.  
  232. //*******************************************************************************//
  233.  
  234. JButton del_article = new JButton("delete");
  235. del_article.addActionListener(new ActionListener() {
  236. public void actionPerformed(ActionEvent arg0) {
  237.  
  238. /*ResultSet result;
  239. String res = null;
  240.  
  241. try {
  242. Class.forName("com.mysql.jdbc.Driver");
  243. Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/supnews?useSSL=true","root","");
  244. Statement st =con.createStatement();
  245.  
  246. result = st.executeQuery("select Title from article = "+mylist.getSelectedItem().toString());
  247.  
  248. while(result.next()){
  249.  
  250. res = result.getString("Title");
  251.  
  252. }
  253.  
  254. st.executeUpdate("DELETE FROM article where Title = " +"'"+res+"'");
  255.  
  256.  
  257. Dashboard.this.dispose();
  258. Dashboard Dashboard= new Dashboard();
  259. Dashboard.setVisible(true);
  260.  
  261.  
  262. }catch(Exception e){System.out.println(e);}*/
  263.  
  264.  
  265. PreparedStatement ps;
  266. String valeur = (String)mylist.getSelectedItem();
  267.  
  268. try {
  269. Class.forName("com.mysql.jdbc.Driver");
  270. Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/supnews?useSSL=true","root","");
  271. Statement st =con.createStatement();
  272.  
  273. ps = con.prepareStatement("DELETE FROM article where Title='"+valeur+"'");
  274.  
  275. ps.execute();
  276.  
  277. Dashboard.this.dispose();
  278. Dashboard Dashboard= new Dashboard();
  279. Dashboard.setVisible(true);
  280.  
  281.  
  282. }catch (Exception error) {
  283. error.printStackTrace();
  284. System.out.println(error);
  285. }
  286. //*******************************************************************************//
  287.  
  288. }
  289. });
  290. del_article.setBounds(52, 329, 97, 25);
  291. contentPane.add(del_article);
  292.  
  293. //*******************************************************************************//
  294.  
  295. JButton btnView = new JButton("view");
  296. btnView.addActionListener(new ActionListener() {
  297. public void actionPerformed(ActionEvent arg0) {
  298.  
  299. PreparedStatement ps=null;
  300. ResultSet rs=null;
  301. String valeur = (String)mylist.getSelectedItem();
  302. String cont = "Content";
  303. titre_article.setText(valeur);
  304.  
  305. rs = find(area_text.getText());
  306.  
  307.  
  308. /* try {
  309. Class.forName("com.mysql.jdbc.Driver");
  310. Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/supnews?useSSL=true","root","");
  311. Statement st =con.createStatement();
  312.  
  313. String sql = "Select Content from article where Title =" + "' "+valeur+" ' " ;
  314.  
  315. ps=con.prepareStatement(sql);
  316. rs = pst.executeQuery();
  317.  
  318. while(rs.next()) {
  319.  
  320. //titre_article.setText(rs.getString("Title"));
  321. area_text.setText(rs.getString("Content"));
  322. area_text.setText(rs.getObject(1).toString());
  323. System.out.println(rs.getObject("Content").toString());
  324.  
  325. }
  326.  
  327. }catch(Exception e){System.out.println(e);}*/
  328.  
  329. try {
  330. Connection con = null;
  331. con = DriverManager.getConnection("jdbc:mysql://localhost:3306/supnews?useSSL=true","root","");
  332. ps = con.prepareStatement("Select Content from article where Title =" + "'" + valeur + "'");
  333. rs = ps.executeQuery();
  334.  
  335. if(rs.next()) {
  336. area_text.setText(rs.getString("Content"));
  337. }
  338.  
  339.  
  340. }catch(Exception e){JOptionPane.showMessageDialog(null, e.getMessage());}
  341. return;
  342.  
  343. //*******************************************************************************//
  344. }
  345.  
  346. });
  347. btnView.setBounds(52, 272, 97, 25);
  348. contentPane.add(btnView);
  349.  
  350.  
  351. //*******************************************************************************//
  352. JButton btnUpdate = new JButton("Update");
  353. btnUpdate.addActionListener(new ActionListener() {
  354. public void actionPerformed(ActionEvent arg0) {
  355.  
  356. try {
  357. PreparedStatement pst=null;
  358. ResultSet rs=null;
  359. String valeur = (String)mylist.getSelectedItem();
  360. titre_article.setText(valeur);
  361.  
  362. String titre = titre_article.getText();
  363. String champ = test.getText();
  364. String ID = "1";
  365.  
  366.  
  367. Class.forName("com.mysql.jdbc.Driver");
  368. Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/supnews?useSSL=true","root","");
  369. Statement st =con.createStatement();
  370.  
  371. pst = con.prepareStatement("Select ID from article where Title =" + "'" + valeur + "'");
  372. rs = pst.executeQuery();
  373. String sql =("UPDATE article SET Title = " + "'"+ titre +"'" + ", Content =" + "'" + champ + "'" + "WHERE ID = 6");
  374.  
  375. pst=con.prepareStatement(sql);
  376.  
  377.  
  378. pst.executeUpdate();
  379. rs = pst.executeQuery();
  380.  
  381. while(rs.next()) {
  382.  
  383. titre_article.setText(rs.getString("Title"));
  384. area_text.setText(rs.getString("Content"));
  385.  
  386. }
  387.  
  388. Dashboard.this.dispose();
  389. Dashboard Dashboard= new Dashboard();
  390. Dashboard.setVisible(true);
  391. }catch(Exception e) {System.out.print(e);}
  392.  
  393. }});
  394. btnUpdate.setBounds(412, 354, 89, 23);
  395. contentPane.add(btnUpdate);
  396. //*******************************************************************************//
  397.  
  398.  
  399.  
  400.  
  401.  
  402. }
  403.  
  404. protected ResultSet find(String text) {
  405. // TODO Auto-generated method stub
  406. return null;
  407. }
  408. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement