Guest User

DAO

a guest
Feb 2nd, 2016
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. package database;
  2.  
  3. import java.sql.*;
  4. import java.util.ArrayList;
  5. import java.util.regex.Matcher;
  6. import java.util.regex.Pattern;
  7.  
  8. import org.apache.commons.fileupload.FileItem;
  9.  
  10. import bean.CreditCard;
  11.  
  12. public class DAO {
  13. Connection con;
  14. public static String url = "jdbc:mysql://localhost/it3191db?autoReconnect=true&relaxAutoCommit=true";
  15. public static String dbdriver = "com.mysql.jdbc.Driver";
  16. public static String username = "root";
  17. public static String password = "";
  18.  
  19. public DAO() throws Exception {
  20. try {
  21. Class.forName(dbdriver);
  22. con = DriverManager.getConnection(url, username, password);// Getting
  23. // connection
  24. // to
  25. // database
  26. con.setAutoCommit(true);
  27. } catch (Exception ex) {
  28. System.out.println("Exception in DBAO " + ex);
  29. throw new Exception("Couldn't open connection to database: "
  30. + ex.getMessage());
  31. }
  32. }
  33.  
  34.  
  35. public int createCreditCard(String cardNo){
  36. int status = 0;
  37.  
  38. //SQL statement
  39. String sql = "INSERT INTO it3191db.creditcard (cardNo) VALUES (?)";
  40.  
  41. try {
  42.  
  43. // Use the connection to create a PreparedStatement object
  44. PreparedStatement ps = con.prepareStatement(sql);
  45. ps.setString(1, cardNo);
  46. status = ps.executeUpdate();
  47. con.commit();
  48. con.close();
  49.  
  50. } catch (Exception e) {
  51. System.out.println("SQL Error= " + e.getMessage());
  52. }
  53. return status;
  54. }
  55. }
Add Comment
Please, Sign In to add comment