Connecting JSP to MySql in XAMPP - mysql

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.

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.

Related

how to fix an error on " DriverManager.getConnection(connectionURL, UN, PW); "?

I'm new to MYSQL and JSP. I am trying check the connectivity with between mysql databse and jsp. When I run the project on server it throws an exception. I cann't find the error on code.
I have created oop database on mysql and UN, PW are also correct.
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%# page import="java.sql.*" %>
<%# page import="java.io.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Connection status </h1>
<%
/* 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 oop. */
String connectionURL = "jdbc:mysql://localhost:3306/oop";
// declare a connection by using Connection interface
Connection connection = null;
// Load JBBC driver "com.mysql.jdbc.Driver"
Class.forName("com.mysql.jdbc.Driver").getDeclaredConstructors();
/* 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", "raviya");
// check weather connection is established or not by isClosed() method
if(!connection.isClosed()){
%>
<font size="+3" color="green">
<%
out.println("Successfully connected to " + "MySQL server using TCP/IP...");
connection.close();
}
else{
%>
</font>
<font size="+3" color="red">
<%
out.println("Unable to connect to database.");
}
%>
</font>
</body>
</html>
Error:
enter image description here
If you are not import mysql connector jar file these type of error can occur. So you can download jar file related to the your version and you can paste it in your own project /WEB-INF/lib folder.

Inserting into mysql database using jsp throwing exception

I am trying to store information from a registration form page to the corresponding database but the code is throwing the exception as stated 'unable to connect to database' I am a beginner and having a hard time trying to figure out what's going wrong in this. Can somebody please help?
<%#page import="java.sql.*"%>
<html>
<head>
<meta charset="utf-8">
<title>Sign Up</title>
<link rel="stylesheet" type="text/css" href="reg.css">
</head>
<body>
<%
String name = request.getParameter("name");
String email = request.getParameter("email");
String password = request.getParameter("password");
String date = request.getParameter("date");
String sex = request.getParameter("sex");
Connection con = null;
PreparedStatement ps = null;
String connectionURL = "jdbc:mysql://localhost:3306/table";
String driverName = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "";
Class.forName(driverName).newInstance();
try {
con = DriverManager.getConnection(connectionURL, user, pass);
String queryString = "INSERT INTO detail(name,password,email,date,sex) VALUES (?,?,?,?,?)";
ps = con.prepareStatement(queryString);
ps.setString(1, name);
ps.setString(2, password);
ps.setString(3, email);
ps.setString(4, date);
ps.setString(5, sex);
int updateQuery = ps.executeUpdate();
if (updateQuery != 0) {
out.println("Successful Registration");
}
}
catch (Exception ex) {
out.println("Unable to connect to database.");
}
finally {
// close all the connections.
ps.close();
con.close();
}
%>
</body>
</html>
Have you included driver jar file in your project ?
At String connectionURL = "jdbc:mysql://localhost:3306/table"; is table is your database name which you want to connect?
At
catch (Exception ex) {
out.println("Unable to connect to database.");
}
include ex.printStackTrace() which will show you exact error.
Well... "unable to connect to database" means exactly that. Your JSP could not connect to the database.
Please add the full stack trace (error) in your question. That way it will be clearer what the problem is.
Off the top of my head, the possible culprits are:
You don't have the driver loaded in your JEE container. Did you add the database driver?
Your driver is not properly configured. Did you specify the correct "driver class" when adding the driver?
Your URL is wrong. Please check the value of the URL. Is it well-formed? Follow the exact format as the example one and insert your specific values.
There's no database actually running at that HOST:PORT. Was the database created?
Your credentials are wrong, disabled, or don't even exist yet. Check your username and password with your DBA. Do you have the right ones?
So, if you have checked already that and you think you have all those good, check the connection using a standalone client first. That way you'll see if the connection can be established. I would recommend Squirrel SQL Client or Eclipse's Data Source Explorer. Give them a try to check you have the right parameters.
Once you check your connection with a local client, everything should work in the JEE container.

Error: No suitable driver found for jdbc:mysql://localhost:3306/wt

I know I am not supposed to use this code on JSP, but I just wanna check the connectivity to the database. So I am using this to check with a JSP page. But when I checked it, it threw me an error like this:
Error: No suitable driver found for jdbc:mysql://localhost:3306/wt
Source code of database.jsp:
<section>
<h3><code>Users</code> Table</h3>
<%
Connection con = null;
Statement st = null;
ResultSet rs = null;
String url = "jdbc:mysql://localhost:3306/wt";
String user = "root";
String password = "";
try {
con = DriverManager.getConnection(url, user, password);
st = con.createStatement();
rs = st.executeQuery("SELECT VERSION()");
if (rs.next()) {
out.println(rs.getString(1));
}
} catch (SQLException ex) {
System.out.println("Error: " + ex.getMessage());
}
%>
</section>
I have also included mysql-connector-java-5.1.34-bin.jar to the list of libraries. I am using the following stack:
MySQL 5.6.17
Eclipse Luna (J2EE Perspective)
JBoss 6.1 Distribution Server
Please guide me where I am going wrong. Thanks in advance.
Load the class:
Class.forName(mysqlDriverClass).newInstance();
Before you invoke any jdbc methods?
UPDATE
Include the SQL JSTL class using <%# taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%> and set your datasource <sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost/TEST" user="root" password="pass123"/> to whatever you need. Be sure to include the jstl jar.

HTTP Status 500 - An exception occurred processing JSP page /webtech/login.jsp at line 6

Its basicaly a simple project to check login and password...
i did all the things possible to figure out the error...but i cannot somehow get over it...can anyone please help and provide solution for me.
i have a html file name Login.html and a jsp file named login.jsp
mysql port no. is 3306 with username and password sait
my tomcat port no. is 8801.
Login.html
<html>
<head></head>
<body>
<form action="login.jsp" method="post">
User name :<input type="text" name="usr" />
password :<input type="password" name="pwd" />
<input type="submit" />
</form>
</body>
</html>
login.jsp
<%# page import ="java.sql.*" %>
<%# page import ="javax.sql.*" %>
<%
String userid=request.getParameter("usr");
String pswd=request.getParameter("pwd");
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sait","root","root");
Statement st= con.createStatement();
ResultSet rs=st.executeQuery("select * from login where username='"+userid+"'");
if(rs.next())
{
if(rs.getString("password").equals(pswd))
{
out.println("welcome"+userid);
}
else
{
out.println("Invalid password try again");
}
}
%>
i created a folder in C:\program files\apache software foundation\tomcat 7.0\webapps\ROOT\webtech**
and placed this 2 files **\webetech\Login.html and \webtech\login.jsp
i opened mysql and created a database named "sait"..then changed the database to "sait" using the command "use sait;" in mysql.
then i executed the following code
mysql>create table login(username varchar(10),password varchar(10));
>insert into login values("sait","sait");
>select * from login;
all executed successfully..
then i placed mysql-connector-java-5.1.26-bin in d destination C:\program files\apache software foundation\tomcat 7.0\lib\mysql-connector-java-5.1.26-bin.jar
now if i go to
http://localhost:8081/webtech/Login.html
i get the login page..but after i submit i get HTTP Status 500 - An exception occurred processing JSP page /webtech/login.jsp at line 6
Plssss help
I guess you might get NullpointerException . so just print the values of
String userid=request.getParameter("usr");
String pswd=request.getParameter("pwd");
out.print(userid + "" + pswd);
and check it out . Also its highly discourged to use the java codes in jsp pages . try using a servlet to establish JDBC
You should also add try/catch block your code to catch any exception if thrown ,
<%# page import ="java.sql.*" %>
<%# page import ="javax.sql.*" %>
<%
String userid=request.getParameter("usr");
String pswd=request.getParameter("pwd");
out.print(userid + "" + pswd);
try
{
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sait","root","root");
Statement st= con.createStatement();
ResultSet rs=st.executeQuery("select * from login where username='"+userid+"'");
while(rs.next())
{
if(rs.getString("password").equals(pswd))
{
out.println("welcome"+userid);
}
else
{
out.println("Invalid password try again");
}
}}
catch(Exception e)
{
out.print("Exception is " + e);
}
%>
If this didnt solve your issue . please post us the exception you are getting
Hope this helps !!

Connecting MySQL from JSP

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?
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
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:( )
Download the right driver :
http://dev.mysql.com/downloads/connector/j/
And add the jar in the classpath of your project.