A Tutorial for NetBeans Java 6.5 on MySQL

A Tutorial for NetBeans Java 6.5 on MySQL thumbnail
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

  1. 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.

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).

Related Searches:

References

Resources

  • Photo Credit Stockbyte/Stockbyte/Getty Images

Comments

You May Also Like

  • Netbeans Debugger Tutorial

    The Netbeans Debugger is an important tool for any Java programmer using Sun's Netbeans Java Integrated Development Environment (IDE). With this tool,...

  • MySQL Java Tutorial

    The Java Developers Kit includes a package known as the Java Database Connectivity API, or the JDBC for short. This provides a...

  • How to Create a Database in NetBeans

    NetBeans is a popular cross-platform integrated development environment. It supports a wide range of programming languages, including Java, PHP, C++ and Python....

  • How to Create a Java Applet in Netbeans

    Netbeans is an IDE for Java, PHP and Apache. IDE, which stands for Integrated Development Environment, is basically a fancy way of...

  • MySQL ConnectorJ Tutorial

    The MySQL Connector/J driver is used to connect Java applications to a MySQL database. The driver is completely Java driven and does...

  • How to Run a Client Server in NetBeans

    NetBeans is a free Integrated Development Environment (IDE) software suite for computer developers. The project creation tools within NetBeans can help you...

  • Netbeans IDE 6.1 Mobile Tutorials

    NetBeans IDE 6.1 is an integrated development environment (IDE) written in the Java programming language that can be used as a generic...

  • How to Test a JDBC Driver

    The JDBC driver is used in applications to call procedures and query tables in mySQL. When programming a database connection, it's important...

  • Java Sun SQL Tutorial

    Accessing a relational database from Java is a straightforward process. Many applications use Java interfaces for accessing data from a desktop environment....

  • How to Make a Java Game With NetBeans

    NetBeans is a popular integrated development environment for the Java programming language. It is sponsored by Oracle and is free to use...

Related Ads

Featured