Home > Database > Mysql Tutorial > body text

java jdbc连接mysql_MySQL

WBOY
Release: 2016-06-01 13:06:38
Original
748 people have browsed it

JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口组成。JDBC为数据库开发人员提供了一个标准的API,使数据库开发人员能够用纯 Java API 编写数据库应用程序,并且可跨平台运行,并且不受数据库供应商的限制。其优点:

  • 操作便捷:开发人员不需要再使用复杂的驱动器调用命令和函数;
  • 可移植性强:JDBC支持不同的关系数据库
  • 通用性好:JDBC-ODBC桥接驱动器将JDBC函数换成ODBC;
  • 面向对象:可以将常用的JDBC数据库连接封装成一个类,在使用的时候直接调用即可
package DAO;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import java.sql.SQLException;import java.util.ArrayList;import java.util.Collections;import java.util.HashMap;import java.util.List;import java.util.Map;import com.mysql.jdbc.Statement;public class JDBC {    protected  Connection conn;    protected  Statement statement;    public static Connection getConnection() throws Exception {        Connection connection = null; // 创建用于连接数据库的Connection对象        try {            Class.forName("com.mysql.jdbc.Driver");// 加载Mysql数据驱动            /**             * 127.0.0.1指本机,若是链接远程服务机器,则填写远程机器的ip 3306 mysql默认的端口号 test 数据库名称             * user 数据库用户名称 password 密码             */            connection = DriverManager.getConnection(                    "jdbc:mysql://127.0.0.1:3306/test", "user", "password");// 创建数据连接        } catch (Exception e) {            e.printStackTrace();            throw new Exception("链接mysql数据失败");        }        return connection; // 返回所建立的数据库连接    }    /**     * 向mysql插入数据记录 返回插入数据的个数     *      * @param sql     *            要插入的sql语句     * @return count 插入数据的个数     * @throws Exception     */    public  int insert(String sql) throws Exception {        conn = getConnection(); // 连接到数据库        try {            statement = (Statement) conn.createStatement(); // 创建用于执行静态sql语句的Statement对象            int count = statement.executeUpdate(sql); // 执行插入操作的sql语句            conn.close(); // 关闭数据库连接            return count;// 返回插入数据的个数        } catch (SQLException e) {            e.printStackTrace();            throw new Exception("插入数据失败");        }    }    /**     * 更新符合要求的记录 返回更新的记录数目     *      * @param sql     *            更新数据的sql语句     * @return count 更新数据的个数     * @throws Exception     */    public  int update(String sql) throws Exception {        conn = getConnection(); // 连接到数据库        try {            // 创建用于执行静态sql语句的Statement对象,            statement = (Statement) conn.createStatement();            int count = statement.executeUpdate(sql);// 执行更新操作的sql语句,            conn.close(); // 关闭数据库连接            return count; // 返回更新数据的个数        } catch (SQLException e) {            e.printStackTrace();            throw new Exception("更新数据失败");        }    }    /**     * 查询数据库,返回符合要求的记录的数据     *      * @param sql 查询数据的sql语句     * @throws Exception     * @return list      */    public  List<object> query(String sql) throws Exception {        conn = getConnection(); // 连接到数据库        try {            statement = (Statement) conn.createStatement(); // 创建用于执行静态sql语句的Statement对象            ResultSet rs = statement.executeQuery(sql); // 执行sql查询语句,返回查询数据的结果集            List<object> list=ResultSetToList(rs);            conn.close(); // 关闭数据库连接            return list;        } catch (SQLException e) {            e.printStackTrace();            throw new Exception("查询数据失败");        }    }    /* 删除符合要求的记录,输出情况*/      /**     *      * @param sql 删除数据的sql语句     * @return count 返回删除数据的数量     * @throws Exception     */    public  int delete(String sql) throws Exception {          conn = getConnection(); //连接到数据库          try {              statement = (Statement) conn.createStatement();    //创建用于执行静态sql语句的Statement对象            int count = statement.executeUpdate(sql);// 执行sql删除语句              conn.close();   //关闭数据库连接                return count;//返回删除数据的数量        } catch (SQLException e) {              e.printStackTrace();            throw new Exception("删除数据失败");        }                }    /**     * 分页查找     * @param sql 要查找的sql语句     * @param page 页数     * @param count 数据条数     * @return List<object>     * @throws Exception     */    public  List<object> findByPage(String sql,              int page,int count) throws Exception {            conn = getConnection(); //连接到数据库          PreparedStatement pre = conn.prepareStatement(sql);          pre.setMaxRows(count);          ResultSet rs = pre.executeQuery();          if(page list=ResultSetToList(rs);        return list;      }      /**     * ResultSet 转换成List     */    public  List<object> ResultSetToList(ResultSet rs) throws SQLException{        if (rs == null)            return Collections.emptyList();        ResultSetMetaData md = rs.getMetaData(); // 得到结果集(rs)的结构信息        int columnCount = md.getColumnCount(); // 返回此 ResultSet 对象中的列数        List<object> list = new ArrayList<object>();        Map<object object> rowData = new HashMap<object object>();        while (rs.next()) {            rowData = new HashMap<object object>(columnCount);            for (int i = 1; i <p> </p>
    </object></object></object></object></object></object></object></object></object></object>
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!