MySQL Netbeans Integration

MySQL is one of a myriad of database systems that can be connected to NetBeans and incorporated into Java programs. This howto should get you started.

  • Download the MySQL Connector/J from http://dev.mysql.com/.  The ".zip" version will be easier for Windows users (7-Zip will expand tar.gz files).
  • You will need the "jar" file at runtime so add it to Libraries for your current project.
  • Add the driver - Runtime, Databases, right click Drivers and select New Driver
  • Locate the same jar file you added to Libraries
  • Your Java program will need 4 sorts ofr variables:
    • java.sql.Connection e.g. myConnection
    • Statement e.g. stmt
    • ResultSet e.g. rs
    • variables to contain cells in the database table
  • Alt+Shift+F will fix your "imports"
  • Your code needs 6 types of statements to retrieve values from a MySQL table]
    • driver initialization - Class.forName("com.mysql.jdbc.Driver");
    • a database connection - myConnection = java.sql.DriverManager.getConnection(                "jdbc:mysql://localhost:3306/database", "user", "password");
    • a holder for SQL statements - stmt=myConnection.createStatement();
    • a result set - rs = stmt.executeQuery("select * from pq");
    • an iterator - while(rs.next()){...}
    • "getters" - p=rs.getString("p");
  • Much detail can be found at:

 
The former provides a more thorough discussion of the mysql programming issues and the latter hints what might be done with Matisse.

randomness