Advertisement
Guest User

Untitled

a guest
Apr 11th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. ----
  2. // Create Point objects from the coordinate array
  3. Point[] points = new Point[coords.length];
  4. for (int i = 0; i<points.length; i++) {
  5. points[i] = new Point(coords[i][0], coords[i][1]);
  6. }
  7.  
  8. someArray[][] = { {value, value}, {value, value}, {value, value} };
  9.  
  10. ----
  11. ITTERATION
  12.  
  13. int ar[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  14. int x;
  15.  
  16. // iterating over an array
  17. for (int i : ar) {
  18.  
  19. // accessing each element of array
  20. x = i;
  21. System.out.print(x + " ");
  22. }
  23.  
  24. https://stackoverflow.com/questions/11844012/they-say-in-java-every-thing-is-an-object-is-that-true
  25.  
  26. resultSet.close();
  27. query.close();
  28. connection.close();
  29.  
  30. ----
  31.  
  32. package project;
  33.  
  34. import java.sql.*;
  35.  
  36. public class DatabaseConnection {
  37.  
  38. private String dbUsername, dbPassword, dbHost, dbPort, dbName, dbUrl;
  39.  
  40. private Connection dbConnection;
  41.  
  42. public DatabaseConnection() throws ClassNotFoundException, SQLException {
  43. initComponents();
  44. }
  45.  
  46. private void initComponents() throws ClassNotFoundException, SQLException {
  47. Class.forName("com.mysql.jdbc.Driver");
  48.  
  49. this.dbUsername = "root";
  50. this.dbPassword = "root";
  51. this.dbHost = "localhost";
  52. this.dbPort = "3306";
  53. this.dbName = "test_my_database";
  54.  
  55. this.dbUrl = "jdbc.mysql://" + this.dbHost + ":" + this.dbPort + "/" + this.dbName;
  56.  
  57. this.dbConnection = DriverManager.getConnection(dbUrl, dbUsername, dbPassword);
  58.  
  59. System.out.println("CONNECTION ESTABLISHED");
  60. }
  61.  
  62. // public Connection getConnection() {
  63. //
  64. // }
  65. }
  66.  
  67. ----
  68.  
  69. package project;
  70.  
  71. import java.sql.Connection;
  72. import java.sql.ResultSet;
  73. import java.sql.SQLException;
  74. import java.sql.Statement;
  75.  
  76. public class Project {
  77.  
  78. public static void main(String[] args) {
  79.  
  80. try {
  81. DatabaseConnection foo = new DatabaseConnection();
  82.  
  83. Connection kook = foo.getConnection();
  84.  
  85. Statement stat = foo.createStatement();
  86.  
  87. ResultSet result = stat.executeQuery("select");
  88.  
  89. while(result.next()) {
  90. int idusers = result.getInt("idusers");
  91. String name = result.getString("name");
  92. String email = result.getString("email");
  93. float value = result.getFloat("value");
  94. System.out.println(idusers + ": " + name + " at " + email + ". value: " + value);
  95. }
  96. }
  97. catch(ClassNotFoundException ex) {
  98. System.out.println("Driver not found");
  99. ex.printStackTrace(System.out);
  100. }
  101. catch(SQLException ex) {
  102. System.out.println("ERROR: Connection not established");
  103. ex.printStackTrace(System.out);
  104. }
  105. }
  106. }
  107.  
  108. ----
  109.  
  110. package project;
  111.  
  112. import java.awt.BorderLayout;
  113. import java.awt.GridLayout;
  114. import java.awt.event.ActionEvent;
  115. import java.awt.event.ActionListener;
  116. import javax.swing.*;
  117.  
  118. public class FrameDataInsert extends JFrame {
  119. // behaviours
  120. private BorderLayout borderLayout;
  121. private GridLayout gridLayout;
  122. // frame
  123. private JLabel label;
  124. private JButton button;
  125. // panel
  126. private JPanel panel;
  127. private JTextArea username;
  128. private JTextArea password;
  129. private JTextArea value;
  130.  
  131. public FrameDataInsert() {
  132. initComponents();
  133.  
  134. this.setTitle("java.sql API testing");
  135. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  136. this.pack();
  137. this.setVisible(true);
  138. }
  139.  
  140. private void initComponents() {
  141. //behaviours
  142. gridLayout = new GridLayout(3,0);
  143. // frame
  144. label = new JLabel ("Insert data in fields, then press button");
  145. button = new JButton("insert");
  146. button.addActionListener(new ActionListener() {
  147. @Override
  148. public void actionPerformed(ActionEvent stim) {
  149. insertData();
  150. }
  151. });
  152. // panel
  153. panel = new JPanel(gridLayout);
  154. username = new JTextArea();
  155. password = new JTextArea();
  156. value = new JTextArea();
  157. // add
  158. this.add(label, BorderLayout.PAGE_START);
  159. this.add(button, BorderLayout.PAGE_END);
  160.  
  161. this.add(panel, borderLayout.CENTER);
  162. panel.add(username);
  163. panel.add(password);
  164. panel.add(value);
  165. }
  166.  
  167. private void insertData() {
  168.  
  169. }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement