Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package databaseconnectionapi;
- import com.mysql.jdbc.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- public class DatabaseConnectionAPI {
- String username = "root";
- String password = "";
- Connection conn = null;
- String classForName = "com.mysql.jdbc.Driver";
- public Connection databaseConnection(String dbName) {
- try {
- String driver = "jdbc:mysql://localhost:3306/"+dbName;
- Class.forName(classForName);
- conn = (Connection) DriverManager.getConnection(driver, username, password);
- System.out.println("Connected");
- } catch (ClassNotFoundException ex) {
- System.out.println(ex);
- } catch (SQLException SQLex) {
- System.out.println(SQLex);
- } catch (Exception SQLexc) {
- System.out.println(SQLexc);
- }
- return conn ;
- }
- public PreparedStatement executeQuery(String dbsName, String SQLQuery) {
- Connection connPst = databaseConnection(dbsName);
- PreparedStatement pst = null;
- try {
- pst = connPst.prepareStatement(SQLQuery);
- pst.execute();
- System.out.println("Executed Successfully");
- } catch (SQLException sQLException) {
- System.out.println(sQLException);
- }
- return pst;
- }
- public static void main(String[] args) {
- DatabaseConnectionAPI c = new DatabaseConnectionAPI();
- c.databaseConnection("amazon");
- }
- }
Add Comment
Please, Sign In to add comment