Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package laborator7;
- import java.awt.BorderLayout;
- import java.awt.Component;
- import java.awt.FlowLayout;
- import java.awt.HeadlessException;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import javax.swing.ImageIcon;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- import javax.swing.JToolBar;
- import com.mysql.jdbc.Statement;
- public class MyFrame extends JFrame {
- /**
- *
- */
- //Start declarare variabile utilizate
- private JButton btnFirst,btnPrevious, btnLast,btnAdd,btnEdit,btnDelete,btnSearch,btnSave,btnUndo;
- private JTextField txtVarsta, txtID,txtNume,txtAfis;
- private Connection con;
- private ResultSet rs;
- private Statement sql;
- private static final long serialVersionUID = 1L;
- private Connection stabilireConecsiune(){
- Connection con=null;
- try {
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- } catch (InstantiationException e) {
- JOptionPane.showMessageDialog(null,"eroare");
- } catch (IllegalAccessException e) {
- JOptionPane.showMessageDialog(null,"eroare");
- } catch (ClassNotFoundException e) {
- JOptionPane.showMessageDialog(null,"eroare");
- }
- try {
- con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
- } catch (SQLException e) {
- JOptionPane.showMessageDialog(null,e);
- }
- return con;
- }
- private void scrieInceput(){
- try {
- txtID.setText(rs.getInt("Id")+"");
- } catch (SQLException e) {
- JOptionPane.showMessageDialog(null, e);
- }
- try {
- txtNume.setText(rs.getString("nume"));
- } catch (SQLException e) {
- JOptionPane.showMessageDialog(null, e);
- }
- try {
- txtVarsta.setText(rs.getInt(3)+" ");
- } catch (SQLException e) {
- JOptionPane.showMessageDialog(null, e);
- }
- }
- private void scrieNext(){
- try {
- while( rs.next() ){
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- private void inchideConecsiune(){
- try {
- con.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- try {
- rs.close();
- } catch (SQLException e1) {
- JOptionPane.showMessageDialog(null,e1);
- }
- try {
- sql.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- private void scrieCaseteDeText(){
- if (con!=null) {
- try {
- sql=(Statement)con.createStatement();
- } catch (SQLException e) {
- JOptionPane.showMessageDialog(null, e);
- }
- try {
- if(sql!=null)
- rs=sql.executeQuery("Select * From persoane");
- } catch (SQLException e) {
- JOptionPane.showMessageDialog(null, e);
- }
- }
- try {
- if(rs.next()) {
- scrieInceput();
- }
- } catch (HeadlessException | SQLException e2) {
- e2.printStackTrace();
- }
- inchideConecsiune();
- }
- public MyFrame()
- {
- super("Tabelul MySQL - persoane");
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setSize(300,300);
- getContentPane().setLayout(new BorderLayout());
- JToolBar myBar= new JToolBar();
- getContentPane().add(myBar, BorderLayout.NORTH);
- btnFirst= new JButton();
- btnPrevious= new JButton();
- btnLast= new JButton();
- btnAdd= new JButton();
- btnEdit= new JButton();
- btnDelete= new JButton();
- btnSearch= new JButton();
- btnSave= new JButton();
- btnUndo= new JButton();
- txtAfis= new JTextField();
- btnFirst.setIcon(new ImageIcon("MoveFirst.png"));
- btnFirst.setEnabled(false);
- btnPrevious.setIcon(new ImageIcon("MovePrevious.png"));
- btnPrevious.setEnabled(false);
- btnLast.setIcon(new ImageIcon("MoveLast.png"));
- btnAdd.setIcon(new ImageIcon("Add.png"));
- btnEdit.setIcon(new ImageIcon("Edit.png"));
- btnDelete.setIcon(new ImageIcon("Delete.png"));
- btnSearch.setIcon(new ImageIcon("find.JPG"));
- btnSave.setIcon(new ImageIcon("save.JPG"));
- btnSave.setEnabled(false);
- btnUndo.setIcon(new ImageIcon("undo.JPG"));
- btnUndo.setEnabled(false);
- myBar.add(btnFirst);
- myBar.add(btnPrevious);
- myBar.add(txtAfis);
- myBar.add(btnLast);
- myBar.add(btnAdd);
- myBar.add(btnEdit);
- myBar.add(btnDelete);
- myBar.add(btnSearch);
- myBar.add(btnSave);
- myBar.add(btnUndo);
- JPanel panel= new JPanel();
- getContentPane().add(panel, BorderLayout.CENTER);
- panel.setLayout(new FlowLayout(FlowLayout.CENTER));
- // panel.setSize(100, 100);
- JLabel empty1= new JLabel(" ");
- JLabel lblID= new JLabel("ID ");
- lblID.setAlignmentX(Component.RIGHT_ALIGNMENT);
- txtID= new JTextField(10);
- txtID.setAlignmentX(Component.LEFT_ALIGNMENT);
- JLabel empty= new JLabel(" ");
- panel.add(empty1);
- panel.add(lblID);
- panel.add(txtID);
- panel.add(empty);
- JLabel empty2= new JLabel(" ");
- JLabel lblNume= new JLabel("Nume ");
- lblNume.setAlignmentX(Component.RIGHT_ALIGNMENT);
- txtNume= new JTextField(10);
- txtNume.setAlignmentX(Component.LEFT_ALIGNMENT);
- JLabel empty3= new JLabel(" ");
- panel.add(empty2);
- panel.add(lblNume);
- panel.add(txtNume);
- panel.add(empty3);
- JLabel empty4= new JLabel(" ");
- JLabel lblVarsta= new JLabel("Varsta");
- lblVarsta.setAlignmentX(Component.RIGHT_ALIGNMENT);
- txtVarsta= new JTextField(10);
- txtVarsta.setAlignmentX(Component.LEFT_ALIGNMENT);
- JLabel empty5= new JLabel(" ");
- panel.add(empty4);
- panel.add(lblVarsta);
- panel.add(txtVarsta);
- panel.add(empty5);
- con=stabilireConecsiune();
- scrieCaseteDeText();
- setLocationRelativeTo(null);
- panel.setVisible(true);
- }
- abstract class urmatorul implements MouseListener{
- String btnApasat;
- public urmatorul(String btnApasat){
- this.btnApasat=btnApasat;
- }
- @Override
- public void mouseClicked(MouseEvent arg0) {
- if( btnApasat.equals("urmatorul") ){
- }
- }
- }
- public static void main(String[] args) throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException
- {
- MyFrame frm = new MyFrame();
- frm.setVisible(true);
- /* Statement sql;
- ResultSet rs;
- sql = (Statement) con.createStatement();
- rs = sql.executeQuery("select * from persoane");
- while(rs.next())
- System.out.println("id="+rs.getInt("Id")+", nume= "+ rs.getString("nume")+", varsta="+rs.getInt(3));
- con.close();
- sql.close();
- rs.close();*/
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement