Connecting MySQL from JSP [duplicate]
Asked Answered
N

4

6

I just set foot on JSP. I started writing simple programs to display dates, system info. Then I tried to connect a MySQL database I have a free hosting account, but I am not able to connect to MySQL database. Here is my code:

<%@ page import="java.sql.*" %> 
<%@ page import="java.io.*" %> 
<html> 
<head> 
<title>Connection with mysql database</title>
</head> 
<body>
<h1>Connection status</h1>
<% 
try {
    String connectionURL = "jdbc:mysql://mysql2.000webhost.com/a3932573_product";
    Connection connection = null; 
    Class.forName("com.mysql.jdbc.Driver").newInstance(); 
    connection = DriverManager.getConnection(connectionURL, "a3932573_dibya", "******");
    if(!connection.isClosed())
         out.println("Successfully connected to " + "MySQL server using TCP/IP...");
    connection.close();
}catch(Exception ex){
    out.println("Unable to connect to database.");
}
%>
</font>
</body> 
</html>

I am getting Message as Connection Status unable to connect to database. I have tested this connection using PHP using the same username, password and database name. Where am I making mistake?

Nightie answered 22/11, 2012 at 4:19 Comment(18)
Are your JSP server and PHP server on the same machine?Philoprogenitive
Please print this in catch and let me know ex.printStackTrace();Glacier
I am getting a single character before my Connection status.Nightie
Bet 10$ the driver isn't loadedPhiloprogenitive
I edited the question please check catch block and show the full exceptions.Glacier
add printStacktrace in the catch blockFrenetic
@AlexandreLavoie Which driver? As I am very new to JSP I am not able to get which driver should be loadedNightie
Start by doing what @Glacier said... and show us the result.Philoprogenitive
@sheldonCooper I have added the printStackTrace() in the catch block.Nightie
Are you getting the error stacktrace ?Frenetic
After adding printStackTrace() in catch statement there are no error messages. I am getting the same output with one character before my output.Nightie
Looks the driver is missing in your libraries please add it and try its should work fine ..Zennas
@ATR Please tell me which driver am I missing?Nightie
Add external jar mysql connector jar to the lib folder and to your class path of the project @DibyaZennas
I have added mysql connector jar before starting developing JSP.Nightie
okies.. first try with standalone app and check it should work fine use the same connection statements please and lets know know the stack trace ..Zennas
Why I got a down-vote? Pls leave a comment.Nightie
@Dibya I gave you an upvote to counter the down vote. I think your question is legitimate :)Organist
Z
9

Reason is the driver have not been loaded in the libraries, it does not get instantiated in the connection so the connection failed:

try {
            String connectionURL = "jdbc:mysql://host/db";
            Connection connection = null; 
            Class.forName("com.mysql.jdbc.Driver").newInstance(); 
            connection = DriverManager.getConnection(connectionURL, "username", "password");
            if(!connection.isClosed())
                 out.println("Successfully connected to " + "MySQL server using TCP/IP...");
            connection.close();
        }catch(Exception ex){
            out.println("Unable to connect to database"+ex);
        }   

Download Driver

Zennas answered 22/11, 2012 at 4:54 Comment(0)
F
5

I‘ve got the same problem. I'm pretty much sure what's wrong with your program: you haven't added .jar to web lib. Copy it and paste into WEB-INF/lib.

Sorry for not using the correct format for posting answers(I'm new here and this is my first anwser:( )

Fitly answered 18/9, 2014 at 16:0 Comment(0)
P
2

Download the right driver :

http://dev.mysql.com/downloads/connector/j/

And add the jar in the classpath of your project.

Philoprogenitive answered 22/11, 2012 at 4:58 Comment(0)
E
0

go to this location in your pc C:\Program Files\Apache Software Foundation\Tomcat 10.1\lib and paste .jar file and restart the server or your pc and that's it. The download link for it is in the above answers

that how mine got worked!

Eschatology answered 1/2, 2024 at 6:28 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.