Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: problem connecting sql server to java

  1. #1
    Junior Member
    Join Date
    Jun 2019
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default problem connecting sql server to java

    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

    public 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();
    		}
    	}
    When i try to run my main prog all i get in my console is
    "com.microsoft.sqlserver.jdbc.SQLServerDriver"
    and no change to my databse.What am i forgetting?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: problem connecting sql server to java

    I see an empty catch block for an exception. Is it missing reporting an exception?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2019
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problem connecting sql server to java

    If its the one in the connection pool class ive edited,still no progress

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: problem connecting sql server to java

    all i get in my console is
    "com.microsoft.sqlserver.jdbc.SQLServerDriver"
    Where is the print statement that prints that on the console?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jun 2019
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problem connecting sql server to java

    Quote Originally Posted by Norm View Post
    Where is the print statement that prints that on the console?

    I dont have a print statement the prints that

    i kinda forgot to add the extrenal build path..i just did that and i ge tthis messge

    The TCP/IP connection to the host localhost, port 1433 has failed. Error: "Connection refused: connect. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".

    My sql server is running,and i never blocked it,ive used my sql server before with no errors
    Last edited by mike512; September 8th, 2019 at 11:31 AM.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: problem connecting sql server to java

    I dont have a print statement the prints that
    What does this print?
    		} catch (ClassNotFoundException e1) {
    			System.out.println(e1.getMessage());
    		}

    A clearer message might be shown from this:
    System.out.println("CNFE " + e1.getMessage());
    Then you would know where it came from.

    i ge t this messge
    From where?

    I think The message says it can't find a server.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jun 2019
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problem connecting sql server to java

    Quote Originally Posted by Norm View Post
    What does this print?
    		} catch (ClassNotFoundException e1) {
    			System.out.println(e1.getMessage());
    		}

    A clearer message might be shown from this:
    System.out.println("CNFE " + e1.getMessage());
    Then you would know where it came from.


    From where?

    I think The message says it can't find a server.
    Error was solved via
    https://kb.sos-berlin.com/pages/view...ageId=17499564

    Thanks for the help!

Similar Threads

  1. Java 12.0.2 for MS SQL Server 2012
    By amdulala in forum JDBC & Databases
    Replies: 0
    Last Post: September 5th, 2019, 12:44 AM
  2. Connecting Java to SQL
    By moskiongo in forum JDBC & Databases
    Replies: 3
    Last Post: September 10th, 2012, 11:29 AM
  3. Replies: 1
    Last Post: December 15th, 2011, 08:29 AM
  4. java application connecting to a server
    By wildheart25c in forum Java Networking
    Replies: 2
    Last Post: September 17th, 2009, 07:22 AM