Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.07 KB | None | 0 0
  1. package de.fh_lu.o4s.beans;
  2.  
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7.  
  8. import de.fh_lu.o4s.jdbc.DB2Access;
  9. import de.fh_lu.o4s.jdbc.JDBCAccess;
  10.  
  11. public class UserBean {
  12. String userid;
  13. String username;
  14. String password;
  15. String email;
  16. String active;
  17. String admin;
  18. JDBCAccess jdbcAcc;
  19.  
  20. public UserBean() {
  21. super();
  22. jdbcAcc = new DB2Access();
  23. userid = "";
  24. username = "";
  25. password = "";
  26. email = "";
  27. }
  28.  
  29. public int checkUseridPassword(){
  30. // 1 - User existiert nicht
  31. // 0 - User existiert, alles ok
  32. // 98 - keine Connection
  33. // 99 - sonstiger Fehler
  34.  
  35. String sql = "SELECT userid, password FROM user" +
  36. "WHERE userid = ? AND password = ?";
  37. System.out.println(sql);
  38.  
  39.  
  40. java.sql.Connection dbConn = jdbcAcc.getConnection();
  41. if (dbConn == null){
  42. return 98;
  43. }else{
  44. try {
  45. PreparedStatement prepStat = dbConn.prepareStatement(sql);
  46. prepStat.setString(1, this.userid);
  47. prepStat.setString(2, this.password);
  48. ResultSet dbRes = prepStat.executeQuery();
  49. if(dbRes.next()){
  50. System.out.println("User & Pw gefunden");
  51. return 0;
  52. }else{
  53. System.out.println("user & Pw nicht gefunden");
  54. return 1;
  55. // return (debRes.next())? 0 : 1;
  56. }
  57. } catch (SQLException e) {
  58. e.printStackTrace();
  59. int rc = e.getErrorCode();
  60. if (rc < 0) return rc;
  61. else return 99;
  62. }
  63. }
  64.  
  65. }
  66.  
  67. public int readComparePassword(){
  68. // 1 - User existiert nicht
  69. // 0 - User existiert, alles ok
  70. // 98 - keine Connection
  71. // 99 - sonstiger Fehler
  72.  
  73. String sql = "SELECT userid, password FROM user" +
  74. "WHERE userid = ? AND password = ?";
  75. System.out.println(sql);
  76.  
  77.  
  78. java.sql.Connection dbConn = jdbcAcc.getConnection();
  79. if (dbConn == null){
  80. return 98;
  81. }else{
  82. try {
  83. PreparedStatement prepStat = dbConn.prepareStatement(sql);
  84. prepStat.setString(1, this.userid);
  85. prepStat.setString(2, this.password);
  86. ResultSet dbRes = prepStat.executeQuery();
  87. if(dbRes.next()){
  88. System.out.println("User gefunden");
  89. // getString(2) = hol den String aus dem 2. Feld!
  90. if (this.password.equals(dbRes.getString(2).trim())) {
  91. return 0;
  92. }else{
  93. return 1;
  94. }
  95. }else{
  96. System.out.println("user nicht gefunden");
  97. return 1;
  98. // return (debRes.next())? 0 : 1;
  99. }
  100. } catch (SQLException e) {
  101. e.printStackTrace();
  102. int rc = e.getErrorCode();
  103. if (rc < 0) return rc;
  104. else return 99;
  105. }
  106. }
  107.  
  108. }
  109.  
  110.  
  111. public int checkUserExists(){
  112. return this.checkUserExists1();
  113. }
  114. public int checkUserExists1(){
  115. // 0 - User existiert nicht
  116. // 1 - User existiert
  117. // 98 - keine Connection
  118. // 99 - sonstiger Fehler
  119. // <SQL-Code> - SQL-Fehler < 0
  120.  
  121. // mit SELECT * ... WHERE
  122.  
  123. String tabelle = "User";
  124. String stringwert = this.getUserid();
  125. String spalte ="userid";
  126.  
  127. String sql = "SELECT * FROM " + tabelle+ " WHERE " + spalte +" = '"+ stringwert+"'";
  128. System.out.println(sql);
  129.  
  130.  
  131. java.sql.Connection dbConn = jdbcAcc.getConnection();
  132. if (dbConn == null){
  133. return 98;
  134. }else{
  135. try {
  136. Statement myStat = dbConn.createStatement();
  137. myStat.execute(sql);
  138. ResultSet x = myStat.getResultSet();
  139. if (x.equals(null)){
  140. System.out.println("User existiert nicht");
  141. return 0;
  142. }else{ System.out.println("User existiert");
  143. return 1;
  144. }
  145. } catch (SQLException e) {
  146. e.printStackTrace();
  147. int rc = e.getErrorCode();
  148. if (rc < 0) return rc;
  149. else return 99;
  150. }
  151. }
  152. }
  153. public int checkUserExists2(){
  154. // 0 - User existiert nicht
  155. // 1 - User existiert
  156. // 98 - keine Connection
  157. // 99 - sonstiger Fehler
  158. // <SQL-Code> - SQL-Fehler < 0
  159.  
  160. // mit SELECT COUNT(*) ... WHERE
  161. String tabelle = "User";
  162. String stringwert = this.getUserid();
  163. String spalte ="userid";
  164.  
  165. String sql = "SELECT * FROM " + tabelle+ " WHERE " + spalte +" = '"+ stringwert+"'";
  166. System.out.println(sql);
  167.  
  168.  
  169. java.sql.Connection dbConn = jdbcAcc.getConnection();
  170. if (dbConn == null){
  171. return 98;
  172. }else{
  173. try {
  174. Statement myStat = dbConn.createStatement();
  175. myStat.execute(sql);
  176. ResultSet x = myStat.getResultSet();
  177. if (x.equals(null)){
  178. System.out.println("User existiert nicht");
  179. return 0;
  180. }else{ System.out.println("User existiert");
  181. return 1;
  182. }
  183. } catch (SQLException e) {
  184. e.printStackTrace();
  185. int rc = e.getErrorCode();
  186. if (rc < 0) return rc;
  187. else return 99;
  188. }
  189.  
  190. }
  191. }
  192. public int checkUserExists3(){
  193. // 0 - User existiert nicht
  194. // 1 - User existiert
  195. // 98 - keine Connection
  196. // 99 - sonstiger Fehler
  197. // <SQL-Code> - SQL-Fehler < 0
  198.  
  199. // SELECT * ... ohne WHERE
  200. // mit ResultSet durchsuchen
  201.  
  202. String tabelle = "User";
  203. String stringwert = this.getUserid();
  204. String spalte ="userid";
  205.  
  206. String sql = "SELECT * FROM " + tabelle+ "'";
  207. System.out.println(sql);
  208.  
  209. java.sql.Connection dbConn = jdbcAcc.getConnection();
  210. if (dbConn == null){
  211. return 98;
  212. }else{
  213. try {
  214. Statement myStat = dbConn.createStatement();
  215. myStat.execute(sql);
  216. ResultSet x = myStat.getResultSet();
  217. if (x.equals(null)){
  218. System.out.println("User existiert nicht");
  219. return 0;
  220. }else{
  221. // resultset durchlaufen
  222. while (x.next()) {
  223. // user suchen
  224. if (x.getArray(spalte).equals(stringwert)) {
  225. System.out.println("User existiert:" );
  226. return 0;
  227. }
  228.  
  229. }}}
  230.  
  231.  
  232. catch (SQLException e) {
  233. e.printStackTrace();
  234. int rc = e.getErrorCode();
  235. if (rc < 0) return rc;
  236. else return 99;
  237. }}
  238. return 99;
  239. }
  240.  
  241. public int insertuserifnew(){
  242. int rc = this.checkUserExists1();
  243. if (rc != 0) return rc;
  244. return this.insertUserNoCheck();
  245. }
  246.  
  247. public int getNumberOfUsers(){
  248. // >=0 - Befehl ok, Anzahl der User
  249. // <0 - SQL-Code
  250. // -999998 - keine Connection
  251. // -999999 - sonstiger Fehler
  252.  
  253. String sql = "SELECT COUNT(*) FROM USER";
  254. System.out.println(sql);
  255.  
  256. java.sql.Connection dbConn = jdbcAcc.getConnection();
  257. if (dbConn == null){
  258. return -999998;
  259. }else{
  260. try {
  261. ResultSet dbRes = dbConn.createStatement().executeQuery(sql);
  262. if (dbRes.next()){
  263. int num = dbRes.getInt(1);
  264. return num;
  265. }else{
  266. return -999999;
  267. }
  268. } catch (SQLException e) {
  269. e.printStackTrace();
  270. int rc = e.getErrorCode();
  271. if (rc < 0) return rc;
  272. else return -999999;
  273. }
  274. }
  275. }
  276. public void prepareAttributesForDB(){
  277. if (this.userid.length() > 16) userid = userid.substring(0,16);
  278. if (this.username.length() > 256) username = username.substring(0,256);
  279. if (this.email.length() > 256) email = email.substring(0,256);
  280. if (this.password.length() > 32) password = password.substring(0,32);
  281.  
  282. if (admin == null) admin = "N";
  283. if (active == null) active = "N";
  284.  
  285. if (admin.equals("Y")
  286. || admin.equalsIgnoreCase("yes")
  287. || admin.equalsIgnoreCase("j")
  288. || admin.equalsIgnoreCase("ja")) admin = "Y";
  289. else admin = "N";
  290. if (active.equals("Y")
  291. || active.equalsIgnoreCase("yes")
  292. || active.equalsIgnoreCase("j")
  293. || active.equalsIgnoreCase("ja")) active = "Y";
  294. else active = "N";
  295. }
  296. public int updateUserNoCheck(){
  297. // 0 - alles ok
  298. // 98 - keine Connection
  299. // 99 - sonstiger Fehler
  300. // <SQL-Code> - SQL-Fehler < 0
  301. this.prepareAttributesForDB();
  302.  
  303. String sql = "UPDATE USER " +
  304. "SET (username, email, password, " +
  305. "active, admin) " +
  306. "= (?, ?, ?, ?, ?) " +
  307. "WHERE userid = ?";
  308. System.out.println(sql);
  309.  
  310. java.sql.Connection dbConn = jdbcAcc.getConnection();
  311. if (dbConn == null){
  312. return 98;
  313. }else{
  314. try {
  315. // Statement myStat = dbConn.createStatement();
  316. PreparedStatement myStat = dbConn.prepareStatement(sql);
  317.  
  318. myStat.setString(1, this.getUsername());
  319. myStat.setString(2, this.getEmail());
  320. myStat.setString(3, this.getPassword());
  321. myStat.setString(4, this.getActive());
  322. myStat.setString(5, this.getAdmin());
  323. myStat.setString(6, this.getUserid());
  324.  
  325. myStat.executeUpdate();
  326.  
  327. System.out.println("Update-Befehl erfolgreich");
  328. return 0;
  329. } catch (SQLException e) {
  330. e.printStackTrace();
  331. int rc = e.getErrorCode();
  332. if (rc < 0) return rc;
  333. else return 99;
  334. }
  335. }
  336. }
  337. public int updateUserIfExists(){
  338. // 0 - alles ok, user wurde geupdated
  339. // 1 - User Existiert noch nicht, nichts gemacht
  340. // 98 - keine Connection
  341. // 99 - sonstiger Fehler
  342. // <SQL-Code> - SQL-Fehler < 0
  343. int ret = this.checkUserExists();
  344. if (ret == 0)return 1;
  345. if (ret != 1)return ret;
  346. // nutzer existiert, weil checkuserExists() ==1;
  347. String tabelle = "User";
  348. String stringwert = this.getUserid();
  349. String spalte ="userid";
  350.  
  351. String sql = "SELECT USERNAME, PASSWORD, EMAIL, ACTIVE,ADMIN FROM USER WHERE userid = ?";
  352. System.out.println(sql);
  353. java.sql.Connection dbConn = jdbcAcc.getConnection();
  354.  
  355. if (dbConn == null){
  356. return 98;
  357. }else{
  358. try {
  359. // Statement myStat = dbConn.createStatement();
  360. PreparedStatement prepStat = dbConn.prepareStatement(sql);
  361. prepStat.setString(1, this.getUserid());
  362. ResultSet dbRes = prepStat.executeQuery();
  363. if (!dbRes.next())return 99;
  364. // jetzt steht pointer auf einzigem richtigen datensatz
  365. this.prepareAttributesForDB();
  366.  
  367. dbRes.updateString(username, username);
  368. dbRes.updateString(password, password);
  369. dbRes.updateString(email, email);
  370. dbRes.updateString(active, active);
  371. dbRes.updateString(admin, admin);
  372.  
  373. } catch (SQLException e) {
  374. e.printStackTrace();
  375. int rc = e.getErrorCode();
  376. if (rc < 0) return rc;
  377. else return 99;
  378. }
  379. }
  380. return 99;
  381. }
  382.  
  383.  
  384.  
  385.  
  386. public int deleteUserNoCheck(String string){
  387. // 0 - alles ok
  388. // 98 - keine Connection
  389. // 99 - sonstiger Fehler
  390. // <SQL-Code> - SQL-Fehler < 0
  391. String sql = "DELETE FROM USER " +
  392. "WHERE userid = '" + this.getUserid() + "'";
  393. System.out.println(sql);
  394. java.sql.Connection dbConn = jdbcAcc.getConnection();
  395. if (dbConn == null){
  396. return 98;
  397. }else{
  398. try {
  399. dbConn.createStatement().executeUpdate(sql);
  400. System.out.println("Delete-Befehl erfolgreich");
  401. return 0;
  402. } catch (SQLException e) {
  403. e.printStackTrace();
  404. int rc = e.getErrorCode();
  405. if (rc < 0) return rc;
  406. else return 99;
  407. }
  408. }
  409. }
  410. public int insertUserNoCheck(){
  411. // 0 - alles ok
  412. // 98 - keine Connection
  413. // 99 - sonstiger Fehler
  414. // <SQL-Code> - SQL-Fehler < 0
  415. this.prepareAttributesForDB();
  416. String sql = "INSERT INTO USER " +
  417. "(userid, username, email, password, active, admin) " +
  418. "VALUES ('" +
  419. this.getUserid() + "', '" +
  420. this.getUsername() + "', '" +
  421. this.getEmail() + "', '" +
  422. this.getPassword() + "', '" +
  423. this.getActive() + "', '" +
  424. this.getAdmin() + "')";
  425. System.out.println(sql);
  426. java.sql.Connection dbConn = jdbcAcc.getConnection();
  427. if (dbConn == null){
  428. return 98;
  429. }else{
  430. try {
  431. dbConn.createStatement().executeUpdate(sql);
  432. System.out.println("Insert-Befehl erfolgreich");
  433. return 0;
  434. } catch (SQLException e) {
  435. e.printStackTrace();
  436. int rc = e.getErrorCode();
  437. if (rc < 0) return rc;
  438. else return 99;
  439. }
  440. }
  441. }
  442.  
  443. public String getUserid() {
  444. return userid;
  445. }
  446. public void setUserid(String userid) {
  447. this.userid = userid;
  448. }
  449. public String getUsername() {
  450. return username;
  451. }
  452. public void setUsername(String username) {
  453. this.username = username;
  454. }
  455. public String getPassword() {
  456. return password;
  457. }
  458. public void setPassword(String password) {
  459. this.password = password;
  460. }
  461. public String getEmail() {
  462. return email;
  463. }
  464. public void setEmail(String email) {
  465. this.email = email;
  466. }
  467. public String getActive() {
  468. return active;
  469. }
  470. public void setActive(String active) {
  471. this.active = active;
  472. }
  473. public String getAdmin() {
  474. return admin;
  475. }
  476. public void setAdmin(String admin) {
  477. this.admin = admin;
  478. }
  479. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement