Advertisement
Guest User

Untitled

a guest
Sep 26th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. package com.e2open.dbprogram;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6.  
  7. public class db
  8. {
  9. private String classname,constring;
  10. protected Connection con;
  11. public db()
  12. {
  13. classname="com.mysql.jdbc.Driver";
  14. constring="jdbc:mysql://localhost/e2opendb?user=root&password=&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
  15. }
  16. public void init()
  17. {
  18. try
  19. {
  20. Class.forName(classname);
  21. }
  22. catch(ClassNotFoundException e) {e.printStackTrace();}
  23. }
  24. public void opencon()
  25. {
  26. try
  27. {
  28. con=DriverManager.getConnection(constring);
  29. }
  30. catch(SQLException e) {e.printStackTrace();}
  31. }
  32. public void closecon()
  33. {
  34. try
  35. {
  36. con.close();
  37. }
  38. catch(SQLException e) {e.printStackTrace();}
  39. }
  40. }
  41.  
  42. package com.e2open.dbprogram;
  43.  
  44. public class executer
  45. {
  46. public static void main(String[] args)
  47. {
  48. user u=new user();
  49. u.init();
  50. u.opencon();
  51. if(u.validate("selva", "password"))
  52. System.out.println("validated");
  53. else
  54. System.out.println("not validated");
  55. }
  56. }
  57.  
  58. package com.e2open.dbprogram;
  59.  
  60. import java.sql.PreparedStatement;
  61. import java.sql.ResultSet;
  62. import java.sql.SQLException;
  63.  
  64. public class user extends db
  65. {
  66. String sql;
  67. ResultSet rs;
  68. PreparedStatement ps;
  69. public user()
  70. {
  71. sql=null;
  72. rs=null;
  73. ps=null;
  74. }
  75. public boolean validate(String username,String password)
  76. {
  77. try
  78. {
  79. sql="select fullname from users where username=? and password=?";
  80. ps=con.prepareStatement(sql);
  81. ps.setString(1, username);
  82. ps.setString(2,password);
  83. rs=ps.executeQuery();
  84. if(rs.next())
  85. return true;
  86. else
  87. return false;
  88. }
  89. catch(SQLException e)
  90. {e.printStackTrace();return false;}
  91. finally
  92. {
  93. try {
  94. con.close();
  95. } catch (SQLException e) {
  96. // TODO Auto-generated catch block
  97. e.printStackTrace();
  98. }
  99. }
  100. }
  101.  
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement