Open XAMPP Control Panel In the Apache service config button or select box, click httpd-xampp.conf A preview of http-xampp.conf file Go to the following line and change the Require local by Require all granted. The go back to the XAMPP control panel and restart the Apache service.
This tutorial explains steps for mysql connector java i.e connect mysql database to netbeans java project. This consists of 4 steps as,
- Install XAMPP for MySQL
- Create User, Database and Table in MySQL
- Generate connection string in Netbeans project
- Steps to insert records in table using JDBC.
You can download the XAMPP installer from Sourceforge here (102MB). Installing XAMPP Follow these steps for installing XAMPP: Step 1: Disable your anti-virus as it can cause some XAMPP components to behave erratically. Step 2: Disable User Account Control (UAC). UAC limits write permissions to XAMPP’s default installation directory (c:/Program Files/xampp), forcing you to install in a separate directory. On the root of the Xampp folder you have one mysqlstart.bat and one mysqlstop.bat, for start/stop the mysql database included on the Xampp package. You can use they in another bat you should create to start your Java Desktop application.
Install XAMPP for MySQL
XAMPP is an open source software developed by Apache friends. XAMPP software package contains Apache distributions for Apache server, MariaDB, PHP, and Perl. And it is basically a local host or a local server.
Installation of XAMPP software is very easy. You can download XAMPP from here and just complete the installation wizard. then open XAMPP control panel and start apache and mysql services as shown in image below.
Create User, Database and Table in MySQL
Xampp For Java
To create a new database in MySQL, open browser, type localhost, then click on phpMyAdmin link, click on users tab and create new user and database.as shown in image below.
Click on Add new user and enter username , password & host-name localhost. the important step is select checkbox Create database with same name and grant all privileges & global privileges.then click on go button.
To Create Table.
Xampp For Java Software
Click on Database Tab, click on database name which just created. then enter table name and click on go button.
Generate connection string in Netbeans project
Open Netbeans, create new project, right click on project, select add library. select Library for MySQL JDBC Driver. then click on Add Library. Here you can download netbeans if you don't have.
Create New Database Connection
Then Goto Services tab, right click on database , click on new connection , select driver MySQL (Connector /J Driver), next , enter username and password of mysql database which you created. and check connection by test connection button. then copy JDBC URL which is connection string.
Steps to insert records in table using JDBC
There are 6 steps to apply DML( Data Manipulation Language) statements on database using JDBC.
Load Driver - This instruction for JVM to load the desired driver implementation into memory for further JDBC requests. You can skip this step for JDBC 4.0 API because this load driver instruction in integrated in getConnection Method.
Create Connection Object - The DriverManager class acts as an interface between user and drivers. Its static method is getConnection. This method take 3 parameters as connection-string(JDBC URL), database username & password.
Create Statement Object - Statement object is created by methods of connection object. createStatement, preparedStatement & callableStatement.
Write SQL statement - Here you can write SQL DML statements like INSERT, SELECT, UPDATE & DELETE. Here we insert a record in table using INSERT query.
Execute Query on database - after writing a query string, we have to execute it on database. Query can be executed on database table by executeUpdate method of statement class.
Close connection - Active connection is terminated by close method of connection class.
Here is complete java code to insert a record into database using JDBC steps.
Goto Phpmyadmin and click on table to verify data insertion.