-1

I'm new to Java network side. For some reason my java file cannot connect to the localhost database. I keep on receiving an error. Please help

test.java:

import java.sql.*;

class test {
    public static void main(String[] args) {
        try {
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/user", "root", "");
        } catch(Exception exp) {
            exp.printStackTrace();
        }
    }
}

error:

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/user
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at test.main(test.java:7)

directory:

enter image description here

Aman
  • 691
  • 11
  • 27
  • Been on that question. Useless – Aman Apr 09 '19 at 23:36
  • 1
    Have you added the mysql connector jar to the classpath? – Aishwarya Apr 09 '19 at 23:53
  • 2
    @Aman If you have actually studied that other page and found your problem to be different, you must specify exactly how your situation is distinct. Explain what solutions from that page you tried, and how exactly did they fail to resolve your problem. – Basil Bourque Apr 10 '19 at 00:05
  • @BasilBourque I don't even understand anything stated in that. The program is too complex to understand – Aman Apr 10 '19 at 02:12
  • @Aman "No driver found" means no JDBC driver was found available by the JVM at runtime. Learn about [JDBC drivers](https://en.wikipedia.org/wiki/JDBC_driver), and how to properly install one. And [find some tutorials](https://duckduckgo.com/?q=tutorial+JDBC+MySQL&t=osx&ia=web) to follow. While learning, consider using another database such as [H2](https://en.wikipedia.org/wiki/H2_(DBMS)). Configuring JDBC drivers has been covered many many times on Stack Overflow. Always search and study before posting here. – Basil Bourque Apr 10 '19 at 04:00
  • @BasilBourque So in conclusion its impossible for a single Java file to connect to Database. – Aman Apr 10 '19 at 09:30
  • @Aman Correct, your single `.java` file is not enough to connect to a database via JDBC. You need a JDBC driver for your particular database as well, at runtime. Study the [Oracle tutorials on JDBC](https://docs.oracle.com/javase/tutorial/jdbc/TOC.html). – Basil Bourque Apr 10 '19 at 10:44

1 Answers1

-1

I think that you should add the jar connector (https://dev.mysql.com/downloads/connector/j/)

ayoub mlaouah
  • 166
  • 1
  • 9