1. First, add a database package reference: Project-> database, right-click-> Add database, find the MySQL JDBC driver in the list of available databases, and click Add database.
2. Configure to connect to the MySQL database
I access the MySQL database on the remote server. First, I configure the remote access to the MySQL database. This is not the focus of this article. I can find it online and have a lot of information. Service-> database-> driver-> MySQL (connector/J driver), right-click and choose connection settings. The following interface is displayed for Configuration:
Name: Default Value
HOST: IP address of the database
Port: Default MySQL port 3306
Database: name of the database that can be accessed remotely
User Name
Password
3. Program
Import javax. Swing. joptionpane;
Import java. SQL .*;
Import java. SQL. resultset;
Import com. MySQL. JDBC. connection;
Import com. MySQL. JDBC. statement;
Try
{
Class. forname ("com. MySQL. JDBC. Driver"). newinstance ();
Connection conn = (connection) drivermanager. getconnection ("JDBC: mysql: // 111.111.111.111: 3306/databasename", "user", "pass ");
Statement stmt = (statement) Conn. createstatement ();
Resultset result = (resultset) stmt.exe cutequery ("select * from user ");
String STR = "";
While (result. Next ())
{
String TMP = result. getstring ("username ");
STR + = TMP;
}
Joptionpane. showmessagedialog (null, STR );
}
Catch (Java. Lang. Exception ex)
{
}
The above code is successfully debugged in netbeans6.5.