This slide is based on Jeffrey D. Ullman's work, which can be download from his website.
References: 刘增杰、张少军 《MySQL 5.5 从零开始学》
启动 Eclipse.
新建一个 java project. 点击菜单 File-->New-->Java Project.
出现如下对话框:
输入要建的工程的名字, 比如
可以点击各个选项卡查看一下什么内容
点击 Finish, 完成工程的创建. 在 Package Explorer 中可以看到刚创建的项目 de.vogella.mysql.first
由于我们要连接 MySQL 数据库, 因此需要 java 连接 mysql 的库文件, 即 mysql-connector-java-5.1.40-bin.jar
在项目中建立 lib 文件夹
新的文件夹命名为 lib, 选择所在的父文件夹, 这里是 de.vogella.mysql.first
然后将 mysql-connector-java-5.1.40-bin.jar 导入. 在菜单上点击 File --> Import, 出现 Import 对话框. 点击 General --> File System, 然后点击 Next 按钮.
从文件系统中找到 mysql-connector-java-5.1.40-bin.jar 所在的目录. 点击确定按钮.
勾选要导入的文件 mysql-connector-java-5.1.40-bin.jar, 点击 Finish 按钮.
查看 Package Explorer 中是否列出了 lib/mysql-connector-java-5.1.40-bin.jar
将此文件添加到 Build Path. 右键此文件, 出现如下菜单, 在 Build Path 子项中点击 Add to Build Path.
package mysql_java_test; import mysql_java_test.MySQLAccess; public class Main { public static void main(String[] args) throws Exception { MySQLAccess dao = new MySQLAccess(); dao.readDataBase(); } }
package mysql_java_test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; //import java.util.Date; public class MySQLAccess { private Connection connect = null; private Statement statement = null; private PreparedStatement preparedStatement = null; private ResultSet resultSet = null; public void readDataBase() throws Exception { try { // This will load the MySQL driver, each DB has its own driver Class.forName("com.mysql.jdbc.Driver"); // Setup the connection with the DB connect = DriverManager .getConnection("jdbc:mysql://localhost/test_bar?" + "user=odbc&password="); // Statements allow to issue SQL queries to the database statement = connect.createStatement(); // Result set get the result of the SQL query resultSet = statement .executeQuery("select * from test_bar.bars"); writeResultSet(resultSet); // PreparedStatements can use variables and are more efficient preparedStatement = connect .prepareStatement("insert into test_bar.bars2 values (?, ?, ?)"); // "name, addr, license; // Parameters start with 1 preparedStatement.setString(1, "NewBar1"); preparedStatement.setString(2, "NewAddress1"); preparedStatement.setString(3, "NewLicense1"); //preparedStatement.setDate(4, new java.sql.Date(2009, 12, 11)); preparedStatement.executeUpdate(); /* preparedStatement = connect .prepareStatement("SELECT bar, beer, price from test_bar.Sells"); resultSet = preparedStatement.executeQuery(); writeResultSet(resultSet); */ // Remove again the insert comment preparedStatement = connect .prepareStatement("delete from test_bar.bars2 where name= ? ; "); preparedStatement.setString(1, "NewBar1"); preparedStatement.executeUpdate(); resultSet = statement .executeQuery("select * from test_bar.Beers"); writeMetaData(resultSet); } catch (Exception e) { throw e; } finally { close(); } } private void writeMetaData(ResultSet resultSet) throws SQLException { // Now get some metadata from the database // Result set get the result of the SQL query System.out.println("The columns in the table are: "); System.out.println("Table: " + resultSet.getMetaData().getTableName(1)); for (int i = 1; i<= resultSet.getMetaData().getColumnCount(); i++){ System.out.println("Column " +i + " "+ resultSet.getMetaData().getColumnName(i)); } } private void writeResultSet(ResultSet resultSet) throws SQLException { // ResultSet is initially before the first data set while (resultSet.next()) { // It is possible to get the columns via name // also possible to get the columns via the column number // which starts at 1 // e.g. resultSet.getSTring(2); String user = resultSet.getString("name"); String website = resultSet.getString("addr"); String summary = resultSet.getString("license"); //Date date = resultSet.getDate("datum"); System.out.println("Name: " + user); System.out.println("Address: " + website); System.out.println("License: " + summary); System.out.println("---------------------------"); } } // You need to close the resultSet private void close() { try { if (resultSet != null) { resultSet.close(); } if (statement != null) { statement.close(); } if (connect != null) { connect.close(); } } catch (Exception e) { } } }