'Connecting JSP to MySql in XAMPP
I have my sample code here :
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%
/* Create string of connection url within specified format with machine name, port number and database name. Here machine name id localhost and database name is usermaster. */
String connectionURL = "jdbc:mysql://localhost/ekoh";
// declare a connection by using Connection interface
Connection connection = null;
// Load JBBC driver "com.mysql.jdbc.Driver"
Class.forName("com.mysql.jdbc.Driver").newInstance();
/* Create a connection by using getConnection() method that takes parameters of string type connection url, user name and password to connect to database. */
connection = DriverManager.getConnection(connectionURL, "root", "");
// check weather connection is established or not by isClosed() method
if(!connection.isClosed())
%>
<font size="+3" color="green"></b>
<%
out.println("Successfully connected to " + "MySQL server using TCP/IP...");
connection.close();
}
catch(Exception ex){
%>
</font>
<font size="+3" color="red"></b>
<%
out.println("Unable to connect to database.");
}
%>
my db name is "ekoh", I used root account with no password.. Still no clue for me why it is still not working till now.. Can you give me some alternative code to run for? :) n.b. I code JSP with tomcat and MySql in XAMPP.
Solution 1:[1]
What one needs to do is download the MySQL connector jar file and then put it inside the folder path /WEB-INF/lib in your project folder.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jeremy Caney |
