CodeRo
CodeRo

Reputation: 97

How to connect to mysql using eclipse running apache

I am creating a dynamic web project in eclipse. I have my local apache server running and configured with appropriate resource, mysql running and configured with the appropriate port.

I downloaded the appropriate driver, included it in the lib directory - I even tried adding it as an external JAR file to no avail. On the dynamic web page the result is "error connecting to database".

I created a JSP with the following code:

<%@ page import="java.sql.*"%><%@ page import="java.io.*"%><%@ page import="com.mysql.*"%><?xml version="1.0"?>

<tours>
<%
 Connection connection = null;
 Statement statement = null;
 ResultSet result = null;

 try {
     Class.forName("com.mysql.jdbc.Driver").newInstance();
     connection = DriverManager.getConnection("jbdc:mysql://localhost:3306/tours", "root", "root");
     out.println("connected to database!");
 }
 catch(SQLException e) {
     out.print("error connecting to database");
 }
%>


</tours>

Please advise...

Thanks in advance.

Upvotes: 1

Views: 1125

Answers (4)

R D
R D

Reputation: 1332

Put print after Class.forName() check, if that statement is not printing then problem is driver if that line is printing then problem is mysql database name problem or credential problem

If problem in drive then download from here :

http://www.java2s.com/Code/Jar/c/Downloadcommysqljdbc515jar.htm

Problem is u put jbdc instead of jdbc

Code like this

<%
 Connection connection = null;
 Statement statement = null;
 ResultSet result = null;

 try {
     Class.forName("com.mysql.jdbc.Driver").newInstance();
     out.println("Driver is available !");
   connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/tours",
    "root", "root");
     out.println("connected to database!");
 }
 catch(SQLException e) {
     out.print("error connecting to database");
 }
%>

Upvotes: 1

Chittibabu
Chittibabu

Reputation: 1

Printing the stack trace might help it could be because of authorization issue. If the error is related to authorization issue, you may have to change the server configurations.

Upvotes: 0

Santosh Domakonda
Santosh Domakonda

Reputation: 162

Please check weather your database is started or not i know it should be in comment but i cannot keep it in comment because i don't have sufficient reputations. please try it if it helps you it is fine.

How to start database is shown below:

Click Windows button+r then enter Services.msc in search bar then select Mysql and click start option

Upvotes: 0

Darshan Lila
Darshan Lila

Reputation: 5868

There are couple of things to take care of here. 1. Make sure that mysql is listening on port 3306. 2. Cross check the database name.

Lastly get the driver jar file in deployment assembly.

If the problem still persists, please print the stacktrace and post it here.

Upvotes: 0

Related Questions