Hi im learning java and after learning about java spring and spring boot i kinda forgot how to proporley connect java to sql on native java.
Im working on an assigment where we were givin a connection pool class which we use to connect to our database,but something isnt working for me
Here is the connection pool code (relevent part)
public class ConnectionPool { private static ConnectionPool instance = new ConnectionPool(); private ArrayList<Connection> connections = new ArrayList<>(); private static final int MAX_CONNECTIONS = 5; private ConnectionPool() { try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); for (int i = 0; i < MAX_CONNECTIONS; i++) { try { Connection c = DriverManager.getConnection( "jdbc:sqlserver://localhost;databaseName=Coupon data;integratedSecurity=true"); connections.add(c); } catch (SQLException e) { } } } catch (ClassNotFoundException e1) { System.out.println(e1.getMessage()); } }
and here is my classDBDAO for example
public class CompanyDBDAO implements CompanyDAO { private ConnectionPool pool = ConnectionPool.getInstance(); @Override public void addCompany(Company company) throws SQLException { Connection con = pool.getConnection(); try { PreparedStatement st = con.prepareStatement("insert into companies values(?, ?, ?)"); st.setString(1, company.getName()); st.setString(2, company.getEmail()); st.setString(3, company.getPassword()); st.execute(); } catch (SQLException e) { throw new SQLException(e.getMessage()); } finally { pool.returnConnection(con); } }
and my main
When i try to run my main prog all i get in my console ispublic static void main(String[] args) { CompanyDBDAO comdb = new CompanyDBDAO(); try { comdb.addCompany(new Company("Company1", "blah1@gmail", "1")); comdb.addCompany(new Company("Company2", "blah2@gmail", "12")); comdb.addCompany(new Company("Company3", "blah3@gmail", "123")); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
"com.microsoft.sqlserver.jdbc.SQLServerDriver"
and no change to my databse.What am i forgetting?