Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Databases

Shannon Ferguson
Shannon Ferguson
4,307 Points

How to link java in intelliJ to MYSQL databases

Hi there

I hope someone out there is having a similar issue.

I have managed to connect a MYSQL database to IntelliJ. This is what I would like to happen: When a user is prompted to type their name in the console it saves the name to a table created in MYSQL. The problem I am having is that I I can't seem to connect the java code to the SQL code so that they can work together. I have injected the MYSQL language but I am not sure where to go to find the correct documentation or tutorial to link the two languages.

I have done the Databases course and the Java course but there is no course that links the two using intelliJ and MYSQL. I would like to create a program where user input is saved to a database.

Please Please can someone help with this? I look forward to any response. Many thanks. Shannon

Ken Alger
Ken Alger
Treehouse Teacher

Shannon;

Is you code up on GitHub or a similar place where folks can take a look at it?

Post a link to it if so.

Ken

4 Answers

Shannon Ferguson
Shannon Ferguson
4,307 Points

I realized that I haven't actually installed the JDBC driver or Connector/J. I think that is why it isn't working. I get this error: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhos:3306/pops at java.sql.DriverManager.getConnection(DriverManager.java:689) at java.sql.DriverManager.getConnection(DriverManager.java:247) at com.pops101.Main.main(Main.java:19) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

I am struggling with the Connector/J installation. Do I have to alter my class path? I can't seem to find a simple tutorial on how to do this.

Shannon Ferguson
Shannon Ferguson
4,307 Points

This is the code I had:

package com.pops101;

import java.sql.SQLException; import java.sql.*;

public class Main { private static String username = "root"; private static String password = "indigoblueMYSQL01"; private static String connectionString = "jdbc:mysql://localhos:3306/pops"; private static Connection connection; private static Statement command; private static ResultSet data;

public static void main(String[] args)
{
    try
    {
        connection = DriverManager.getConnection(connectionString, username, password);
        command = connection.createStatement();
        command.execute("INSERT INTO details VALUES ('Shan','FERG')");
    }
    catch(SQLException sqle)
    {
        sqle.printStackTrace();
    }
}

}

Are you using Hibernate (with Spring)? (The course is out in February, it looks like.) It connects Java and SQL through Java Annotations.

Shannon Ferguson
Shannon Ferguson
4,307 Points

Thanks Lukas. That course is exactly the kind of thing I need. I will look out for it.