3

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

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

有谁能给我一步一步的方法吗?!

非常感谢!

4

2 回答 2

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();
        }
于 2010-03-18T10:37:34.757 回答