Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.eclipse.swt.widgets.Display;
- import org.eclipse.swt.widgets.Shell;
- import org.eclipse.swt.widgets.List;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import org.eclipse.swt.SWT;
- import org.eclipse.swt.widgets.Text;
- import com.mysql.jdbc.Connection;
- import com.mysql.jdbc.PreparedStatement;
- import com.mysql.jdbc.Statement;
- import org.eclipse.swt.widgets.Button;
- import org.eclipse.swt.widgets.Label;
- import org.eclipse.swt.events.SelectionAdapter;
- import org.eclipse.swt.events.SelectionEvent;
- public class App0701 {
- protected Shell shlMojAdresar;
- private Text txtIme;
- private Text txtPrezime;
- private Text txtEmail;
- private Text txtTelefon;
- private List lstKontakti;
- ArrayList<String> listaKontakata = new ArrayList<String>();
- // parametri za konekciju na bazu
- static String dbHost = "jdbc:mysql://127.0.0.1:3306/adresar1";
- static String dbUser = "root";
- static String dbPass = "";
- // dodatne varijable za mysql
- static Connection conn = null;
- static Statement stmt = null;
- static PreparedStatement pstmt = null;
- static ResultSet rs = null;
- /**
- * Launch the application.
- * @param args
- */
- public static void main(String[] args) {
- try {
- App0701 window = new App0701();
- window.open();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * Open the window.
- */
- public void open() {
- Display display = Display.getDefault();
- createContents();
- shlMojAdresar.open();
- shlMojAdresar.layout();
- while (!shlMojAdresar.isDisposed()) {
- if (!display.readAndDispatch()) {
- display.sleep();
- }
- }
- }
- /**
- * Create contents of the window.
- */
- protected void createContents() {
- shlMojAdresar = new Shell();
- shlMojAdresar.setSize(382, 192);
- shlMojAdresar.setText("Moj adresar");
- Label lblId = new Label(shlMojAdresar, SWT.NONE);
- // lista je definisana kao globalna
- lstKontakti = new List(shlMojAdresar, SWT.BORDER);
- lstKontakti.addSelectionListener(new SelectionAdapter() {
- @Override
- public void widgetSelected(SelectionEvent e) {
- int recordId = lstKontakti.getSelectionIndex();
- // konacno dobijamo ID recorda na koji smo kliknuli
- lblId.setText(String.valueOf(listaKontakata.get(recordId)));
- String sql = "select * from `kontakti` where `id` = '" + lblId.getText() + "'";
- System.out.println(sql);
- try {
- // otvaramo konekciju ka bazi
- conn = (Connection)DriverManager.getConnection(dbHost, dbUser, dbPass);
- stmt = (Statement)conn.createStatement();
- rs = stmt.executeQuery(sql);
- while(rs.next()) {
- //lstKontakti.add(rs.getString("ime") + " " + rs.getString("prezime"));
- if (rs.getString("ime") != null) txtIme.setText(rs.getString("ime")); else txtIme.setText("");
- if (rs.getString("prezime") != null) txtPrezime.setText(rs.getString("prezime")); else txtPrezime.setText("");
- if (rs.getString("telefon") != null) txtTelefon.setText(rs.getString("telefon")); else txtTelefon.setText("");
- if (rs.getString("email") != null) txtEmail.setText(rs.getString("email")); else txtEmail.setText("");
- }
- // zatvaramo konekciju
- conn.close();
- } catch (SQLException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- }
- });
- lstKontakti.setBounds(10, 10, 150, 129);
- txtIme = new Text(shlMojAdresar, SWT.BORDER);
- txtIme.setBounds(238, 10, 118, 21);
- txtPrezime = new Text(shlMojAdresar, SWT.BORDER);
- txtPrezime.setBounds(238, 37, 118, 21);
- txtEmail = new Text(shlMojAdresar, SWT.BORDER);
- txtEmail.setBounds(238, 64, 118, 21);
- txtTelefon = new Text(shlMojAdresar, SWT.BORDER);
- txtTelefon.setBounds(238, 91, 118, 21);
- Button btnOk = new Button(shlMojAdresar, SWT.NONE);
- btnOk.addSelectionListener(new SelectionAdapter() {
- @Override
- public void widgetSelected(SelectionEvent e) {
- // ako je vrednost labele 0, radim insert, inace radim update
- String sql = "";
- if (lblId.getText() == "0") {
- sql = "insert into `kontakti` ";
- sql += " (`ime`, `prezime`, `telefon`, `email`) ";
- sql += " values ( ";
- sql += "'" + txtIme.getText() + "', ";
- sql += "'" + txtPrezime.getText() + "', ";
- sql += "'" + txtTelefon.getText() + "', ";
- sql += "'" + txtEmail.getText() + "'";
- sql += ")";
- } else {
- sql = "update `kontakti` set"
- + "`ime` = '" + txtIme.getText() + "',"
- + "`prezime` = '" + txtPrezime.getText() + "',"
- + "`telefon` = '" + txtTelefon.getText() + "',"
- + "`email` = '" + txtEmail.getText() + "'"
- + " where `id` = '" + lblId.getText() + "'";
- }
- System.out.println(sql);
- try {
- conn = (Connection)DriverManager.getConnection(dbHost, dbUser, dbPass);
- stmt = (Statement)conn.createStatement();
- stmt.execute(sql);
- conn.close();
- listContacts();
- } catch (SQLException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- }
- });
- btnOk.setBounds(238, 118, 75, 25);
- btnOk.setText("Unesi");
- Label lblNewLabel = new Label(shlMojAdresar, SWT.NONE);
- lblNewLabel.setAlignment(SWT.RIGHT);
- lblNewLabel.setBounds(177, 14, 55, 15);
- lblNewLabel.setText("Ime:");
- Label lblNewLabel_1 = new Label(shlMojAdresar, SWT.NONE);
- lblNewLabel_1.setAlignment(SWT.RIGHT);
- lblNewLabel_1.setBounds(177, 40, 55, 15);
- lblNewLabel_1.setText("Prezime:");
- Label lblNewLabel_2 = new Label(shlMojAdresar, SWT.NONE);
- lblNewLabel_2.setAlignment(SWT.RIGHT);
- lblNewLabel_2.setBounds(177, 67, 55, 15);
- lblNewLabel_2.setText("E-mail:");
- Label lblNewLabel_3 = new Label(shlMojAdresar, SWT.NONE);
- lblNewLabel_3.setAlignment(SWT.RIGHT);
- lblNewLabel_3.setBounds(177, 94, 55, 15);
- lblNewLabel_3.setText("Telefon:");
- lblId.setBounds(177, 118, 55, 15);
- lblId.setText("0");
- listContacts();
- }
- // izlistava sve kontakte
- void listContacts() {
- String sql = "select * from `kontakti` order by `ime`, `prezime`";
- lstKontakti.removeAll();
- listaKontakata.clear();
- try {
- // otvaramo konekciju ka bazi
- conn = (Connection)DriverManager.getConnection(dbHost, dbUser, dbPass);
- stmt = (Statement)conn.createStatement();
- rs = stmt.executeQuery(sql);
- while(rs.next()) {
- lstKontakti.add(rs.getString("ime") + " " + rs.getString("prezime"));
- listaKontakata.add(rs.getString("id"));
- }
- // zatvaramo konekciju
- conn.close();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement