NetBeans, connect to a database [closed]
want to improve this question? Add details and clarify the problem by editing this post .
Closed 4 years ago .
Improve this questionHello I am developing a basic program that consists of connecting netbeans to a database via mysql, the problem is that I do not know what to do, this database is supposed not only to be on one machine if not several Should the machines be networked? Or is there any way to access by WIFI?
WHAT DO YOU RECOMMEND?
2 answers
You can use JDBC with the MySQL driver. To create the database you can use mysql workbench, you can manage it on the same machine.
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 auto Wizard (next image).
- by downloading it from the official page and importing it into your project.
Where should my database be located data?
Depends, if you need it only for Intranet, on a computer of your Network p.e 192.168.1.100
and when you connect you access 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, comes more to account PHP-PDO).
How do I work/manage with MySQL?
There are many alternatives, the 2 most simple and popular sound:
- phpMyAdmin : web environment located on the server.
- MySQLWorkbench : desktop environment for working locally.
I recommend you work in MySQLWorkbench and then port it to phpMyAdmin.
How do I establish a connection and query?
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 get 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" );
});