Advertisement
Sasuke_Uchiha

Tutorial - Database Java.

Jun 29th, 2019
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.70 KB | None | 0 0
  1. What is Database?
  2. The database is an organized collection of data.
  3. MySQL Database?
  4. 'My' - the name of the daughter of co-founder.
  5. SQL - Standard Query Language.
  6.  
  7. If you want to connect and practice database? (Extra)
  8. Work on the notepad, so you would remember the classes name that's all you need in the exam.
  9. Download the JDBC Driver - https://www.dropbox.com/s/p6ztza1eqos8x3j/SQL.rar?dl=0
  10. Copy the files from SQL folder, place it in your java folder where you installed it, in the bin folder.
  11. Copy the path to bin folder.
  12. Now open Advance System Settings -> Enviorment Variables ->
  13. add this
  14. ThePathYouCopied\mysql-connector-java-5.1.47-bin.jar
  15. to CLASSPATH
  16. if you don't have any CLASSPATH variable create and add this.
  17. Example: C:\Program Files\Java\jre1.8.0_191\bin\mysql-connector-java-5.1.47-bin.jar
  18. For MySQL Server
  19. Download WAMP.
  20. Run it.
  21. Learn how to use MySQL from PhpMyAdmin.
  22.  
  23. What to learn? (Only the basics)
  24. FX or Swing?
  25. None, database has nothing to do with FX or Swing. We will learn how to run a database using prints.
  26.  
  27. Classes used:
  28. DriverManager
  29. SQLException
  30. Connection
  31. Statement
  32. ResultSet
  33.  
  34. To check if you have installed the driver properly. (Extra)
  35. Run this code
  36.  
  37. import java.sql.*; //This is used to import all the classes we need to establish a connection and use it.
  38. public class DriverTest { //DriverTest is class name.
  39.  
  40. static { //The Static Block
  41. try { //Try Block
  42. Class.forName("com.mysql.jdbc.Driver");
  43. } catch(Exception ex) { //Catching the exception
  44. System.out.println("Unable to load MySQL Driver");
  45. System.exit(0);//This will close the program if driver does not exsist.
  46. }
  47. }//Static block ends.
  48. public static void main(String args[]) { //The Main method, which runs in start.
  49. System.out.println("Java Driver Worked");
  50. }//Main method ends.
  51. }//Class ends.
  52.  
  53.  
  54. Setting up the connection: (According to Sir Shahbaz, your connection would already be established)
  55. Info required about of your database.
  56. Server port - Default (3306)
  57. Database name.
  58. Server Username - Default (root)
  59. Server Password - By Default, there is no password for root username.
  60.  
  61. Classes used:
  62. Connection
  63. DriverManager
  64. SQLException
  65.  
  66. Method:
  67. getConnection(String url,String Username, String Password);
  68.  
  69. Getting Started
  70. Create an object of class 'Connection'.
  71. Store connection in it.
  72. DriverManager.getConnection(); - This method returns the the Object of Connection class.
  73.  
  74. Code:
  75. import java.sql.*; //This is used to import all the classes we need to establish a connection and use it.
  76. public class ConnectionTest { //ConnectionTest is class name.
  77. public static void main(String args[]) {//The Main method, which runs in the start.
  78. try { //Try Block
  79. Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/accounts","Arose","123");//Connection is class. accounts is my database name. 3306 is port. Arose is my username. 123 is my password. DriverManager will select the JDBC driver and call getConnection method from it.
  80. }//End of try block
  81. catch(SQLException e) //Catch Block
  82. { System.out.println("Connection Failed");}
  83. }//End of main method
  84.  
  85. }//End of class
  86.  
  87. Your connection is stored
  88.  
  89. Inserting Data into your table.
  90.  
  91. Info required about of your database:
  92. Table name.
  93. Column names and type.
  94.  
  95. Classes used:
  96. SQLException
  97. Connection
  98. Statement
  99.  
  100. Method:
  101. createStatement();
  102. execute(String Query);
  103. excuteUpdate(String Query);
  104. getUpdateCount();
  105.  
  106. Getting Started
  107. Suppose
  108. we have already established a connection and stored it in our 'con' variable.
  109. String username stores username I need to store in my table.
  110. String password stores password I need to store in my table.
  111. My table name is Accounts.
  112. Columns are Username and Password, both are of String type.
  113. Now create an object of Statement, i.e create a new statement.
  114. con.createStatement(); //This returns a Statement.
  115. Create a string with your query.
  116. Execute the query.
  117.  
  118. Code 1:
  119. try {
  120. Statement stmt=con.createStatement(); //This will create our statement that we will be sent to database.
  121. String query = String.format("INSERT INTO Accounts (username,password) VALUES ('%s','%s')",username,password); //To Learn the SQL queries, i'll tell later in this tutorial, most likely sir might include the queries too.
  122. stmt.execute(query); //This will execute our query. Will send our data to the database.
  123. int rows =stmt.getUpdateCount(); //This returns the no of rows that were changed or inserted in the database.
  124. if(rows > 0) //If the no of rows changed was greater than 0 than means our code worked.
  125. System.out.println("Data has been stored");
  126. else
  127. System.out.println("Failed to store.");
  128. }
  129. catch(SQLException e) //Exception is thrown when, your access is to database is denied.
  130. {
  131. System.out.println("Error occured");
  132. }
  133. Code 2:
  134. try {
  135. Statement stmt=con.createStatement(); //Statement Created.
  136. String query = "INSERT INTO Accounts (username,password) VALUES ('Arose Khan Niazi','ParhLoAbTu')";
  137. int rows = stmt.executeUpdate(query); //This will execute our query. Will send our data to the database. And return the no of rows that have been changed or updated.
  138. if(rows > 0) //If the no of rows changed was greater than 0 than means our code worked. Means that Arose Khan Niazi with password ParhLoAbTu has been updated to my Accounts table.
  139. System.out.println("Data has been stored");
  140. else
  141. System.out.println("Failed to store.");
  142. }
  143. catch(SQLException e) //Exception is thrown when, your access is to database is denied.
  144. {
  145. System.out.println("Error occured");
  146. }
  147.  
  148. Getting Data from your table.
  149.  
  150. Info required about of your database:
  151. Table name.
  152. Column names and type.
  153.  
  154. Classes used:
  155. SQLException
  156. Connection
  157. Statement
  158. ResultSet
  159.  
  160. Method:
  161. createStatement();
  162. execute(String Query);
  163. excuteQuery(String Query);
  164. getResultSet();
  165. next();
  166. getString(String ColumnName);
  167.  
  168.  
  169. Getting Started
  170. Suppose
  171. we have already established a connection and stored it in our 'con' variable.
  172. My table name is Accounts.
  173. Columns are Username and Password, both are of String type.
  174. Now create an object of Statement, i.e create a new statement.
  175. con.createStatement(); //This returns a Statement.
  176. Create a string with your query.
  177. Execute the query and Store it the ResultSet Object.
  178. Retrieve and print data.
  179.  
  180.  
  181. Code 1:
  182. try {
  183. Statement stmt=con.createStatement(); //This will create our statement that we will be sent to database.
  184. String query = "SELECT * FROM Accounts"; //Query
  185. stmt.execute(query);//This will execute our query. Will get our data from the database.
  186. ResultSet rs =stmt.getResultSet(); //This will return all the data we have Selected from our database, and store it into rs variable of ResultSet class.
  187. while(rs.next()) //This checks if the data is available. Starts from row 0, will move to the first row and see if there is any data. This statement would work until we have gone through all of our data.
  188. {
  189. String username= rs.getString("Username"); //This will get the username at the place where our rs would be.
  190. String password= rs.getString("Password"); //This will get the password at the place where our rs would be.
  191. System.out.println("Username: "+username);
  192. System.out.println("\tPassword: "+password);
  193. }
  194. }
  195. catch(SQLException e) //Exception is thrown when, your access is to database is denied.
  196. {
  197. System.out.println("Error occured");
  198. }
  199.  
  200. Code 2:
  201. try {
  202. Statement stmt=con.createStatement(); //This will create our statement that we will be sent to database.
  203. String query = "SELECT * FROM Accounts"; //Query
  204. ResultSet rs= stmt.executeUpdate(query);//This will execute our query. Will get our data from the database. And store the data into ResultSet Object rs.
  205. while(rs.next()) //This checks if the data is available. Starts from row 0, will move to the first row and see if there is any data. This statement would work until we have gone through all of our data.
  206. {
  207. String username= rs.getString("Username"); //This will get the username at the place where our rs would be.
  208. String password= rs.getString("Password"); //This will get the password at the place where our rs would be.
  209. System.out.println("Username: "+username);
  210. System.out.println("\tPassword: "+password);
  211. }
  212. }
  213. catch(SQLException e) //Exception is thrown when, your access is to database is denied.
  214. {
  215. System.out.println("Error occured");
  216. }
  217.  
  218. Example:
  219. File DBTest1.java
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement