How to connect MySQL program to Java

I installed MySQL (latest update). I need to code to create and establish a connection to SQL DB & amp; manage the database (using SELECT, INSERT, CREATE).

I did everything, but could not create a connection. I also installed the MySQL / J connector, I just extracted the package .zipto the folder and added the path to the folder in Variables).

Can someone say wat means URL in the line below?

Connection connection = DriverManager.getConnection(url, username, password);

I tried this:

String url = "jdbc:odbc:sqlserver://localhost:3306/myfirstdb";
Connection con = DriverManager.getConnection(url, "root", "1234");

But it does not work. I can not understand the term "URL". Can someone explain the meaning of "url" and "wat" must be done to connect to the SQL server with Java.


Update:

This is the complete code. He still cannot connect.

import java.sql.*;

public class TestDriver {

public static void main(String[] args) {
try {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//This s wat actually i did for connection
    System.out.println("Driver Loaded Succesfully");
}
catch (Exception e){
    System.out.println("Unable to Load Driver!!!");
}

try {
    Class.forName(com.mysql.jdbc.Driver");  // initialise the driver
    String url ="jdbc:mysql://localhost:3306/myfirstdb";
    Connection con = DriverManager.getConnection(url, "root", "1234");
    System.out.println("connection Established");
    }
    catch(Exception e) {
                System.out.println("Couldnt get connection");
    }
    }
}

Can you say that wat is the goal of connecting MySQL Connector / J?

+5
4

, , jdbc MySQL URL- jdbc SQL Server. .

MySQL:

Class.forName("com.mysql.jdbc.Driver");  // initialise the driver

String url ="jdbc:mysql://localhost:3306/myfirstdb";

SQL Server, jdbc. jTDS - . jtds.jar - :

Class.forName("net.sourceforge.jtds.jdbc.Driver");  // initialise the driver

String url = "jdbc:jtds:sqlserver://localhost:1433/myfirstdb";
+4

:

} catch (Exception e) {
    System.out.println("Couldnt get connection");
}

, . :

} catch (Exception e) {
    System.out.println("Could not get connection");
    e.printStackTrace();
}

, ,

} catch (Exception e) {
    throw new RuntimeException("Could not get connection", e);
}

, . ClassNotFoundException SQLException. , . , . / .

. - . , , :)

. :


- , wat URL ?

URL- Uniform Resource Locator. () . URL MySQL JDBC.

, wat MySQL Connector/J?

JDBC. JDBC API . API JDBC, JDBC. JDBC- JDBC API.

+3

MS SQL Server,

String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
Class.forName(driver);
String url = "jdbc:microsoft:sqlserver://host:1433/database";
Connection conn = DriverManager.getConnection(url, "username", "password");

For more information, see this to get started with Microsoft JDBC.

You can use either of the two JDBC drivers for MSSQL:

For MS SQL Server 2.0 driver use

URL: jdbc:sqlserver://server:port; DatabaseName=dbname
Class name: com.microsoft.sqlserver.jdbc.SQLServerDriver

For MySql and Java, see this in SO.

+2
source

You forgot "at Class.forName(com.mysql.jdbc.Driver"); It should be

Class.forName("com.mysql.jdbc.Driver");
0
source

All Articles