如何在 Eclipse 中使用 MySql 数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2457179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 14:31:19  来源:igfitidea点击:

how to use a MySql database within Eclipse

javamysqleclipsemysql-connector

提问by aadersh patel

I am very new to programming, so please bear with me, and apologies in advance if at first I dont make sense...!

我对编程很陌生,所以请多多包涵,如果一开始我没有理解,请提前道歉......!

I am doing an undergrad programming project, and need to make some databases within a Java program. I am using eclipse (galilo) to write my program. I have downloaded a connector/J, but havent the foggiest how i should use it!

我正在做一个本科编程项目,需要在 Java 程序中创建一些数据库。我正在使用 eclipse (galilo) 来编写我的程序。我已经下载了一个连接器/J,但我还不清楚我应该如何使用它!

Anyone out there able to give me a step by step approach?!

任何人都可以给我一步一步的方法?!

Many thanks!

非常感谢!

回答by Vaishak Suresh

If you need a data explorer of some sort inside your eclipse, you can look at the links provided above or more specifically the plugin's documentation.

如果您在 Eclipse 中需要某种类型的数据浏览器,您可以查看上面提供的链接或更具体地说是插件的文档。

OTOH, if you want to know how you connect to a mysql database using JDBC, the below code sample explains it.

OTOH,如果您想知道如何使用 JDBC 连接到 mysql 数据库,下面的代码示例对此进行了解释。

Connection connection = null;
        try {
            //Loading the JDBC driver for MySql
            Class.forName("com.mysql.jdbc.Driver");

            //Getting a connection to the database. Change the URL parameters
            connection = DriverManager.getConnection("jdbc:mysql://Server/Schema", "username", "password");

            //Creating a statement object
            Statement stmt = connection.createStatement();

            //Executing the query and getting the result set
            ResultSet rs = stmt.executeQuery("select * from item");

            //Iterating the resultset and printing the 3rd column
            while (rs.next()) {
                System.out.println(rs.getString(3));
            }
            //close the resultset, statement and connection.
            rs.close();
            stmt.close();
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }