https://mariadb.com/kb/en/about-mariadb-connector-j/
https://docs.oracle.com/javase/8/docs/api/java/sql/DriverManager.html
In that site, Class.forName is no longer needed.
(that file based Java11, JavaEE8 with gradle)
but In my case.
If I didn't use this.
Class.forName("org.mariadb.jdbc.Driver")
HTTP:500 error occurred and that error message is
java.lang.RuntimeException: java.sql.SQLException: No suitable driver found for jdbc:mariadb://localhost:3306/mydb
Otherwise when I use that code, It work well.
Class.forName("org.mariadb.jdbc.Driver")
Why do they need [Class.forName]?
<%# page import="java.sql.Connection" %>
<%# page import="java.sql.DriverManager" %>
<%# page import="java.sql.SQLException" %>
<%# page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<%
Connection conn = null;
Class.forName("org.mariadb.jdbc.Driver");
try {
conn = DriverManager.getConnection("jdbc:mariadb://localhost:3306/mydb", "root", "1234");
} catch (SQLException e) {
throw new RuntimeException(e);
}
conn.setAutoCommit(false);
%>
The JDBC 4 standard introduced automatic registration of JDBC drivers. Earlier drivers had to manually be registered by forcing the classloader to load them, e.g., by calling Class.forName.
It seems you're using an older version of the driver that still requires the Class.forName call.
Related
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.
I'm new to JSP and I'm trying to write a function that executes a query and then returns the metadata. I'm getting an error that reads:
Generated servlet error:
Syntax error on token ")", Block expected after this token
Here is my code:
<%! ResultSetMetaData test(ResultSet rs, Statement s){
try{
rs = s.executeQuery("SELECT * FROM students WHERE name = 'Alice Wood'");
}
catch(SQLException e);
return rs.getMetaData();
}
%>
Firstly you should not write your Java code in JSP file, especially SQL queries, you should do it in your Servlet.
Secondly you used declaration tag: <%! %> which is suitable only for declarations, you need Scriptlet tag here: <% your code here %>, but as I said it is not good too, at least you should transfer your code into Servlet.
Here is good tutorial for JSP tags and overall about JSP: http://www.tutorialspoint.com/jsp/jsp_syntax.htm
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 !!
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.
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.