of {$slidecount} ½ {$title} ATZJG.NET {$author}

首页






Java 连接 MySQL
Java 与数据库的连接


Haifeng Xu


(hfxu@yzu.edu.cn)

This slide is based on Jeffrey D. Ullman's work, which can be download from his website.

References: 刘增杰、张少军 《MySQL 5.5 从零开始学》

目录

Eclipse

Eclipse

启动 Eclipse.

新建一个 java project

新建一个 java project

新建一个 java project. 点击菜单 File-->New-->Java Project.

输入要建的工程的名字, 比如

Main.java

Main.java

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

}

MySQLAccess.java

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) {

		}
	}

}

End






Thanks very much!