0x001D - Java DB Connector Example

Java DB Connector Example

0x001D - Java DB Connector Example
Photo by Jéssica Silveira / Unsplash

In this quick tutorial we will be going over the construction of a  Java class method that will make a test connection to a simple database

MySQL Connector:

A full working example pulling data from the table from keywords.

import com.mysql.cj.protocol.a.result.ResultsetRowsStatic;
import java.sql.*;
public class sql_handler {
    public sql_handler() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://192.168.2.200:3306/","root",   "<password>");
            Statement sql_query = con.createStatement();
            ResultSet res_set = sql_query.executeQuery("SELECT * FROM fmp_data.fmp_table");
            while(res_set.next())
            {
               String sym = res_set.getString("symbol");
               String name = res_set.getString("name");
               float price = res_set.getFloat("price");
               String exch = res_set.getString("exchange");
               String exch_sn = res_set.getString("exch_sn");
               String stype = res_set.getString("stype");
            }

        } catch (Exception E) {
            String Error = String.valueOf(E);
            System.out.println(Error);
        }

    }
}
Linux Rocks Every Day