Configure Java in netbeans to access the MySQL database

Source: Internet
Author: User
Tags netbeans

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.