How to connect NetBeans to a MySQL database using Java?
There are 2 ways with NetBeans, but for both you will need the MySQL driver- JDBC .
- With the automatic assistant (next image).
- Downloading it from the official page and importing it into your project.
Where should my database be located?
Depends, if you need it only for Intranet, on a computer in your network p.e 192.168.1.100
and when you connect access to that address, if you need it for the Internet, with JDBC is more complex, hardly any hosting offers support for JDBC, you should hire a server and install it (much more cost, go out more PHP-PDO account).
How do I work / manage with MySQL?
There are many alternatives, the 2 most simple and popular are:
I recommend working in MySQLWorkbench and then porting it to the phpMyAdmin.
How do I establish a connection and make inquiries?
I have a small library that complies with this, LINK
Usage examples
tryConnection (arguments) .ping () to configure the connection and check it.
boolean response = MySQL.tryConnection(url, port, database, user, password).ping();
insert (query, parameters, OnInsert) to perform an insert SQL query.
String query = "INSERT INTO person VALUES (?, ?, ?, ?);";
String[] params = new String[]{null, "java", "mysql", "22"};
MySQL.insert(query, params, (rowsInserted) -> {
System.out.println( rowsInserted != 0 ? "INSERT OK" : "INSERT ERROR" );
});
select (query, parameters, OnSelect) to perform a procurement SQL query.
String query = "SELECT * FROM person WHERE age > ?;";
String[] params = new String[]{"17"};
MySQL.select(query, params, (resultSet) -> {
while (resultSet.next()) { /* Código */ }
System.out.println( resultSet != null ? "SELECT OK" : "SELECT ERROR" );
});