Cant make connection with MariaDB and Java

java mariadb connection example
mariadb connection string
jsp mariadb connect
mariadb-java-client github
how to connect mariadb with netbeans
mariadb-java-client maven repo
no suitable driver found for jdbc:mariadb
mariadb-java-client-2.1.0.jar download

Im trying to create web app using java and mariadb but i encountered problem when tried to implement mariadb to login. Here my code:

initSql: package dao;

import java.sql.Connection;
import java.sql.DriverManager;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;


@WebServlet("/initSql")
public class initSql extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#HttpServlet()
 */
public initSql() {
    super();
    // TODO Auto-generated constructor stub
}

/**
 * @see Servlet#init(ServletConfig)
 */
Connection conn = null;
public void init(ServletConfig config) throws ServletException {
    // TODO Auto-generated method stub
    try {
        Class.forName("org.mariadb.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mariadb://localhost:3306/baza_new", "root","root");
        System.out.println("db povezana");

    }catch(Exception e){
        //JOptionPane.showMessageDialog(null, e);
        System.out.println("db NIiiJE povezana");
        //return null;
}

}
}   

LoginDAO:

    package dao;

    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import dao.initSql;

    public class LoginDAO {

    static Connection con = null;

    public static boolean validate(String username, String password, String type) {

        boolean status = false;
        try {
            con = initSql.init();
            System.out.println("1");
            String query = "select * from users where username=? and password=?";
            PreparedStatement pst = con.prepareStatement(query);

            //pst.setString(1, type);
            pst.setString(1, username);
            pst.setString(2, password);

            ResultSet rs = pst.executeQuery();
            status= rs.next();
            con.close();    
        }catch(Exception e) {System.out.print(e);}

        return status;      
    }   
}

and i get markers: Cannot make static reference to non-static method from type generic servler Type mistmatch cannot connect from void to Connection I'm little bit stuck with this problem.Can someone help me with my code?

People seem to be neglecting the more broad-scale issues in your code. There are standards to follow like capitalization etc but overall you have some bigger issues.

You shouldn't be making erroneous instances of initSql as it's an HttpServlet, it just doesn't make sense. You also have static/non-static references to a Connection field when you don't need it. To start with, change initSql#init to return a Connection, and while I normally wouldn't recommend abusing static this way, make the method itself static:

//returns a connection, requires no class instance
public static Connection init(ServletConfig config) { ... }

From there, we can now retrieve a Connection instance by calling this method:

Connection con = initSql.init();

Overall you should have a proper class or design for handling this, but for simple learning this is "okay".

Secondly, you're not quite using ResultSet correctly. #next will determine if there is an available row to point to from the SQL results, and if so it moves the marker to the next row. You would use it in order to check if you can retrieve results:

ResultSet set = /* some sql query */;
String someField;
if (set.next()) {
    //gets the value of the column "my_field"
    someField = set.getString("my_field");
} else {
    //no results!
    someField = null;
}

Alternatively, if you were looking for multiple results you can loop over #next

while (set.next()) {
    //just one value out of many
    String myField = set.getString("my_field");
}

In this use-case it's alright to check if the row exists, but I would personally check against something like user permissions or somesuch. If you relied on code like this for something sensitive you might expose something you don't want to.

Overall, I would work a little more on your logical structure for the code, and maybe go over some of the basics for Java and common coding standards for it (Google and Oracle have good guides for this).

About MariaDB Connector/J, Additionally, sample code is provided that demonstrates how to connect to one of​  About MariaDB Connector/J. MariaDB Connector/J is a Type 4 JDBC driver. It was developed specifically as a lightweight JDBC connector for use with MariaDB and MySQL database servers. It was originally based on the Drizzle JDBC code with numerous additions and bug fixes.

Firstly, your class name initSql should have Capitalized first letter to follow conventions.

Secondly, you should either create an instance/object of InitSql and then call the method init() on that object or make the init() method static.

Cannot connect to mysql/mariadb from java, Strangely enough however, using mysql workbench I can connect with no /cant​-connect-to-mariadb-from-java-only-with-mysqldeveloper Just as an FYI, adding pictures to DO questions can be done in markdown with. Some MariaDB packages bind MariaDB to 127.0.0.1 (the loopback IP address) by default as a security measure using the bind-address configuration directive. Old MySQL packages sometimes disabled TCP/IP networking altogether using the skip-networking directive.

initSql.init() isn't static, which is not a problem of MariaDB and its connection from Java :) To fix this error you can add static to the mentioned method. But: As there are multiple errors in your code (e.g. assigning the result of a void method to a variable), it will not work then either..

Java Connection to MariaDB, Learn how to establish connection to MariaDB database server from your Java Log into your Jelastic account and create an environment with the MariaDB (or This value should be set to false, as such implementation doesn't work with the  just looked.. our test image isn't mariadb it is mysql. We use mariadb as the client for license reasons. Latest java client is 2.2.3 which is only a couple patches below latest.

"Error establishing a database connection” MySQL/MariaDB Linux , Make Sure MySQL is Running. If MySQL has stopped running, scripts and websites will be unable to connect. On a Linux server, you can see if MySQL is  Java Connection to MariaDB/MySQL 1. Log into your Jelastic account and create an environment with the MariaDB (or MySQL) database server (available 2. Check your email inbox - it should contain a message with administration details for the created MariaDB (or MySQL) 3. Switch back to the

Connecting to a MariaDB/MySQL using Java, Create a new Java console project. I'm using tje IntellJ IDE. 7.-. Add MySQL connector class Duration: 5:22 Posted: Apr 3, 2020 To find the endpoint for a MariaDB DB instance in the AWS Management Console. Open the RDS console and then choose Databases to display a list of your DB instances. Choose the MariaDB DB instance name to display its details. On the Connectivity & security tab, copy the endpoint. Also, note the port number.

Connect using MySQL Workbench, Get the connection information that's required to connect to the Azure Database for MariaDB instance. You need the fully qualified server name  MariaDB Tutorial. MariaDB Tutorial provides basic and advanced concepts of MariaDB. Our MariaDB Tutorial is designed for beginners and professionals both. MariaDB is a community based project of the MySQL relational database management system. It is open source and relational database technology. It is a great replacement of MySQL.

Comments
  • Unrelated to the problem, you can use the MySQL driver here instead of the MariaDB one for a MariaDB connection, usually the driver for MySQL is kept more sanely.
  • How should i fix this?
  • remove static from your validate method, and call it on an instance of your LoginDAO class
  • @louseDepo I moved my comment to my answer. Feel free to mark it (or any other answer for that matter) as accepted, to resolve this question. Questions without accepted answers remain "open", which is undesired. Ask a new question (after searching the existing questions/answers, obviously) in case there's something unclear with the remaining problems