A Tutorial for NetBeans Java 6.5 on MySQL
The NetBeans interactive development environment is a product created by Sun Microsystems. It supports programming development in Java, C++, PHP and other high-level languages. One fundamental task that is performed by many modern applications is connecting to a data source, either in the form of a database or a flat file, such as a MySQL or an XML file. The programming language Java can connect to MySQL. The programming can be done in NetBeans.
Instructions
-
Create a New Project in NetBeans
-
1
Open up the NetBeans IDE.
-
2
Navigate to "File" and select "New Project." From the New Project window, select "Java" from the "Categories" menu and "Java Application" from the "Projects" menu. Click "Next."
-
-
3
Enter "MySQLTest" into the project name field. Accept the default values in the other fields. Click "Finish."
Write the Program
-
4
Verify that the Editor window has appeared after the new project has been created.
-
5
Locate the "{" and "}" characters that span several lines. The gray text that was just removed should have been located between the two curly brace characters.
-
6
Enter the following lines of code between the curly braces:
Connection connection = null; // Instantiate a new connection
try
{
String USERNAME = "username";
//MySQL Username
String PASSWORD = "password";
//MySQL Password
String url = "jdbc:mysql://localhost/test";
//Location of Database
Class.forName ("com.mysql.jdbc.Driver").newInstance();
//JDBC driver
connection = DriverManager.getConnection (url, USERNAME, PASSWORD);
System.out.println ("Success! Database connection has been established");
Statement statement = connection.createStatement(); //create SQL statement wrapper
statement.execute("create table table_test ( test_column integer )"); //create a sample table in the database
statement.execute("insert into table_test values(1)"); //create some test data
statement.execute("select test_column from table_test "); //select some data from the newly created table
ResultSet resultset = statement.getResultSet(); //get the resultset
if (resultset != null){ //if the result set exists
while ( resultset.next() ) //step through the result set and print out the data
{
System.out.println("Data from test_column: " + resultset.getString(1) );
}//end while
}//end if
//statement.execute("drop table table_test ");
statement.close(); // close the Statement
connection.close(); // close the Connection
}
catch (Exception e)
{
System.err.println ("Failure! Cannot connect to database server");
}
finally
{
if (connection != null)
{
try
{
connection.close (); // close the connection to the database
System.out.println ("Database connection has been terminated");
}
catch (Exception e) { /* disregard closing errors */ }
}
}
-
7
Locate the line that reads:
"package mysqltest;"
-
8
Enter the following line of code on the next line:
import java.sql.*;
-
9
Navigate to "File" and then select "Save."
Compile, Run and Verify the Results
-
10
Navigate to "Run" and select "Run Main Project." This compiles the Java code and runs it as a Java application.
-
11
Locate the "Output" window. It is usually collapsed by default. Clicking on the icon in the lower-left corner that reads "Output" should expand it.
-
12
Verify that the output reads:
"Data from test_column: 1"
If the program performed without error, sample data should be visible and no error messages will have appeared.
-
1
Tips & Warnings
If there are errors during the running of the program, check over the code and make sure that it is copied correctly.
Additionally, make sure that the file path to the database file is correct, and that the username and password are correct.
To avoid errors, ensure that your JDBC driver is installed properly on your machine (see Resources).
References
Resources
- Photo Credit Stockbyte/Stockbyte/Getty Images