-2

So, I'm trying to get the variable Weekly Sales from a SQL table, and then add the amounts to a total amount. That being said, I can't seem to be able to connect and I get an error. The error is "No suitable driver found for jdbc:mysql://128.198.162.204/INFS3070" even though I believe I have everything that I need, and everything looks correct to me.

Here's my code for the part I'm stuck on.

    import java.sql.*;

    public class FileConnection 
    {
    public static void main(String[] args) 
    {
        try 
        { 
            // Create variables for database connection parameters
            String url = "jdbc:mysql://128.198.162.204/INFS3070";
            String user = "infsclass";
            String password = "webclass";
            Connection conn = null;
            Statement sqlQuery = null;
            ResultSet results = null;
                        
            // Connect to database using variables
            conn = DriverManager.getConnection(url, user, password); // Different to separate connection file
                        
            // Simply for esthetic purposes
            System.out.println("Connection to database created.");
            System.out.println("------------------------------------------------------");
                        
            // Create object to execute SQL statements
            sqlQuery = conn.createStatement();
                                    
            // Use a String to send the SQL statement to the database
            results = sqlQuery.executeQuery("SELECT SUM FROM SALES");
                                
            // Display results in console
            // We use a while loop to read all of the data
            while (results.next()) 
            {
                // Formatting the console output to appear as a table.
                System.out.printf("%s, %-15s %-15s %-15s %-15s %s\n", // Formats how the printf data will align
                         
                        results.getString("PK"), 
                        results.getString("Store"), 
                        results.getString("Dept"), 
                        results.getString("SalesDate"), 
                        results.getString("Weekly_Sales"), 
                        results.getString("IsHoliday"));
            }
              
            // Close database connection
            conn.close();
            System.out.println("-----------------------------------------------------");
            System.out.println("Connection to database closed.");     
        }
          
        catch (SQLException e) 
        {  
            e.printStackTrace();
        }           
    }
}

Also, here's the SQL table

enter image description here

Shadow
  • 32,277
  • 10
  • 49
  • 61
Mario
  • 33
  • 5
  • 1
    MySQL <> SQL Server - please correct your tags. – Dale K May 12 '22 at 20:06
  • As per the question guide, please do not post images of code, data, error messages, etc. - copy or type the text into the question. Please reserve the use of images for diagrams or demonstrating rendering bugs, things that are impossible to describe accurately via text. – Dale K May 12 '22 at 20:06
  • Hi @DaleK, Okay, thanks for letting me know. I corrected the tags. Is there an easier way to make a table, other than using the | & -, as I feel using those will make it take a while, as I have a kind of bigger table to show. – Mario May 12 '22 at 20:15
  • Are you using MySQL _and_ Microsoft SQL Server? If not, please leave the tag edits alone. Also I can't imagine `SELECT SUM FROM SALES` is a valid query in _any_ platform - `SUM` of _what?_ – Aaron Bertrand May 12 '22 at 20:15
  • You haven't corrected the tags, you're still showing both when you are actually only using 1. Secondly, we only need a [mre] with the emphasis on *minimal* i.e. we only need enough data to demonstrate the issue. And as far as I know there are tools out there where you can copy and paste your data and have the formatted text returned. – Dale K May 12 '22 at 20:17
  • @AaronBertrand Yes, I am for MySQL. No, for Microsoft SQL Server. I was just told to change them from what I had a few minutes ago. I thought that SQL server was the correct tag, as I thought that since I was pulling from a SQL server, that it was the correct tag, if that makes sense. The SUM is supposed to be the total from the Weekly Sales variable in the table. – Mario May 12 '22 at 20:21
  • Possible duplicate of https://stackoverflow.com/questions/1457716/what-is-the-mysql-jdbc-driver-connection-string – Aaron D May 12 '22 at 20:22
  • If you're unsure (especially after someone has corrected you), [read the tag description](https://stackoverflow.com/questions/tagged/sql-server). There it says `Do not use this tag for other types of DBMS (MySQL, PostgreSQL, Oracle, etc.)` Established users aren't going to correct your tags unless they're incorrect. – Aaron Bertrand May 12 '22 at 20:23
  • You may need to specify your port in the connection URL. Try this `jdbc:mysql://128.198.162.204:3306/INFS3070` – Daniel Black May 12 '22 at 21:08
  • Hi @DanielBlack, I did that and it's still not working. For some reason, the DriverManager.getConnection line seems to be the issue, even though I have everything set up correctly. – Mario May 12 '22 at 21:27

0 Answers0