如何在Eclipse中使用MySql数据库

时间:2010-03-16 18:47:45

标签: java mysql eclipse mysql-connector

我是编程的新手,所以请耐心等待,如果一开始我没有意义,请提前道歉......!

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

那里的任何人都能够一步一步地给我一个方法吗?!

非常感谢!

2 个答案:

答案 0 :(得分:4)

如果您需要在Eclipse中使用某种数据资源管理器,您可以查看上面提供的链接或更具体的插件文档。

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();
        }

答案 1 :(得分:3)