1

I am newbie to java db coding...

This is my java code

Connection conn = null;
Statement stmt = null;
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Connecting to database...");
conn = DriverManager.getConnection("jdbc:mysql://localhost/EMP",USER,PASS);
System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql;

This is the exception I am getting :

ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/tiles].[default]] (http-localhost-127.0.0.1-8080-1) Servlet.service() for servlet default threw exception: java.net.ConnectException: Connection refused: connect

I am mySQL server also running and contains simple table named EMP. I am not interpreting the get connection URL comppletely. Can anyone explain it briefly?

Thanks.

4
  • "connection refused" = db's not running, tcp port is firewalled, socket is in the wrong place, blah blah blah. Commented Oct 29, 2014 at 14:17
  • Look at the server logfile to find out what is happend. Commented Oct 29, 2014 at 14:18
  • 4
    Are you trying to connect to an SQL Express server using a MySQL driver? Commented Oct 29, 2014 at 14:21
  • is it a MySQL server or a MS SQL server? Commented Oct 29, 2014 at 14:38

3 Answers 3

2

You are using the wrong driver for the database. Every database (MySQL, PostgreSQL, SQL Server, Oracle etc.) has a different driver which is what connects between JDBC and the particular database's communication protocol.

You need the Microsoft SQL Server driver. You can download and install it from here if you don't already have it: Microsoft down page for JDBC 4.0 driver.

And here are the instructions on how to add the jar to your classpath, and what URL to use. Note that in JDBC 4.0 and above, the forName is not necessary anymore.

Sign up to request clarification or add additional context in comments.

Comments

1

from what i can see and understand is that you're missing the port.

you'd have done it in this way:

conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/EMP",USER,PASS);

The url can be describe in this way

jdbc: which is your connector mysql:which is your current installed database //hostname: which the machine hosting the server portNumber: the port which allows the connection for the server /databaseName: which is your created database.

hope it was helpful

1 Comment

and as further explained by @RealSkeptic, try to verify your path and that your database has been correctly installed.
0

Download the Microsoft JDBC driver and use Class.forName(com.microsoft.sqlserver.jdbc.SQLServerDriver);

If the problem still persists enable TCP/IP network protocol, which is disabled by default, and set the TCP/IP port to 1433 which is the default port number.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.