Guest User

Untitled

a guest
Dec 13th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. package databaseconnectionapi;
  2.  
  3. import com.mysql.jdbc.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.SQLException;
  7.  
  8. public class DatabaseConnectionAPI {
  9.  
  10.  
  11. String username = "root";
  12. String password = "";
  13. Connection conn = null;
  14. String classForName = "com.mysql.jdbc.Driver";
  15.  
  16.  
  17. public Connection databaseConnection(String dbName) {
  18.  
  19. try {
  20. String driver = "jdbc:mysql://localhost:3306/"+dbName;
  21. Class.forName(classForName);
  22. conn = (Connection) DriverManager.getConnection(driver, username, password);
  23. System.out.println("Connected");
  24.  
  25. } catch (ClassNotFoundException ex) {
  26. System.out.println(ex);
  27. } catch (SQLException SQLex) {
  28. System.out.println(SQLex);
  29. } catch (Exception SQLexc) {
  30. System.out.println(SQLexc);
  31. }
  32. return conn ;
  33. }
  34.  
  35. public PreparedStatement executeQuery(String dbsName, String SQLQuery) {
  36. Connection connPst = databaseConnection(dbsName);
  37. PreparedStatement pst = null;
  38. try {
  39.  
  40. pst = connPst.prepareStatement(SQLQuery);
  41. pst.execute();
  42. System.out.println("Executed Successfully");
  43. } catch (SQLException sQLException) {
  44. System.out.println(sQLException);
  45. }
  46. return pst;
  47. }
  48.  
  49. public static void main(String[] args) {
  50. DatabaseConnectionAPI c = new DatabaseConnectionAPI();
  51. c.databaseConnection("amazon");
  52.  
  53.  
  54. }
  55.  
  56. }
Add Comment
Please, Sign In to add comment