pom.xml添加依赖

 <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.45</version>
    </dependency>
Java:方式一(亲测实用)
import java.sql.*;
public class JDBCUtil {
    private static final String DBDRIVER = "com.mysql.jdbc.Driver";// 驱动类类名
    private static final String DBURL = "jdbc:mysql://(ip):3306/stu?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai";// 连接URL

    private static final String DBUSER = "root";// 数据库用户名
    private static final String DBPASSWORD = "xxxxxx";// 数据库密码
    private static Connection conn = null;
    private static PreparedStatement ps = null;
    private static ResultSet rs = null;

    /*
     * 获取数据库连接
     */
    public static Connection getConnection() {
        try {
            Class.forName(DBDRIVER);// 注册驱动
            conn = DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD);// 获得连接对象
            System.out.println("成功加载SQL Server驱动程序");
        } catch (ClassNotFoundException e) {// 捕获驱动类无法找到异常
            System.out.println("找不到SQL Server驱动程序");
            e.printStackTrace();
        } catch (SQLException e) {// 捕获SQL异常
            e.printStackTrace();
        }
        return conn;
    }

    public static ResultSet select(String sql) throws Exception {
        try {
            ps = (PreparedStatement) conn.prepareStatement(sql);
            rs = ps.executeQuery();
            return rs;
        } catch (SQLException sqle) {
            throw new SQLException("select data Exception: "
                    + sqle.getMessage());
        } catch (Exception e) {
            throw new Exception("System error: " + e.getMessage());
        }
    }

    /*
     * 增删改均调用这个方法
     */
    public static void update(String sql) throws Exception {
        try {
            conn = getConnection();
            ps = (PreparedStatement) conn.prepareStatement(sql);
            ps.executeUpdate();
        } catch (SQLException sqle) {
            throw new SQLException("insert data Exception: "
                    + sqle.getMessage());
        } finally {
            try {
                if (ps != null) {
                    ps.close();
                }
            } catch (Exception e) {
                throw new Exception("ps close exception: " + e.getMessage());
            }
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (Exception e) {
                throw new Exception("conn close exception: " + e.getMessage());
            }
        }
    }

    public static void closeAll() {
        try {
            rs.close();
            ps.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
方式二:Scala
import java.sql.{ Connection, DriverManager }
object ScalaJdbcConnectSelect extends App {

  // 访问本地MySQL服务器,通过3306端口访问mysql数据库
  val url = "jdbc:mysql://localhost:3306/cgjr?useUnicode=true&characterEncoding=utf-8&useSSL=false"
  //驱动名称
  val driver = "com.mysql.jdbc.Driver"
  //用户名
  val username = "root"
  //密码
  val password = "12345"
  //初始化数据连接
  var connection: Connection = ""
  try {
    //注册Driver
    Class.forName(driver)
    //得到连接
    connection = DriverManager.getConnection(url, username, password)
    val statement = connection.createStatement
    //执行查询语句,并返回结果
    val rs = statement.executeQuery("SELECT name, num FROM persons")

    //打印返回结果
    while (rs.next) {
      val name = rs.getString("name")
      val num = rs.getString("num")
      println("name = %s, num = %s".format(name, num))
    }

    println("查询数据完成!")
    //    执行插入操作
    val rs2 = statement.executeUpdate("INSERT INTO `persons` (`name`, `num`) VALUES ('徐志摩', '22')")
    println("插入数据完成")

	//执行更新操作
    val rs3 = statement.executeUpdate("UPDATE persons set num=55 WHERE `name`=\"徐志摩\"")
    println("更新数据完成!")

	//执行删除操作
    val rs4 = statement.executeUpdate("delete from persons WHERE `name`=\"徐志摩\"")
    println("删除数据完成!")

	//执行调用存储过程操作
    val rs5 = statement.executeUpdate("call add_student(3)")
    println("调用存储过程完成!")

  } catch {
    case e: Exception => e.printStackTrace
  }
  //关闭连接,释放资源
  connection.close
}
方式三:Java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.apache.log4j.Logger;
 
public class DBMysqlUtil {
	private Connection conn = null;
    private PreparedStatement ps = null;
	private ResultSet rs = null;
	private String dbDriver = null;
	private String dbConnectionURL = null;
	private String dbUsername = null;
	private String dbPassword = null;
	private PropUtil PropUtil=null;
	private Logger logger = Logger.getLogger(DBMysqlUtil.class);
	
	public DBMysqlUtil(){
		PropUtil = new PropUtil("config/db.properties");
		dbDriver = PropUtil.get("Driver");
		dbConnectionURL = PropUtil.get("ConnectionURL");
		dbUsername = PropUtil.get("Username");
		dbPassword = PropUtil.get("Password");	
	}
	
	public DBMysqlUtil(String dbDriver, String dbConnectionURL, String dbUsername,String dbPassword){
		this.dbDriver = dbDriver;
		this.dbConnectionURL = dbConnectionURL;
		this.dbUsername = dbUsername;
		this.dbPassword = dbPassword;
	}
	/**
	 * 功能:获取数据库连接
	 */
	private Connection getConnection() {
		System.out.println("连接地址:"+dbConnectionURL);
		System.out.println("用户名:"+dbUsername);
		System.out.println("密码:"+dbPassword);
		try {
			Class.forName(dbDriver);
			conn = DriverManager.getConnection(dbConnectionURL, dbUsername,
					dbPassword);
			logger.info("数据库连接成功");
		} catch (Exception e) {
			logger.error("Error: DbUtil.getConnection() 获得数据库链接失败.\r\n链接类型:"
					+ dbDriver + "\r\n链接URL:" + dbConnectionURL + "\r\n链接用户:"
					+ dbUsername + "\r\n链接密码:" + dbPassword, e);
		}
		return conn;
	}
	
	/**
	 * 功能:执行查询语句
	 */
    public ResultSet select(String sql) {  
    	logger.info("Exec select sql:" + sql);
            try {
            	conn = getConnection();
				ps = conn.prepareStatement(sql);				
				rs = ps.executeQuery(sql);
			} catch (SQLException e) {				
				logger.error("查询数据异常:"+ e.getMessage());
			}
            return rs;
        
    }
    
    /**
	 * 功能:执行查询语句,获取记录数
	 */
	public int getRecordCount(String sql) {
		logger.info("Exec getRecordCount sql:" + sql);
		int counter = 0;
		try {
			conn = getConnection();
			ps = conn.prepareStatement(sql);				
			rs = ps.executeQuery(sql);
			while (rs.next()) {										
				counter++;
			}
		} catch (SQLException e) {
			logger.error("执行DbUtil.getRecordCount()方法发生异常,异常信息:", e);
		}finally {
     	   close();
        }
		  System.out.println("counter总数:"+counter);
		return counter;
	}
    
    /**
   	 * 功能:针对单条记录执行更新操作(新增、修改、删除)
   	 */
   public int executeupdate(String sql) throws Exception {
	   logger.info("Exec update sql:" + sql);
           int num = 0;
           try {
               conn = getConnection();
               ps = conn.prepareStatement(sql);
               num = ps.executeUpdate();
           } catch (SQLException sqle) {
        	   logger.error("insert/update/delete  data Exception: " +
                   sqle.getMessage());
           } finally {
        	   close();
           }
           System.out.println("影响条数:"+num);
           return num;
       }
    
       /**
   	 * 
   	 * 功能:批量执行SQL(update或delete)
   	 * 
   	 * @param sqlList
   	 *            sql语句集合
   	 */
   	public int executeBatch(List<String> sqlList) {
   		int result = 0;
   		for (String sql : sqlList) {
   			try {
				result += executeupdate(sql);
			} catch (Exception e) {
				  System.out.println("查询异常:"+e.getMessage());
			}
   		}
   		System.out.println("executeBatch Result:"+result);
   		return result;
   	}  
    
    /**
	 * 功能:关闭数据库的连接
	 */
	public void close() {
		try {
			if (rs != null) {
				rs.close();
			}
			if (ps != null) {
				ps.close();
			}
			if (conn != null) {
				conn.close();
			}
			logger.info("关闭数据库连接成功");
		} catch (Exception e) {
			logger.error("执行DbUtil.close()方法发生异常,异常信息:", e);
		}
	}
}
PhoenixJDBCUtils
import java.sql.*;
public class PhoenixJDBCUtils {
    private static String driverClassName;
    private static String URL;
    private static String username;
    private static String password;
    private static boolean autoCommit;

    /** 声明一个 Connection类型的静态属性,用来缓存一个已经存在的连接对象 */
    private static Connection conn;
    static {
        config();
    }
    /**     * 开头配置自己的数据库信息     */
    private static void config() {
        /*         * 获取驱动         */
        driverClassName = "org.apache.phoenix.jdbc.PhoenixDriver";
        /*         * 获取URL         */
        URL = "jdbc:phoenix:slave1,slave2,slave3:2181/hbase";
        /*         * 获取用户名         */
        username = "";
        /*         * 获取密码         */
        password = "";
        /*         * 设置是否自动提交,一般为false不用改         */
        autoCommit = true;
    }
    /**     * 载入数据库驱动类     */
    private static boolean load() {
        try {
            Class.forName(driverClassName);
            return true;
        } catch (ClassNotFoundException e) {
            System.out.println("驱动类 " + driverClassName + " 加载失败");
        }
        return false;
    }

    /**     * 建立数据库连接     */
    public static Connection connect() {
        /* 加载驱动 */
        load();
        try {
            /* 建立连接 */
            conn = DriverManager.getConnection(URL, username, password);
        } catch (SQLException e) {
            System.out.println("建立数据库连接失败 , " + e.getMessage());
        }
        return conn;
    }

    /**     * 设置是否自动提交事务     **/
    public static void transaction() {
        try {
            conn.setAutoCommit(autoCommit);
        } catch (SQLException e) {
            System.out.println("设置事务的提交方式为 : " + (autoCommit ? "自动提交" : "手动提交") + " 时失败: " + e.getMessage());
        }
    }

    /**     * 创建 Statement 对象     */
    public static Statement statement() {
        Statement st = null;
        connect();
        /* 如果连接是无效的就重新连接 */
        transaction();
        /* 设置事务的提交方式 */
        try {
            st = conn.createStatement();
        } catch (SQLException e) {
            System.out.println("创建 Statement 对象失败: " + e.getMessage());
        }
        return st;
    }

    /**     * 根据给定的带参数占位符的SQL语句,创建 PreparedStatement 对象     *
     * @param SQL    带参数占位符的SQL语句
     * @return 返回相应的 PreparedStatement 对象
     */
    private static PreparedStatement prepare(String SQL, boolean autoGeneratedKeys) {
        PreparedStatement ps = null;
        connect();
        /* 如果连接是无效的就重新连接 */
        transaction();
        /* 设置事务的提交方式 */
        try {
            if (autoGeneratedKeys) {
                ps = conn.prepareStatement(SQL, Statement.RETURN_GENERATED_KEYS);
            } else {
                ps = conn.prepareStatement(SQL);
            }
        } catch (SQLException e) {
            System.out.println("创建 PreparedStatement 对象失败: " + e.getMessage());
        }
        return ps;
    }

    public static ResultSet query(String SQL, Object... params) {
        if (SQL == null || !SQL.trim().toLowerCase().startsWith("select")) {
            throw new RuntimeException("你的SQL语句为空或不是查询语句");
        }
        ResultSet rs = null;
        if (params.length > 0) {
            /* 说明 有参数 传入,就需要处理参数 */
            PreparedStatement ps = prepare(SQL, false);
            try {
                for (int i = 0; i < params.length; i++) {
                    ps.setObject(i + 1, params[i]);
                }
                rs = ps.executeQuery();
            } catch (SQLException e) {
                System.out.println("执行SQL失败: " + e.getMessage());
            }
        } else {
            /* 说明没有传入任何参数 */
            Statement st = statement();
            try {
                rs = st.executeQuery(SQL); // 直接执行不带参数的 SQL 语句
            } catch (SQLException e) {
                System.out.println("执行SQL失败: " + e.getMessage());
            }
        }
        return rs;
    }

    /**     *  释放资源     *   **/
    public static void release(Object cloaseable) {
        if (cloaseable != null) {
            if (cloaseable instanceof ResultSet) {
                ResultSet rs = (ResultSet) cloaseable;
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (cloaseable instanceof Statement) {
                Statement st = (Statement) cloaseable;
                try {
                    st.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (cloaseable instanceof Connection) {
                Connection c = (Connection) cloaseable;
                try {
                    c.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}