Advertisement
Guest User

Adresar

a guest
Mar 7th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.02 KB | None | 0 0
  1. import org.eclipse.swt.widgets.Display;
  2. import org.eclipse.swt.widgets.Shell;
  3. import org.eclipse.swt.widgets.List;
  4.  
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.util.ArrayList;
  9.  
  10. import org.eclipse.swt.SWT;
  11. import org.eclipse.swt.widgets.Text;
  12.  
  13. import com.mysql.jdbc.Connection;
  14. import com.mysql.jdbc.PreparedStatement;
  15. import com.mysql.jdbc.Statement;
  16.  
  17. import org.eclipse.swt.widgets.Button;
  18. import org.eclipse.swt.widgets.Label;
  19. import org.eclipse.swt.events.SelectionAdapter;
  20. import org.eclipse.swt.events.SelectionEvent;
  21.  
  22. public class MySqlExample {
  23.  
  24. protected Shell shlMojAdresar;
  25. private Text txtIme;
  26. private Text txtPrezime;
  27. private Text txtEmail;
  28. private Text txtTelefon;
  29. private List lstKontakti;
  30.  
  31. ArrayList<String> listaKontakata = new ArrayList<String>();
  32.  
  33.  
  34.  
  35. // parametri za konekciju na bazu
  36. static String dbHost = "jdbc:mysql://127.0.0.1:3306/adresar1";
  37. static String dbUser = "root";
  38. static String dbPass = "";
  39.  
  40. // dodatne varijable za mysql
  41. static Connection conn = null;
  42. static Statement stmt = null;
  43. static PreparedStatement pstmt = null;
  44. static ResultSet rs = null;
  45.  
  46.  
  47.  
  48.  
  49. /**
  50. * Launch the application.
  51. * @param args
  52. */
  53. public static void main(String[] args) {
  54. try {
  55. MySqlExample window = new MySqlExample();
  56. window.open();
  57. } catch (Exception e) {
  58. e.printStackTrace();
  59. }
  60. }
  61.  
  62. /**
  63. * Open the window.
  64. */
  65. public void open() {
  66. Display display = Display.getDefault();
  67. createContents();
  68. shlMojAdresar.open();
  69. shlMojAdresar.layout();
  70. while (!shlMojAdresar.isDisposed()) {
  71. if (!display.readAndDispatch()) {
  72. display.sleep();
  73. }
  74. }
  75. }
  76.  
  77. /**
  78. * Create contents of the window.
  79. */
  80. protected void createContents() {
  81.  
  82. shlMojAdresar = new Shell();
  83. shlMojAdresar.setSize(382, 192);
  84. shlMojAdresar.setText("Moj adresar");
  85.  
  86. final Label lblId = new Label(shlMojAdresar, SWT.NONE);
  87.  
  88. // lista je definisana kao globalna
  89. lstKontakti = new List(shlMojAdresar, SWT.BORDER);
  90. lstKontakti.addSelectionListener(new SelectionAdapter() {
  91. @Override
  92. public void widgetSelected(SelectionEvent e) {
  93.  
  94. int recordId = lstKontakti.getSelectionIndex();
  95.  
  96.  
  97. // konacno dobijamo ID recorda na koji smo kliknuli
  98. lblId.setText(String.valueOf(listaKontakata.get(recordId)));
  99.  
  100. String sql = "select * from `kontakti` where `id` = '" + lblId.getText() + "'";
  101. System.out.println(sql);
  102.  
  103. try {
  104. // otvaramo konekciju ka bazi
  105. conn = (Connection)DriverManager.getConnection(dbHost, dbUser, dbPass);
  106.  
  107. stmt = (Statement)conn.createStatement();
  108. rs = stmt.executeQuery(sql);
  109.  
  110. while(rs.next()) {
  111. //lstKontakti.add(rs.getString("ime") + " " + rs.getString("prezime"));
  112.  
  113. if (rs.getString("ime") != null) txtIme.setText(rs.getString("ime")); else txtIme.setText("");
  114. if (rs.getString("prezime") != null) txtPrezime.setText(rs.getString("prezime")); else txtPrezime.setText("");
  115. if (rs.getString("telefon") != null) txtTelefon.setText(rs.getString("telefon")); else txtTelefon.setText("");
  116. if (rs.getString("email") != null) txtEmail.setText(rs.getString("email")); else txtEmail.setText("");
  117.  
  118.  
  119.  
  120. }
  121.  
  122. // zatvaramo konekciju
  123. conn.close();
  124. } catch (SQLException e1) {
  125. // TODO Auto-generated catch block
  126. e1.printStackTrace();
  127. }
  128.  
  129.  
  130.  
  131.  
  132. }
  133. });
  134. lstKontakti.setBounds(10, 10, 150, 129);
  135.  
  136. txtIme = new Text(shlMojAdresar, SWT.BORDER);
  137. txtIme.setBounds(238, 10, 118, 21);
  138.  
  139. txtPrezime = new Text(shlMojAdresar, SWT.BORDER);
  140. txtPrezime.setBounds(238, 37, 118, 21);
  141.  
  142. txtEmail = new Text(shlMojAdresar, SWT.BORDER);
  143. txtEmail.setBounds(238, 64, 118, 21);
  144.  
  145. txtTelefon = new Text(shlMojAdresar, SWT.BORDER);
  146. txtTelefon.setBounds(238, 91, 118, 21);
  147.  
  148. Button btnOk = new Button(shlMojAdresar, SWT.NONE);
  149. btnOk.addSelectionListener(new SelectionAdapter() {
  150. @Override
  151. public void widgetSelected(SelectionEvent e) {
  152.  
  153. // ako je vrednost labele 0, radim insert, inace radim update
  154. String sql = "";
  155. if (lblId.getText() == "0") {
  156.  
  157. sql = "insert into `kontakti` ";
  158. sql += " (`ime`, `prezime`, `telefon`, `email`) ";
  159. sql += " values ( ";
  160. sql += "'" + txtIme.getText() + "', ";
  161. sql += "'" + txtPrezime.getText() + "', ";
  162. sql += "'" + txtTelefon.getText() + "', ";
  163. sql += "'" + txtEmail.getText() + "'";
  164. sql += ")";
  165.  
  166. } else {
  167.  
  168. sql = "update `kontakti` set"
  169. + "`ime` = '" + txtIme.getText() + "',"
  170. + "`prezime` = '" + txtPrezime.getText() + "',"
  171. + "`telefon` = '" + txtTelefon.getText() + "',"
  172. + "`email` = '" + txtEmail.getText() + "'"
  173. + " where `id` = '" + lblId.getText() + "'";
  174.  
  175. }
  176.  
  177.  
  178.  
  179. System.out.println(sql);
  180.  
  181. try {
  182. conn = (Connection)DriverManager.getConnection(dbHost, dbUser, dbPass);
  183. stmt = (Statement)conn.createStatement();
  184. stmt.execute(sql);
  185. conn.close();
  186. listContacts();
  187. lblId.setText("0");
  188. txtIme.setText("");
  189. txtPrezime.setText("");
  190. txtTelefon.setText("");
  191. txtEmail.setText("");
  192. txtIme.setFocus();
  193.  
  194.  
  195. } catch (SQLException e1) {
  196. // TODO Auto-generated catch block
  197. e1.printStackTrace();
  198. }
  199.  
  200.  
  201.  
  202. }
  203. });
  204. btnOk.setBounds(238, 118, 75, 25);
  205. btnOk.setText("Unesi");
  206.  
  207. Label lblNewLabel = new Label(shlMojAdresar, SWT.NONE);
  208. lblNewLabel.setAlignment(SWT.RIGHT);
  209. lblNewLabel.setBounds(177, 14, 55, 15);
  210. lblNewLabel.setText("Ime:");
  211.  
  212. Label lblNewLabel_1 = new Label(shlMojAdresar, SWT.NONE);
  213. lblNewLabel_1.setAlignment(SWT.RIGHT);
  214. lblNewLabel_1.setBounds(177, 40, 55, 15);
  215. lblNewLabel_1.setText("Prezime:");
  216.  
  217. Label lblNewLabel_2 = new Label(shlMojAdresar, SWT.NONE);
  218. lblNewLabel_2.setAlignment(SWT.RIGHT);
  219. lblNewLabel_2.setBounds(177, 67, 55, 15);
  220. lblNewLabel_2.setText("E-mail:");
  221.  
  222. Label lblNewLabel_3 = new Label(shlMojAdresar, SWT.NONE);
  223. lblNewLabel_3.setAlignment(SWT.RIGHT);
  224. lblNewLabel_3.setBounds(177, 94, 55, 15);
  225. lblNewLabel_3.setText("Telefon:");
  226.  
  227.  
  228. lblId.setBounds(177, 118, 55, 15);
  229. lblId.setText("0");
  230.  
  231.  
  232. listContacts();
  233.  
  234. }
  235.  
  236.  
  237. // izlistava sve kontakte
  238. void listContacts() {
  239.  
  240. String sql = "select * from `kontakti` order by `ime`, `prezime`";
  241. lstKontakti.removeAll();
  242. listaKontakata.clear();
  243.  
  244. try {
  245. // otvaramo konekciju ka bazi
  246. conn = (Connection)DriverManager.getConnection(dbHost, dbUser, dbPass);
  247.  
  248. stmt = (Statement)conn.createStatement();
  249. rs = stmt.executeQuery(sql);
  250.  
  251. while(rs.next()) {
  252. lstKontakti.add(rs.getString("ime") + " " + rs.getString("prezime"));
  253. listaKontakata.add(rs.getString("id"));
  254. }
  255.  
  256. // zatvaramo konekciju
  257. conn.close();
  258.  
  259.  
  260.  
  261. } catch (SQLException e) {
  262. // TODO Auto-generated catch block
  263. e.printStackTrace();
  264. }
  265.  
  266.  
  267. }
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement