简体   繁体   English

试图连接到mysql数据库

[英]trying to connect to mysql database

I'm trying to connect to my database. 我正在尝试连接到我的数据库。 I added the MySQL connector driver jar by 我通过添加了MySQL连接器驱动程序jar

  • creating a folder called lib 创建一个名为lib的文件夹
  • Putting the jar inside lib 将罐子放入lib
  • Right Clicking on the project >> Properties >> build path >> (going inside the libraries tab) >> adding jar 右键单击项目>>属性>>构建路径>>(进入“库”选项卡)>>添加jar

my code looks like this: 我的代码如下所示:

public static void main(String[] args) {
    try {
        Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/world", "user", "pass");
        Statement myStat = myConn.createStatement();
        ResultSet myRs = myStat.executeQuery("Select * from city");
        while (myRs.next()) {
            System.out.println(myRs.getString("Name"));
        }
    }
    catch (Exception exc) {
        exc.printStackTrace();
    }}

ERROR: 错误:

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/world
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at javademo4.driver.main(driver.java:8)

I'm using Eclipse Luna and mysql-connector-java-5.0.8-bin.jar. 我正在使用Eclipse Luna和mysql-connector-java-5.0.8-bin.jar。 I also definitely have a database called world and a table called city which has a column called Name 我当然也有一个名为world的数据库和一个名为city的表,其中有一个名为Name的列

First of all you have to register your mysql driver, add this line above the Connection line Class.forName("com.mysql.jdbc.Driver") 首先,您必须注册您的mysql驱动程序,在ConnectionClass.forName("com.mysql.jdbc.Driver")上方添加此行

Hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试使用 jdbc 连接到 mysql 数据库 - Trying to connect to mysql database with jdbc 尝试连接到MySQL数据库时Java中的NullPointerException - NullPointerException in java while trying to connect to MySQL database 尝试连接到 MYSQL 数据库时出现 EJBTransactionRolledbackException - Getting an EJBTransactionRolledbackException when trying to connect to a MYSQL database 尝试使用Eclipse和Hibernate 5连接到mysql数据库 - trying to connect to mysql database using eclipse and hibernate 5 尝试通过Eclipse,Java连接到mysql数据库时访问被拒绝 - Access denied when trying to connect to mysql database via eclipse, java Azure-尝试连接到外部MySQL数据库时权限被拒绝 - Azure - permission denied when trying to connect to external MySQL database 尝试从虚拟机(Java)连接到mysql数据库 - Trying to connect to mysql database from virtual machine (Java) 尝试将MySQL数据库连接到android时遇到NullPointerException错误 - Getting a NullPointerException error trying to connect a MySQL database to android 尝试将简单的 Java 程序连接到 Google Cloud Platform MySQL 数据库 - Trying to connect simple Java program to Google Cloud Platform MySQL Database 试图让 java 程序连接到 Mac 上的本地 Mysql 数据库 - Trying to get a java program to connect to a local Mysql Database on Mac
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM