【问题标题】:Trying to use JBDC but getting error finished with non-zero exit value 1尝试使用 JBDC 但以非零退出值 1 完成错误
【发布时间】:2021-08-30 11:45:40
【问题描述】:

我正在尝试实现 JBDC 的简单用途以用于学习目的,但我收到错误“以非零退出值 1 完成”。下面是我的代码和 gradle.build。

我的代码:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnectionJBDC {
    public static void main(String[] args) throws SQLException {
        String urlConnection = "jbdc:mysql://localhost/digital_innovation_one";
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(urlConnection , "MyLogin" , "MyPass");
            System.out.println("Sucesso");
        } catch (Exception e){
            System.out.println("Falhou");
        } finally {
            conn.close();
        }
    }
}

我的 gradle.build:

plugins {
    id 'java'
}

group 'one.innovation.digital'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    implementation group: 'mysql', name: 'mysql-connector-java', version: '8.0.26'
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
}

test {
    useJUnitPlatform()
}

我尝试运行程序时遇到的错误:

Execution failed for task ':ConnectionJBDC.main()'.
> Process 'command '/usr/lib/jvm/default-java/bin/java'' finished with non-zero exit value 1

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

有人可以帮忙吗?我一无所知(这里只是一个菜鸟)。

【问题讨论】:

  • Run with --stacktrace 错误明确指出。但是
  • System.out.println("Falhou"); 帮不了你。你想要e.printStacktrace();
  • 使用 jdbc: 而不是 'jbdc:mysql'。检查你的网址
  • 谢谢大家!那是我与 JAVA 的第一次接触,所以我没有把所有事情都做好。下次我将使用 --stacktrace 和 e.printStacktrace() (刚刚学会如何使用)。

标签: java gradle mysql-connector


【解决方案1】:

首先(根据您的驱动程序版本可能不需要),在开头使用此代码加载您的驱动程序:

try {
    Class.forName("com.mysql.jdbc.Driver");
    // this class name change according to your driver. In your case, I think it's good one
} catch (ClassNotFoundException e) {
    // Cannot find driver for MySQL
}

那么你没有设置好的驱动程序名称。使用这个:

jdbc:mysql://localhost/digital_innovation_one

(jdbc 代替 jbdc)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-13
    • 2015-07-09
    • 1970-01-01
    • 1970-01-01
    • 2015-05-28
    相关资源
    最近更新 更多