如何安装JDBC以及如何使用它连接到mysql? [英] how to install JDBC and how to use it to connect to mysql?

查看:131
本文介绍了如何安装JDBC以及如何使用它连接到mysql?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试安装JDBC,但我不知道如何,当您只有jar文件时,我将其复制到了我的Java ext文件夹中,但是它一直给我一个错误,谁能告诉我如何完成安装驱动程序并用它吗?

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?

下面是我使用的代码

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) {}
}

       }
   }

下面是我得到的回复

Cannot connect to database server

更新#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.

推荐答案

您正在尝试连接带有 jTDS JDBC驱动程序的URL的MySQL ,该URL专为

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.

您确实需要 MySQL JDBC驱动程序.另请参见此答案,以获取简短但完整的教程

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

这篇关于如何安装JDBC以及如何使用它连接到mysql?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆