问题从eclipse项目连接到sql数据库?

时间:2016-05-13 00:14:39

标签: java php mysql sql eclipse

我正在尝试从使用MAMP制作的基本数据库中检索信息。这是创建的db(testdb_vfk)在MAMP中的样子:

enter image description here

这是我的java代码,用于连接和检索它:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class Main {
    public static void main(String[] args) throws Exception{

        //Accessing driver from the JAR file
        Class.forName("com.mysql.jdbc.Driver");

        //Creating a variable for the connection called "con"
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:8888/testdb_vfk","root","root");
        //jdbc:mysql://localhost:3306/employee_record --> This is the database
        // root is the database user
        // root is the password

        // here we create our query
        PreparedStatement statement = con.prepareStatement("select * from name");

        // Creating a variable to execute query
        ResultSet result = statement.executeQuery();

        while(result.next()){
            System.out.println(result.getString(1) + " " + result.getString(2));
            // getString returns the data
            // 1 is the first field in the table
            // 2 is the second field
        }
    }
}

然而,每当我尝试运行它时,我都会收到此错误:

enter image description here

关于我无法连接的任何想法?

编辑:这是我的网址:

enter image description here

0 个答案:

没有答案