Advertisement
Guest User

java

a guest
Aug 5th, 2018
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package dbconn;
  7. import java.sql.*;
  8. /**
  9. *
  10. * @author MYPC
  11. */
  12. public class DBConn {
  13.  
  14. /**
  15. * @param args the command line arguments
  16. */
  17. public static void main(String[] args) {
  18. insert();
  19. select();
  20. numberPalindrome();
  21. reverseArray();
  22. stringPalindrome();
  23.  
  24.  
  25. }
  26. public static void insert(){
  27. String dbURL = "jdbc:mysql://localhost:3306/sampledb";
  28. String username = "root";
  29. String password = "";
  30. String query = "INSERT INTO Users(username, password, fullname, email) Values(?,?,?,?)";
  31.  
  32. try {
  33. Connection conn = DriverManager.getConnection(dbURL, username, password);
  34. if (conn != null) {
  35. System.out.println("Connected");
  36. }
  37. PreparedStatement statement = conn.prepareStatement(query);
  38. statement.setString(1, "bill2222");
  39. statement.setString(2, "secretpass22");
  40. statement.setString(3, "bill gates22");
  41. statement.setString(4, "bill@gmail.com22");
  42.  
  43. int rowsInserted = statement.executeUpdate();
  44. if (rowsInserted > 0) {
  45. System.out.println("Successfully entered");
  46. }
  47. conn.close();
  48. }catch(Exception ex) {
  49. System.out.println("Error: " + ex);
  50. }
  51. }
  52.  
  53. public static void select() {
  54. String dbURL = "jdbc:mysql://localhost:3306/sampledb";
  55. String username = "root";
  56. String password = "";
  57. String query = "SELECT * from Users";
  58.  
  59. try {
  60. Connection conn = DriverManager.getConnection(dbURL, username, password);
  61. if (conn != null) {
  62. System.out.println("Connected");
  63. }
  64. Statement statement = conn.createStatement();
  65. ResultSet result = statement.executeQuery(query);
  66.  
  67. int count = 0;
  68.  
  69. while (result.next()) {
  70. String name = result.getString(2);
  71. String pass = result.getString(3);
  72. String fullname = result.getString("fullname");
  73. String email = result.getString(5);
  74. String output = "User #%d: %s - %s - %s - %s";
  75.  
  76. System.out.println(String.format(output, ++count, name, pass, fullname, email));
  77.  
  78. }
  79.  
  80. conn.close();
  81. }catch(Exception ex) {
  82. System.out.println("Error: " + ex);
  83. }
  84. }
  85.  
  86. public static void numberPalindrome() {
  87. int number = 12345;
  88. int remainder = 0, sum = 0;
  89. while(number > 0) {
  90. remainder = number %10;
  91.  
  92. sum = (sum * 10) + remainder;
  93. number /= 10;
  94. }
  95. System.out.println(sum);
  96. }
  97.  
  98. public static void reverseArray() {
  99. int arr[] = {1, 2,3,4,5};
  100.  
  101. for(int i = 0; i < arr.length/2; i++) {
  102. int temp = arr[i];
  103. arr[i] = arr[arr.length-1 -i];
  104. arr[arr.length-1 -i] = temp;
  105. }
  106. for(int n : arr) {
  107. System.out.print(n);
  108. }
  109. }
  110.  
  111. public static void stringPalindrome() {
  112. String word = "123";
  113. String rev = "";
  114. // for(int i = word.length()-1; i >= 0 ; i--) {
  115. // rev += word.charAt(i);
  116. // }
  117. for(int i = 0; i < word.length();i++) {
  118. rev += word.charAt(word.length()-i -1);
  119. }
  120. System.out.println("\n"+rev);
  121. }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement