日期:2014-05-16  浏览次数:20419 次

简单的jdbc连接mysql数据库清单
package com.jdbc.example;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCExample1 {
	public static void main(String[] args) {
		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "user", "password");
			stmt = con.createStatement();
			rs = stmt.executeQuery("select * from events");
			while (rs.next()) {
				System.out.println("EVENT_ID: " + rs.getLong(1) + " EVENT_DATE: "
						+ rs.getString(2) + " title: "
						+ rs.getString(3));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
			try {
				rs.close();
			} catch (Exception e) {
			}
			try {
				stmt.close();
			} catch (Exception e) {
			}
			try {
				con.close();
			} catch (Exception e) {
			}
		}
	}
}