Advertisement
Guest User

Untitled

a guest
Nov 18th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.84 KB | None | 0 0
  1. package BaseClass;
  2.  
  3. import javax.swing.*;
  4. import java.sql.*;
  5. import java.util.ArrayList;
  6.  
  7. public class MySQL {
  8.  
  9. static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  10. private String DB_URL = "jdbc:mysql://35.189.228.42:3306/programacaodistribuida?useSSL=false";
  11. private String user = "wriken";
  12. private String password = "wriken123";
  13. private Connection con;
  14.  
  15. public MySQL() {
  16.  
  17. }
  18.  
  19. public MySQL(String DB_URL, String user, String password, Connection con) {
  20. this.DB_URL = DB_URL;
  21. this.user = user;
  22. this.password = password;
  23. this.con = con;
  24. }
  25.  
  26. public String getDB_URL() {
  27. return DB_URL;
  28. }
  29.  
  30. public void setDB_URL(String DB_URL) {
  31. this.DB_URL = DB_URL;
  32. }
  33.  
  34. public String getUser() {
  35. return user;
  36. }
  37.  
  38. public void setUser(String user) {
  39. this.user = user;
  40. }
  41.  
  42. public String getPassword() {
  43. return password;
  44. }
  45.  
  46. public void setPassword(String password) {
  47. this.password = password;
  48. }
  49.  
  50. public Connection getCon() {
  51. return con;
  52. }
  53.  
  54. public void setCon(Connection con) {
  55. this.con = con;
  56. }
  57.  
  58. public void conectDB() throws ClassNotFoundException {
  59.  
  60. Class.forName("com.mysql.jdbc.Driver");
  61. System.out.println("Connecting to database...");
  62.  
  63. try {
  64. Connection conn = DriverManager.getConnection(DB_URL, user, password);
  65. Statement stmt = conn.createStatement();
  66.  
  67.  
  68. String sql;
  69. //sql = "SELECT * FROM users_list WHERE ip = 1";
  70. sql = "SELECT id,username FROM users_list";
  71. ResultSet rs = stmt.executeQuery(sql);
  72. System.out.println("Conexção estabelecida com a Base de Dados!");
  73.  
  74.  
  75. while(rs.next()){
  76. int id = rs.getInt("id");
  77. String name = rs.getString("username");
  78.  
  79. //System.out.println("ID:" + id);
  80. //System.out.println("Nome: " + name);
  81. }
  82. } catch (SQLException e) {
  83. e.printStackTrace();
  84. }
  85. }
  86.  
  87. public String findUserPass(String userLogin, String passLogin){
  88.  
  89. String sql = "SELECT username,password FROM users_list " + "WHERE username = '"+userLogin+"'" + " AND password = '"+passLogin+"'";
  90.  
  91. try {
  92. Class.forName(JDBC_DRIVER);
  93. con = DriverManager.getConnection(DB_URL, user, password);
  94. PreparedStatement pst = con.prepareStatement(sql);
  95.  
  96. ResultSet rs = pst.executeQuery(sql);
  97.  
  98. while (rs.next()) {
  99. //Retrieve by column name
  100. String username = rs.getString("username");
  101. String password = rs.getString("password");
  102. String dadosBD = username+password;
  103.  
  104. return dadosBD;
  105. }
  106. rs.close(); pst.close(); con.close();
  107. } catch (SQLException | ClassNotFoundException e) {
  108. e.printStackTrace();
  109. }
  110. return "error";
  111. }
  112.  
  113. public void getUsersList() {
  114.  
  115. String sql = "SELECT * FROM users_list ORDER BY id";
  116.  
  117. try {
  118. Class.forName(JDBC_DRIVER);
  119. con = DriverManager.getConnection(DB_URL, user, password);
  120. PreparedStatement pst = con.prepareStatement(sql);
  121.  
  122. ResultSet rs = pst.executeQuery(sql);
  123.  
  124. while (rs.next()) {
  125. //Retrieve by column name
  126. String username = rs.getString("username");
  127. String password = rs.getString("password");
  128. String ip = rs.getString("ip");
  129. int autenticado = rs.getInt("autenticado");
  130. //Display values
  131. System.out.print("username: " + username);
  132. System.out.print(", password: " + password);
  133. System.out.print(", ip: " + ip);
  134. System.out.println(", autenticado: " + autenticado);
  135. }
  136. rs.close(); pst.close(); con.close();
  137. } catch (SQLException | ClassNotFoundException e) {
  138. e.printStackTrace();
  139. }
  140.  
  141. }
  142.  
  143. public ArrayList<String> getUsersLogged(){
  144. String sql = "SELECT * FROM users_list WHERE autenticado='1'";
  145. ArrayList<String> users = new ArrayList<>();
  146. try {
  147. Class.forName(JDBC_DRIVER);
  148. con = DriverManager.getConnection(DB_URL, user, password);
  149. PreparedStatement pst = con.prepareStatement(sql);
  150.  
  151. ResultSet rs = pst.executeQuery(sql);
  152.  
  153. while (rs.next()) {
  154. //Retrieve by column name
  155. String username = rs.getString("username");
  156. String password = rs.getString("password");
  157. String ip = rs.getString("ip");
  158. int autenticado = rs.getInt("autenticado");
  159. //Display values
  160. //System.out.print("username: " + username);
  161. //System.out.print(", password: " + password);
  162. //System.out.print(", ip: " + ip);
  163. //System.out.println(", autenticado: " + autenticado);
  164.  
  165. users.add(username);
  166. }
  167. rs.close(); pst.close(); con.close();
  168. } catch (SQLException | ClassNotFoundException e) {
  169. e.printStackTrace();
  170. }
  171. return users;
  172. }
  173.  
  174. public ArrayList<String> getUsers() {
  175.  
  176. String sql = "SELECT * FROM users_list ORDER BY id";
  177. ArrayList<String> users = new ArrayList<>();
  178. try {
  179. Class.forName(JDBC_DRIVER);
  180. con = DriverManager.getConnection(DB_URL, user, password);
  181. PreparedStatement pst = con.prepareStatement(sql);
  182.  
  183. ResultSet rs = pst.executeQuery(sql);
  184.  
  185. while (rs.next()) {
  186. //Retrieve by column name
  187. String username = rs.getString("username");
  188. String password = rs.getString("password");
  189. String ip = rs.getString("ip");
  190. int autenticado = rs.getInt("autenticado");
  191. //Display values
  192. System.out.print("username: " + username);
  193. System.out.print(", password: " + password);
  194. System.out.print(", ip: " + ip);
  195. System.out.println(", autenticado: " + autenticado);
  196.  
  197. users.add(username);
  198. }
  199. rs.close(); pst.close(); con.close();
  200. } catch (SQLException | ClassNotFoundException e) {
  201. e.printStackTrace();
  202. }
  203. return users;
  204. }
  205.  
  206. public String getIPbyUser(String user){
  207. String sql = "SELECT IP FROM users_list WHERE username='"+user+"'";
  208. String ip = new String();
  209. try {
  210. Class.forName(JDBC_DRIVER);
  211. con = DriverManager.getConnection(DB_URL, user, password);
  212. PreparedStatement pst = con.prepareStatement(sql);
  213.  
  214. ResultSet rs = pst.executeQuery(sql);
  215.  
  216. while (rs.next()) {
  217. //Retrieve by column name
  218. ip = rs.getString("ip");
  219. }
  220. rs.close(); pst.close(); con.close();
  221. } catch (SQLException | ClassNotFoundException e) {
  222. e.printStackTrace();
  223. }
  224. return ip;
  225. }
  226.  
  227. public void getFiles(){
  228. String sql = "SELECT * FROM files ORDER BY id";
  229.  
  230. try {
  231. Class.forName(JDBC_DRIVER);
  232. con = DriverManager.getConnection(DB_URL, user, password);
  233. PreparedStatement pst = con.prepareStatement(sql);
  234.  
  235. ResultSet rs = pst.executeQuery(sql);
  236.  
  237. while (rs.next()) {
  238. //Retrieve by column name
  239. int id = rs.getInt("id");
  240. String user = rs.getString("user");
  241. String filename = rs.getString("filename");
  242. String filesize = rs.getString("filesize");
  243. String filetype = rs.getString("filetype");
  244. String directoria = rs.getString("directoria");
  245. //Display values
  246. System.out.print("id: " + id);
  247. System.out.print(", user: " + user);
  248.  
  249. System.out.print(", filename: " + filename);
  250. System.out.print(", filesize: " + filesize);
  251. System.out.print(", filetype: " + filetype);
  252. System.out.println(", directoria: " + directoria);
  253. }
  254. rs.close(); pst.close(); con.close();
  255. } catch (SQLException | ClassNotFoundException e) {
  256. e.printStackTrace();
  257. }
  258. }
  259.  
  260. public ArrayList<FileAvailableInformation> getFilesInformation(){
  261. String sql = "SELECT * FROM files ORDER BY id";
  262. ArrayList<FileAvailableInformation> files = new ArrayList<>();
  263.  
  264. try {
  265. Class.forName(JDBC_DRIVER);
  266. con = DriverManager.getConnection(DB_URL, user, password);
  267. PreparedStatement pst = con.prepareStatement(sql);
  268.  
  269. ResultSet rs = pst.executeQuery(sql);
  270.  
  271. while (rs.next()) {
  272. //Retrieve by column name
  273. String user = rs.getString("user");
  274. String filename = rs.getString("filename");
  275. String filesize = rs.getString("filesize");
  276. String directoria = rs.getString("directoria");
  277.  
  278. files.add(new FileAvailableInformation(filename, Long.parseLong(filesize), directoria));
  279. }
  280. rs.close(); pst.close(); con.close();
  281. } catch (SQLException | ClassNotFoundException e) {
  282. e.printStackTrace();
  283. }
  284. return files;
  285. }
  286.  
  287. public void setUserAutenticado(String user){
  288. String sql = "UPDATE users_list SET autenticado='1' WHERE username ='"+user+"'";
  289.  
  290. try {
  291. Class.forName(JDBC_DRIVER);
  292.  
  293. con = DriverManager.getConnection(DB_URL, this.user, password);
  294. PreparedStatement pst = con.prepareStatement(sql);
  295.  
  296. pst.executeUpdate();
  297.  
  298. pst.close();
  299. con.close();
  300.  
  301.  
  302. } catch (ClassNotFoundException e) {
  303. e.printStackTrace();
  304. } catch (SQLException e) {
  305. e.printStackTrace();
  306. }
  307. }
  308.  
  309. public String getFilePath(String filename){
  310.  
  311. String sql = "SELECT * FROM files WHERE filename='"+filename+"'";
  312.  
  313. try {
  314. Class.forName(JDBC_DRIVER);
  315.  
  316. con = DriverManager.getConnection(DB_URL, user, password);
  317. PreparedStatement pst = con.prepareStatement(sql);
  318.  
  319. ResultSet rs = pst.executeQuery(sql);
  320.  
  321. while (rs.next()) {
  322.  
  323. return rs.getString("directoria");
  324. }
  325. rs.close();
  326. pst.close();
  327. con.close();
  328.  
  329.  
  330. } catch (ClassNotFoundException e) {
  331. e.printStackTrace();
  332. } catch (SQLException e) {
  333. e.printStackTrace();
  334. }
  335.  
  336. return "Nao existe esse ficheiro";
  337. }
  338.  
  339. public boolean newFile(String user, String filename, long filesize, String directoria){
  340.  
  341. String sql = "INSERT INTO files (id,user, filesize, directoria, filename) " +
  342. "VALUES ('"+1+"','"+user+"', '"+filesize+"','"+directoria+"', '"+filename+"')";
  343.  
  344. String search = "SELECT filename,filesize FROM files " + "WHERE filename = '"+filename+"'" + " AND filesize = '"+filesize+"'";
  345.  
  346. String namePlusSize = filename+filesize;
  347.  
  348. try {
  349.  
  350. Class.forName(JDBC_DRIVER);
  351. con = DriverManager.getConnection(DB_URL,this.user,this.password);
  352. PreparedStatement ps = con.prepareStatement(search);
  353.  
  354. ResultSet rs = ps.executeQuery(search);
  355.  
  356. while (rs.next()) {
  357. //Retrieve by column name
  358. String fileN = rs.getString("filename");
  359. String fileS = rs.getString("filesize");
  360. String dados = filename+filesize;
  361.  
  362. if (dados.equals(namePlusSize))
  363. return false;
  364. }
  365.  
  366. PreparedStatement pst = con.prepareStatement(sql);
  367. pst.executeUpdate();
  368.  
  369. pst.close(); con.close();
  370. } catch (SQLException | ClassNotFoundException e) {
  371. e.printStackTrace();
  372. }
  373. return true;
  374. }
  375.  
  376. public boolean deleteFile(String filename){
  377.  
  378. String sql = "DELETE FROM files " + "WHERE filename = '"+filename+"'";
  379.  
  380. try {
  381.  
  382. Class.forName(JDBC_DRIVER);
  383. con = DriverManager.getConnection(DB_URL,this.user,this.password);
  384.  
  385. PreparedStatement pst = con.prepareStatement(sql);
  386. pst.executeUpdate();
  387. pst.close(); con.close();
  388.  
  389. return true;
  390. } catch (SQLException | ClassNotFoundException e) {
  391. e.printStackTrace();
  392. }
  393. return false;
  394. }
  395.  
  396. public void newUser(String username, String password, String ip, int autenticado){
  397.  
  398. String sql = "INSERT INTO users_list(username,password,ip,autenticado) " +
  399. "VALUES('" + username + "','" + password + "','" + ip + "','" + autenticado + "')";
  400. try {
  401. Class.forName(JDBC_DRIVER);
  402. con = DriverManager.getConnection(DB_URL,user,this.password);
  403. PreparedStatement pst = con.prepareStatement(sql);
  404. pst.executeUpdate();
  405.  
  406. pst.close(); con.close();
  407. } catch (SQLException | ClassNotFoundException e) {
  408. e.printStackTrace();
  409. }
  410. }
  411.  
  412. public void deleteUserByUsername(String username){
  413. String sql = "DELETE FROM users_list " + "WHERE username = '"+username+"'";
  414. try {
  415. Class.forName(JDBC_DRIVER);
  416. con = DriverManager.getConnection(DB_URL,user,this.password);
  417. PreparedStatement pst = con.prepareStatement(sql);
  418. pst.executeUpdate();
  419.  
  420. pst.close(); con.close();
  421. } catch (SQLException | ClassNotFoundException e) {
  422. e.printStackTrace();
  423. }
  424. }
  425.  
  426. public boolean isUserAuthenticated(String username){
  427. String sql = "SELECT autenticado FROM users_list WHERE username='"+username+"'";
  428.  
  429. try {
  430. Class.forName(JDBC_DRIVER);
  431. con = DriverManager.getConnection(DB_URL, user, password);
  432.  
  433. PreparedStatement pst = con.prepareStatement(sql);
  434. ResultSet rs = pst.executeQuery(sql);
  435.  
  436. while (rs.next()) {
  437. int autenticado = rs.getInt("autenticado");
  438.  
  439. if(autenticado == 0){
  440. return false;
  441. }
  442. else
  443. return true;
  444. }
  445.  
  446. } catch (ClassNotFoundException | SQLException e) {
  447. e.printStackTrace();
  448. return false;
  449. }
  450. return false;
  451. }
  452. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement