How to install JDBC and how to use it to connect to mysql?

I'm trying to install JDBC, but I don’t know how, when you only have the jar file, I copied it to the java ext folder, but it gives me an error all the time, can anyone show me how to complete the driver installation and use his?

below are the codes that I used

import java.sql.*;
   public class Test1
   {
       public static void main (String[] args)
       {
String url = "jdbc:mysql://localhost:3306/sabayafr_sabmah";
String username = "root";
String password = "ma";
Connection connection = null;
try {
    System.out.println("Connecting database...");
    connection = DriverManager.getConnection(url, username, password);
    System.out.println("Database connected!");
} catch (SQLException e) {
    System.err.println("Cannot connect the database!");
    e.printStackTrace();
} finally {
    System.out.println("Closing the connection.");
    if (connection != null) try { connection.close(); } catch (SQLException ignore) {}
}

       }
   }

And below is the answer that I get

Cannot connect to database server

Update # 3

C:\Users\AlAsad\Desktop>java -cp .;mysql-connector-java-5.0.8-bin.jar Test1
Connecting database...
Cannot connect the database!
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/
sabayafr_sabmah
        at java.sql.DriverManager.getConnection(Unknown Source)
        at java.sql.DriverManager.getConnection(Unknown Source)
        at Test1.main(Test1.java:12)
Closing the connection.
+5
source share
6 answers

You are trying to connect MySQL with the jTDS JDBC URL , which is specifically designed for Microsoft SQL Server . It will never work. Even if you do not fix the current problem by placing the JAR file in the classpath.

MySQL JDBC. . ,

+9

, IDE, ​​ Netbeans Eclipse, jar .

+1

, , JDBC, . :

Exception in thread "main" java.lang.NoClassDefFoundError: Test1
Caused by: java.lang.ClassNotFoundException: Test1

Test1.class, , JDBC.

- jre/lib/ext. , JDBC JAR. , , CLASSPATH.

. .

, JDBC JAR JARBC CLASSPATH, :

C:\java -classpath .\mysql-connector-java-5.1.6-bin.jar; persistence.utils.DatabaseUtils
product: MySQL
version: 5.1.24-rc-community
major  : 5
minor  : 1

:

package persistence.utils;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DatabaseUtils
{
    public static final String DRIVER = "com.mysql.jdbc.Driver";
    public static final String URL = "jdbc:mysql://localhost:3306/contacts";
    public static final String USERNAME = "contacts";
    public static final String PASSWORD = "contacts";

    public static void main(String[] args)
    {
        Connection connection = null;

        try
        {
            String driver = ((args.length > 0) ? args[0] : DRIVER);
            String url = ((args.length > 1) ? args[1] : URL);
            String username = ((args.length > 2) ? args[2] : USERNAME);
            String password = ((args.length > 3) ? args[3] : PASSWORD);

            connection = getConnection(driver, url, username, password);

            DatabaseMetaData metaData = connection.getMetaData();

            System.out.println("product: " + metaData.getDatabaseProductName());
            System.out.println("version: " + metaData.getDatabaseProductVersion());
            System.out.println("major  : " + metaData.getDatabaseMajorVersion());
            System.out.println("minor  : " + metaData.getDatabaseMinorVersion());
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            close(connection);
        }
    }


    public static Connection getConnection(String driver, String url, String username, String password) throws ClassNotFoundException, SQLException
    {
        Connection connection = null;

        Class.forName(driver);
        connection = DriverManager.getConnection(url, username, password);

        return connection;
    }

    public static void close(Connection connection)
    {
        try
        {
            if (connection != null)
            {
                connection.close();
            }
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
    }

    public static void close(Statement statement)
    {
        try
        {
            if (statement != null)
            {
                statement.close();
            }
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
    }

    public static void close(ResultSet resultSet)
    {
        try
        {
            if (resultSet != null)
            {
                resultSet.close();
            }
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
    }

    public static void rollback(Connection connection)
    {
        try
        {
            if (connection != null)
            {
                connection.rollback();
            }
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
    }
}
+1

' JDBC'

JDBC. JDK JRE.

+1

.jar classpath.

0

, (net.sourceforge.jtds.jdbc.Driver), .

,

java Test1

do

java -cp .;driver.jar Test1

"driver.jar" ( ) lib.

. , . , .

0

All Articles