【问题标题】:how to use a MySql database within Eclipse如何在 Eclipse 中使用 MySql 数据库
【发布时间】:2011-01-28 06:27:01
【问题描述】:

我对编程很陌生,所以请多多包涵,如果一开始我不明白,请提前道歉......!

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

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

非常感谢!

【问题讨论】:

    标签: java mysql eclipse mysql-connector


    【解决方案1】:

    如果您在 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();
            }
    

    【讨论】:

      【解决方案2】:
      猜你喜欢
      • 2018-07-30
      • 1970-01-01
      • 2020-10-28
      • 1970-01-01
      • 2018-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多