次の方法で共有


Java and MySQL

Java, installation is pretty easy, but if you need the JDK, there was one confusing thing, you have to use one of these links, which are, for version 1.8, THEN download the Demos and Samples, starting at this page: https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

To check your installation, the easy way is to install NetBeans, which won’t load unless the JDK is installed, the NetBeans download is small so it doesn’t hurt.  The installers put the files into the Program Files/Java/Bin/Java.exe or Javac.exe or Program Files (x86)/Java/Bin/Java.exe or Javac.exe folder, and you will need to modify your eclipse.ini to point to that folder.  Although, you could install the Java files in the same folder as the Eclipse folder.  Your choice, Java functions inside of a virtual machine so it doesn’t really care where it is located.  This means you could have an external drive and run everything off of the external hard drive.

Java, Eclipse and MySQL

Ignoring NetBeans and VS Code for now.  Now let’s use the Java/Eclipse through JBDC to MySQL.  The tutorial online that I used seemed to assume that there was a database named “test”, which for mine MySQL server wasn’t true, so I used the MySQL 5.7 Command Line Client and created the database.  Of course I would have used Java right?  Well I didn’t.  So now we have the JBDC connected to Java, Eclipse and MySQL.  This means that we create databases and do all of the fun things we want to do with a database.   But let’s take a look at how I used the JRE 1.8.0., like Microsoft documentation, most of the useful tutorials, articles and so forth use older versions with different UIs.

image

 

 

Connecting to MySQL

Once you have the JBDC connector set-up, and NetBeans was a big help in demonstrating to me that the JDK was installed correctly.  There may be a requirement to change your eclipse.ini, I used Visual Studio Code for the change (I use VS Code for everything these days!).

Note that Line 16 can point to Javac.exe or Java.exe.  You can place this folder in the same folder as Eclipse, which makes it easier to share your set-up with others. 

image

Initially when I ran my Java file against the MySQL database I got a bunch of errors.  First, I used the wrong password, so that was easily fixed.  Second, the tutorial assumed that I had the default ‘test’ database in my LocalHost server, I didn’t.  So I opened the MySQL command line and added the table that way, do this by using search in Windows 10, search for MySQL and then select the MySQL command line client (not the “MySQL Workbench 6.3 CE”, then scroll down to see more:

image

The command line can be run “As Administrator” by right clicking and selecting “As Administrator”.  I didn’t need to do that, and I am not running as administrator, local user is fine.

Once the MySQL you can experiment or use the server hosted on your laptop, this is not an optimum place to have your database, but let’s go with it.

In the following MySQL command line you use normal SQL commands, and I have left some errors in there, where I used the incorrect objects like table and so forth:

image

Use Eclipse to connect to the localhost server

In this component, we will use Eclipse to connect to the localhost server.  Here is my code that I got from the internet, but didn’t store the link, so apologies to the person I swiped it from, let me know in the comments code if it is yours, I will put the code set into GitHub.  As you can see, the code is very similar to what you might use in Visual Studio and C#.

public Connection getConnection() throws SQLException {
        Connection conn = null;
        Properties connectionProps = new Properties();
        connectionProps.put("user", this.userName);
        connectionProps.put("password", this.password);

        conn = DriverManager.getConnection("jdbc:mysql://"
                + this.serverName + ":" + this.portNumber + "/" + this.dbName,
                connectionProps);
        return conn;
    }

Use Eclipse to add a table

Here is simple code.  First try to connect and if you are unsuccessful throw an error, then create a table using the SQL statement, execute an update

public void run() {

        // Connect to MySQL
        Connection conn = null;
        try {
            conn = this.getConnection();
            System.out.println("Connected to database");
        } catch (SQLException e) {
            System.out.println("ERROR: Could not connect to the database");
            e.printStackTrace();
            return;
        }

        // Create a table
        try {
            String createString =
                    "CREATE TABLE " + this.tableName + " ( " +
                    "ID INTEGER NOT NULL, " +
                    "NAME varchar(40) NOT NULL, " +
                    "STREET varchar(40) NOT NULL, " +
                    "CITY varchar(20) NOT NULL, " +
                    "STATE char(2) NOT NULL, " +
                    "ZIP char(5), " +
                    "PRIMARY KEY (ID))";
            this.executeUpdate(conn, createString);
            System.out.println("Created a table");
        } catch (SQLException e) {
            System.out.println("ERROR: Could not create the table");
            e.printStackTrace();
            return;
        }

Ok, big deal, how do I use MySQL in Azure from my favorite Java Tool?

In my next blog (promise) I will discuss how to use Maven and the Azure SDK for Java, or as Java developers who use Eclipse might call it: Azure Plugins. 

The use of Java with Azure is a pretty big story, so I need some white space to make it happen.

 

Conclusion

With the exception of a confusing set-up instructions, once functional the JBDC works in a manner the same as OBDC or other tools you might use.   We used Structured Query Language (SQL), but JSON is also supported.  It appears that even if you are not using NetBeans, NetBeans can act to test your Java JDK or SDK is installed correctly.  Naturally all Windows O/S versions there is a runtime Java component, but for Eclipse to work with JDBC you have to make sure that the Java JDK.

Not having the Java JDK installed correctly is likely the thing that can slow you down in using Azure, which is a good efficient cloud system.