使用jdbc,代码如下:
  package com.example.mysql;
  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.ResultSet;
  import java.sql.SQLException;
  import java.sql.Statement;
  public class Util
  {
  final static String DRIVER_NAME = "com.mysql.jdbc.Driver";
  public static Connection openConnection(String url, String user,
  String password)
  {
  Connection conn = null;
  try
  {
  Class.forName(DRIVER_NAME);
  conn = DriverManager.getConnection(url, user, password);
  }
  catch(ClassNotFoundException e)
  {
  conn = null;
  }
  catch(SQLException e)
  {
  conn = null;
  }
  System.out.println(conn);
  return conn;
  }
  public static void query(Connection conn, String sql)
  {
  if (conn == null)
  {
  return;
  }
  Statement statement = null;
  ResultSet result = null;
  try
  {
  statement = conn.createStatement();
  result = statement.executeQuery(sql);
  System.out.println(result);
  if (result != null && result.first())
  {
  int idColumnIndex = result.findColumn("id");
  int nameColumnIndex = result.findColumn("name");
  System.out.println("id " + "name");
  while (!result.isAfterLast())
  {
  System.out.print(result.getString(idColumnIndex) + " ");
  System.out.println(result.getString(nameColumnIndex));
  result.next();
  }
  }
  } catch (SQLException e)
  {
  e.printStackTrace();
  }
  finally
  {
  try
  {
  if (result != null)
  {
  result.close();
  result = null;
  }
  if (statement != null)
  {
  statement.close();
  statement = null;
  }
  }
  catch (SQLException sqle)
  {
  sqle.printStackTrace();
  }
  }
  }
  public static boolean execSQL(Connection conn, String sql)
  {
  boolean execResult = false;
  if (conn == null)
  {
  return execResult;
  }
  Statement statement = null;
  try
  {
  statement = conn.createStatement();
  if (statement != null)
  {
  execResult = statement.execute(sql);
  }
  }
  catch (SQLException e)
  {
  execResult = false;
  }
  return execResult;
  }
  }