简体   繁体   中英

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

i am trying to install JDBC but i dont know how, when you only have the jar file, i copied it to my java ext folder but it keep giving me an error, can anyone show me how to complete install the driver and use it?

below is 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 Response 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.

You're trying to connect MySQL with the URL of a jTDS JDBC driver which is designed specifically for Microsoft SQL Server . This ain't ever going to work. Even not when you fix the current problem by placing the JAR file in classpath.

You really need the MySQL JDBC driver . Also see this answer for a short but complete tutorial

另一方面,如果您使用诸如 Netbeans 或 Eclipse 之类的 IDE,您可以将 jar 文件作为资源添加到项目中。

You certainly have JDBC problems, but the exception isn't telling you that. Read it again:

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

It's your Test1.class that it can't find, not the JDBC driver.

You should not be copying anything into the jre/lib/ext directory. That's for library extensions, not JDBC JARs. It wasn't meant as a crutch for people who don't understand how CLASSPATH works.

I'd write it more like the following. Those close methods will come in handy.

When I run it on my machine, adding the MySQL JDBC JAR to my CLASSPATH, I get the following result:

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

Here is the source code:

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();
        }
    }
}

'I am trying to install JDBC'

You don't have to install JDBC. It is part of the JDK & JRE.

尝试将您的 .jar 文件放在classpath 中

The library that contains the Driver ( net.sourceforge.jtds.jdbc.Driver ) needs to be on the classpath.

Assuming you start your application with

java Test1

then simply do

java -cp .;driver.jar Test1

where 'driver.jar' should be exchanged with the filename (relative or full path) of your database driver lib.

EDIT

A classpath tutorial will exceed the comments section below this question. Please take a cup of coffee and look at this page . It will most likely help you to continue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM