0

I'm trying to connect to my MariaDB database which is on localhost (using xampp). The first time I connect, the connection is established and I managed to create a table with the two fields "username" "password". Thereafter, when I try to do anything else, the Connection turns null and I cannot insert or execute any queries.

import java.sql.*;

class MyDatabase {
    private Connection conn;
    private PreparedStatement psAddPerson;


    MyDatabase(String host, String user, String password) throws Exception {
        Connection conn = DriverManager.getConnection("jdbc:mariadb:" + host,
                user, password);
        psAddPerson = null;
    }

    public void addPerson(String username, String password) {
        try{
            if( psAddPerson == null ){
                String sqlAddPerson = "insert into users (usern, pass) values (?,?)";
                psAddPerson = conn.prepareStatement(sqlAddPerson);
            }
            psAddPerson.setString(1, username);
            psAddPerson.setString(2, password);
            psAddPerson.executeUpdate();
        } catch (SQLException e) {
            System.err.println("Couldn't create add person");
            e.printStackTrace();
        }
    }
}

public class Main {
    public static void main(String[] args) {
        String host = "//localhost:3306/test";
        String user = "root";
        String pass = "";
        MyDatabase newDb = null;
        try {
            newDb = new MyDatabase(host, user, pass);
        } catch (Exception e) {
            e.printStackTrace();
        }

        String username = "admin";
        String password = "password";
        if (newDb != null) {
            newDb.addPerson(username, password);
        }
    }
}

This code produces a NullPointerException when trying to execute following code:

psAddPerson = conn.prepareStatement(sqlAddPerson);

I want to be able to execute queries to my localhost database.

Picture from phpMyAdmin of server type: Picture of server type

Thanks

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Cows42
  • 307
  • 3
  • 12

3 Answers3

2

Change this:

Connection conn = DriverManager.getConnection("jdbc:mariadb:" + host, user, password);

by this:

conn = DriverManager.getConnection("jdbc:mariadb:" + host, user, password);

This way you're instantiating the instance variable rather than a local variable within the constructor.

Hope this helps!

Ele
  • 33,468
  • 7
  • 37
  • 75
1

The fix is easy change:

Connection conn = DriverManager.getConnection("jdbc:mariadb:" + host,
            user, password);

to:

conn = DriverManager.getConnection("jdbc:mariadb:" + host,
            user, password);
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Saïd
  • 8,780
  • 1
  • 28
  • 28
0

Tweak it a little bit and change this :

Connection conn = DriverManager.getConnection("jdbc:mariadb:" + host, user, password);

to this:

conn = DriverManager.getConnection("jdbc:mariadb:" + host, user, password);
Lucifer Rodstark
  • 206
  • 4
  • 14