Advertisement
Guest User

prog

a guest
Dec 9th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.79 KB | None | 0 0
  1. package alcon;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.InputStreamReader;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.ResultSet;
  8. import java.sql.Statement;
  9.  
  10. /**
  11. *
  12. * @author Leonardo
  13. */
  14. public class AndreDB {
  15.  
  16. /**
  17. * @param args the command line arguments
  18. */
  19. public static void main(String[] args) {
  20.  
  21.  
  22. BufferedReader in;
  23. in = new BufferedReader(new InputStreamReader(System.in));
  24. try {
  25. int opcao = Integer.parseInt(in.readLine()); //captura a entrada feita pelo teclado, tem que ser numero
  26. while (opcao != 0) {
  27.  
  28. switch (opcao) {
  29. case 0: System.exit(0);
  30. break;
  31. case 1:
  32. insereMarca();
  33. break;
  34. case 2:
  35. telemoveisTiagoSousa();
  36. break;
  37. case 3:
  38. insereMarcaOnePlus();
  39. break;
  40. case 4:
  41. printiPhone7();
  42. default:
  43. System.out.println("Insira alguma opção válida");
  44. break;
  45. }
  46. }
  47.  
  48. } catch (Exception ex) {
  49. ex.printStackTrace();
  50. }
  51.  
  52. }
  53.  
  54. private static void insereMarca() {
  55.  
  56. String driver = "com.mysql.jdbc.Driver";
  57. String dbname = "telemoveis";
  58. String dbuser = "andre";
  59. String password = "andre";
  60. String url = "jdbc:mysql://localhost:3306/" + dbname + "?useSSL=false";
  61. try {
  62. Class.forName(driver);
  63. } catch (ClassNotFoundException e) {
  64. e.printStackTrace();
  65. System.err.println("Cannot load driver " + e.getMessage());
  66. System.exit(1);
  67. }
  68. try {
  69. BufferedReader in;
  70. in = new BufferedReader(new InputStreamReader(System.in));
  71. System.out.println("Insira uma nova marca");
  72. System.out.print(">> Nome: ");
  73. String nomeMarca = in.readLine();
  74. System.out.print(">> MoradaSede: ");
  75. String moradaSedeMarca = in.readLine();
  76. System.out.println("Nome: " + nomeMarca + " MoradaSede: " + moradaSedeMarca);
  77.  
  78. Connection c = DriverManager.getConnection(url, dbuser, password);
  79. c.setAutoCommit(false);
  80. Statement stmt = c.createStatement();
  81. String sql = "INSERT INTO Marca(nome,moradaSede) VALUES('"
  82. + nomeMarca + "','" + moradaSedeMarca + "');";
  83. stmt.executeUpdate(sql);
  84. stmt.close();
  85. c.commit();
  86. c.close();
  87. System.out.println("Marca " + nomeMarca + " inserida.");
  88. } catch (Exception exx) {
  89. exx.printStackTrace();
  90. }
  91. }
  92.  
  93. private static void telemoveisTiagoSousa() {
  94. String driver = "com.mysql.jdbc.Driver";
  95. String dbname = "telemoveis";
  96. String dbuser = "andre";
  97. String password = "andre";
  98. String url = "jdbc:mysql://localhost:3306/" + dbname + "?useSSL=false";
  99. try {
  100. Class.forName(driver);
  101. } catch (ClassNotFoundException e) {
  102. e.printStackTrace();
  103. System.err.println("Cannot load driver " + e.getMessage());
  104. System.exit(1);
  105. }
  106.  
  107. try {
  108. Connection c = DriverManager.getConnection(url, dbuser, password);
  109. c.setAutoCommit(false);
  110. Statement stmt = c.createStatement();
  111. String query = "SELECT modeloTelemovel, anoTelemovel "
  112. + "FROM TelemoveisClientes "
  113. + "WHERE nomeCliente='Tiago Sousa';";
  114. ResultSet rs = stmt.executeQuery(query);
  115. if (rs.next()) {
  116. String modeloT = rs.getString("modeloTelemovel");
  117. int anoT = rs.getInt("anoTelemovel");
  118.  
  119. System.out.println("Modelo: " + modeloT);
  120. System.out.println("Ano: " + anoT);
  121. } else {
  122. System.out.println("Tiago Sousa não tem nenhum telemóvel");
  123. }
  124.  
  125. rs.close();
  126. stmt.close();
  127. c.close();
  128. } catch (Exception e) {
  129. System.err.println(e.getClass().getName() + ": " + e.getMessage());
  130. System.exit(0);
  131. }
  132. }
  133. public class insereMarcaOnePlus {
  134. public static void main(String args[]) {
  135. String driver = "com.mysql.jdbc.Driver";
  136. String dbname = "telemoveis";
  137. String dbuser = "andre";
  138. String password = "andre";
  139. String url = "jdbc:mysql://localhost:3306/" + dbname + "?useSSL=false";
  140. try {
  141. Class.forName(driver);
  142. }
  143. catch (ClassNotFoundException e) {
  144. e.printStackTrace();
  145. System.err.println("Cannot load driver " + e.getMessage());
  146. System.exit(1);
  147. }
  148.  
  149. try {
  150. Connection c = DriverManager.getConnection(url, dbuser, password);
  151. c.setAutoCommit(false);
  152. Statement stmt = c.createStatement();
  153. String sql = "INSERT INTO marca(nome,moradaSede) VALUES('OnePlus', 'Shenzhen, China');";
  154. stmt.executeUpdate(sql);
  155. stmt.close();
  156. c.commit();
  157. c.close();
  158. System.out.println("Marca OnePlus inserida corretamente.");
  159. }
  160. catch (Exception e) {
  161. System.err.println( e.getClass().getName()+": "+ e.getMessage() );
  162. System.exit(0);
  163. }
  164. }
  165.  
  166. public class printiPhone7 {
  167. public static void main(String args[]) {
  168. String driver = "com.mysql.jdbc.Driver";
  169. String dbname = "telemoveis";
  170. String dbuser = "andre";
  171. String password = "andre";
  172. String url = "jdbc:mysql://localhost:3306/" + dbname + "?useSSL=false";
  173. try {
  174. Class.forName(driver);
  175. }
  176. catch (ClassNotFoundException e) {
  177. e.printStackTrace();
  178. System.err.println("Cannot load driver " + e.getMessage());
  179. System.exit(1);
  180. }
  181.  
  182. try {
  183. Connection c = DriverManager.getConnection(url, dbuser, password);
  184. c.setAutoCommit(false);
  185. Statement stmt = c.createStatement();
  186. String query = "SELECT modelo, ano, pvp, tamanho, nomeMarca, modeloTelOrig, anoTelOrig " +
  187. "FROM telemoveis " +
  188. "WHERE nome='iPhone 7';";
  189. ResultSet rs = stmt.executeQuery(query);
  190. if( rs.next() ) {
  191. String modeloF = rs.getString("modelo");
  192. int anoF = rs.getInt("ano");
  193. int pvpF = rs.getInt("pvp");
  194. int tamanhoF = rs.getBoolean("tamanho");
  195. String nomeM = rs.getString("nomeMarca");
  196. String modeloTelOrigF = rs.getString("modeloTelOrig");
  197. int anoTelOrigF = rs.getString("anoTelOrig");
  198.  
  199. System.out.println("Modelo: " + modeloF);
  200. System.out.println("Ano: " + anoF);
  201. System.out.println("PVP: " + pvpF);
  202. System.out.println("Tamanho: " + tamanhoF);
  203. System.out.println("Marca: " + nomeM);
  204. System.out.println("ModeloOriginal: " + modeloTelOrigF);
  205. System.out.println("AnoModeloOriginal: " + anoTelOrigF);
  206. }
  207. else
  208. System.out.println("iPhone 7 não foi encontrado na base de dados");
  209.  
  210. rs.close();
  211. stmt.close();
  212. c.close();
  213. }
  214. catch (Exception e) {
  215. System.err.println( e.getClass().getName()+": "+ e.getMessage() );
  216. System.exit(0);
  217. }
  218. }
  219.  
  220.  
  221.  
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement