Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.BorderLayout;
- import java.awt.EventQueue;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.border.EmptyBorder;
- import javax.swing.text.JTextComponent;
- import javax.swing.JComboBox;
- import javax.swing.JTextField;
- import javax.swing.JLabel;
- import java.awt.Font;
- import java.awt.Image;
- import java.awt.List;
- import java.awt.TextArea;
- import javax.swing.JCheckBoxMenuItem;
- import javax.swing.JSplitPane;
- import javax.swing.JSeparator;
- import javax.swing.SwingConstants;
- import java.awt.Color;
- import javax.swing.JButton;
- import javax.swing.JTextPane;
- import javax.swing.JEditorPane;
- import javax.swing.JList;
- import javax.swing.JOptionPane;
- import javax.swing.JTextArea;
- import javax.swing.DefaultComboBoxModel;
- import javax.swing.ImageIcon;
- import java.awt.event.ActionListener;
- import java.awt.geom.Area;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.awt.event.ActionEvent;
- public class Dashboard extends JFrame {
- private JPanel contentPane;
- private final JSeparator separator = new JSeparator();
- private JTextField titre_article;
- JTextArea test;
- private JTextField jtf;
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- Dashboard frame = new Dashboard();
- frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- /**
- * Create the frame.
- */
- public Dashboard() {
- setForeground(Color.RED);
- setBackground(Color.BLUE);
- setTitle("SupNews");
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(100, 100, 531, 447);
- contentPane = new JPanel();
- contentPane.setForeground(Color.RED);
- contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
- setContentPane(contentPane);
- contentPane.setLayout(null);
- JLabel lblNewArticle = new JLabel("New article");
- lblNewArticle.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 18));
- lblNewArticle.setBounds(181, 12, 109, 31);
- contentPane.add(lblNewArticle);
- JLabel lblYour = new JLabel("Your articles");
- lblYour.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 18));
- lblYour.setBounds(26, 12, 122, 31);
- contentPane.add(lblYour);
- separator.setForeground(Color.BLACK);
- separator.setOrientation(SwingConstants.VERTICAL);
- separator.setBounds(161, 0, 27, 387);
- contentPane.add(separator);
- JLabel lblTitle = new JLabel("Title");
- lblTitle.setFont(new Font("Times New Roman", Font.PLAIN, 17));
- lblTitle.setBounds(188, 57, 63, 19);
- contentPane.add(lblTitle);
- titre_article = new JTextField();
- titre_article.setBounds(237, 54, 183, 22);
- contentPane.add(titre_article);
- titre_article.setColumns(10);
- JComboBox mylist = new JComboBox();
- mylist.setBounds(12, 50, 136, 43);
- contentPane.add(mylist);
- jtf = new JTextField();
- jtf.setBounds(198, 355, 72, 19);
- contentPane.add(jtf);
- jtf.setColumns(10);
- JLabel label = new JLabel("Your image");
- label.setBounds(0, 136, 148, 111);
- contentPane.add(label);
- //*******************************************************************************//
- try {
- Class.forName("com.mysql.jdbc.Driver");
- Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/supnews?useSSL=true","root","");
- Statement st =con.createStatement();
- String sql = "select Title from article";
- ResultSet current_data = st.executeQuery(sql);
- while(current_data.next())
- {
- mylist.addItem(current_data.getString("Title"));
- }
- }catch(Exception e) {System.out.print(e);}
- //*********************************************************************************//
- JButton btnSave = new JButton("Save");
- btnSave.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- //*******************************************************************************//
- try {
- // init et insert
- String titre = titre_article.getText();
- String champ = test.getText();
- Class.forName("com.mysql.jdbc.Driver");
- Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/supnews?useSSL=true","root","");
- Statement st =con.createStatement();
- st.executeUpdate("INSERT INTO article (Title, Content) VALUES (\'"+titre+"\', \'"+champ+"\')");
- Dashboard.this.dispose();
- Dashboard Dashboard= new Dashboard();
- Dashboard.setVisible(true);
- }catch(Exception e) {System.out.print(e);}
- }
- //*******************************************************************************//
- });
- btnSave.setBounds(438, 329, 63, 25);
- contentPane.add(btnSave);
- JButton btnNewButton = new JButton("Preview");
- btnNewButton.setBounds(330, 329, 90, 25);
- contentPane.add(btnNewButton);
- //*******************************************************************************//
- JButton btnNewButton_1 = new JButton("Add image...");
- btnNewButton_1.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- String location ="";
- String name ="";
- File monImage = new File(location);
- try {
- Class.forName("com.mysql.jdbc.Driver");
- Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/supnews?useSSL=true","root","");
- Statement st = con.createStatement();
- ResultSet rs = st.executeQuery("SELECT * FROM Image WHERE ID = '"+ jtf.getText()+ "'");
- if(rs.next()) {
- byte [] img = rs.getBytes("Pic");
- ImageIcon image = new ImageIcon(img);
- Image im = image.getImage();
- Image myImg = im.getScaledInstance(label.getWidth(), label.getHeight() , Image.SCALE_SMOOTH);
- ImageIcon newImage = new ImageIcon(myImg);
- label.setIcon(newImage);
- }
- else {
- JOptionPane.showMessageDialog(null, "non data");
- }
- FileInputStream istreamImage = new FileInputStream(monImage);
- } catch (Exception error) {
- error.printStackTrace();
- System.out.println(error);
- }
- }});
- btnNewButton_1.setBounds(175, 329, 115, 25);
- contentPane.add(btnNewButton_1);
- //*******************************************************************************//
- JSeparator separator_1 = new JSeparator();
- separator_1.setForeground(Color.BLACK);
- separator_1.setBounds(-11, 385, 536, 15);
- contentPane.add(separator_1);
- JTextArea area_text = new JTextArea();
- area_text.setBounds(175, 89, 326, 225);
- test = area_text;
- contentPane.add(area_text);
- //*******************************************************************************//
- JButton del_article = new JButton("delete");
- del_article.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- /*ResultSet result;
- String res = null;
- try {
- Class.forName("com.mysql.jdbc.Driver");
- Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/supnews?useSSL=true","root","");
- Statement st =con.createStatement();
- result = st.executeQuery("select Title from article = "+mylist.getSelectedItem().toString());
- while(result.next()){
- res = result.getString("Title");
- }
- st.executeUpdate("DELETE FROM article where Title = " +"'"+res+"'");
- Dashboard.this.dispose();
- Dashboard Dashboard= new Dashboard();
- Dashboard.setVisible(true);
- }catch(Exception e){System.out.println(e);}*/
- PreparedStatement ps;
- String valeur = (String)mylist.getSelectedItem();
- try {
- Class.forName("com.mysql.jdbc.Driver");
- Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/supnews?useSSL=true","root","");
- Statement st =con.createStatement();
- ps = con.prepareStatement("DELETE FROM article where Title='"+valeur+"'");
- ps.execute();
- Dashboard.this.dispose();
- Dashboard Dashboard= new Dashboard();
- Dashboard.setVisible(true);
- }catch (Exception error) {
- error.printStackTrace();
- System.out.println(error);
- }
- //*******************************************************************************//
- }
- });
- del_article.setBounds(52, 329, 97, 25);
- contentPane.add(del_article);
- //*******************************************************************************//
- JButton btnView = new JButton("view");
- btnView.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- PreparedStatement ps=null;
- ResultSet rs=null;
- String valeur = (String)mylist.getSelectedItem();
- String cont = "Content";
- titre_article.setText(valeur);
- rs = find(area_text.getText());
- /* try {
- Class.forName("com.mysql.jdbc.Driver");
- Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/supnews?useSSL=true","root","");
- Statement st =con.createStatement();
- String sql = "Select Content from article where Title =" + "' "+valeur+" ' " ;
- ps=con.prepareStatement(sql);
- rs = pst.executeQuery();
- while(rs.next()) {
- //titre_article.setText(rs.getString("Title"));
- area_text.setText(rs.getString("Content"));
- area_text.setText(rs.getObject(1).toString());
- System.out.println(rs.getObject("Content").toString());
- }
- }catch(Exception e){System.out.println(e);}*/
- try {
- Connection con = null;
- con = DriverManager.getConnection("jdbc:mysql://localhost:3306/supnews?useSSL=true","root","");
- ps = con.prepareStatement("Select Content from article where Title =" + "'" + valeur + "'");
- rs = ps.executeQuery();
- if(rs.next()) {
- area_text.setText(rs.getString("Content"));
- }
- }catch(Exception e){JOptionPane.showMessageDialog(null, e.getMessage());}
- return;
- //*******************************************************************************//
- }
- });
- btnView.setBounds(52, 272, 97, 25);
- contentPane.add(btnView);
- //*******************************************************************************//
- JButton btnUpdate = new JButton("Update");
- btnUpdate.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- try {
- PreparedStatement pst=null;
- ResultSet rs=null;
- String valeur = (String)mylist.getSelectedItem();
- titre_article.setText(valeur);
- String titre = titre_article.getText();
- String champ = test.getText();
- String ID = "1";
- Class.forName("com.mysql.jdbc.Driver");
- Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/supnews?useSSL=true","root","");
- Statement st =con.createStatement();
- pst = con.prepareStatement("Select ID from article where Title =" + "'" + valeur + "'");
- rs = pst.executeQuery();
- String sql =("UPDATE article SET Title = " + "'"+ titre +"'" + ", Content =" + "'" + champ + "'" + "WHERE ID = 6");
- pst=con.prepareStatement(sql);
- pst.executeUpdate();
- rs = pst.executeQuery();
- while(rs.next()) {
- titre_article.setText(rs.getString("Title"));
- area_text.setText(rs.getString("Content"));
- }
- Dashboard.this.dispose();
- Dashboard Dashboard= new Dashboard();
- Dashboard.setVisible(true);
- }catch(Exception e) {System.out.print(e);}
- }});
- btnUpdate.setBounds(412, 354, 89, 23);
- contentPane.add(btnUpdate);
- //*******************************************************************************//
- }
- protected ResultSet find(String text) {
- // TODO Auto-generated method stub
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement