Java数据库编程

JDBC(Java Database Connectivity):是java中提供的一套数据库编程API,它定义了一套用来访问数据库的标准Java类库(位于java.sql和javax.sql包中)。利用JDBC,可以                                                      用java编写程序,实现与特定的数据库连接,向数据库发送SQL语句,实现对数据库的特定操作,并对数据库返回结果进行处理。

1.JDBC编程六个步骤 :(1).根据应用程序所用数据库,选择JDBC驱动程序类型。

                              (2).连接到数据库,得到Connection对象。

                              (3).通过Connection创建Statement对象。

                              (4).使用Statement对象提交SQL语句。

                              (5).操作结果集。

                             (6).回收数据库资源。

2.选定JDBC驱动程序类型 (1).LDBC-ODBC桥加ODBC 驱动程序

                                 (2).本地API和部分基于Java的驱动程

                                 (3).为数据库中间件实现的纯Java驱动程序

                                 (4).本地协议纯Java驱动程序(性能最好)

3.JDBC连接代码示例:

package com;



import java.io.IOException;

import java.io.InputStream;

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.Properties;



/**

 * JDBC辅助类

 * 

 * @author

 *

 */

public class JdbcUtil {



    /* 最好使用配置文件保存 */

    private static String driver; // 驱动

    private static String url; // 连接字符串

    private static String user; // 用户名

    private static String password; // 密码



    private static Connection conn; // Connection对象

    private static PreparedStatement pstmt; // PreparedStatement对象

    private static ResultSet rs; // ResultSet对象



    /* 加载数据库驱动 */

    static {

        loadProperties();

        try {

            Class.forName(driver);

        } catch (ClassNotFoundException e) {

            throw new RuntimeException(e);

        }

    }



    private JdbcUtil() {

    }



    /**

     * 读取配置文件

     */

    private static void loadProperties() {

        Properties properties = new Properties();

        InputStream in = Thread.currentThread().getClass()

                .getResourceAsStream("/com/lovo/day2/jdbc.properties");

        try {

            properties.load(in);

        } catch (IOException e) {

            throw new RuntimeException(e);

        }

        driver = properties.getProperty("driver");

        url = properties.getProperty("url");

        user = properties.getProperty("user");

        password = properties.getProperty("password");

    }



    /**

     * 获取数据库连接

     * 

     * @return 连接对象

     */

    public static Connection getConnection() {

        try {

            return conn = DriverManager.getConnection(url, user, password);

        } catch (SQLException e) {

            throw new RuntimeException(e);

        }

    }



    /**

     * 释放ResultSet资源

     * 

     * @param rs

     */

    public static void close(ResultSet rs) {

        try {

            if (null != rs)

                rs.close();

        } catch (SQLException e) {

            throw new RuntimeException(e);

        }

    }



    /**

     * 释放ResultSet资源

     * 

     * @param rs

     */

    public static void close(Statement stmt) {

        try {

            if (null != stmt)

                stmt.close();

        } catch (SQLException e) {

            throw new RuntimeException(e);

        }

    }



    /**

     * 释放ResultSet资源

     * 

     * @param rs

     */

    public static void close(Connection conn) {

        try {

            if (null != conn)

                conn.close();

        } catch (SQLException e) {

            throw new RuntimeException(e);

        }

    }



    /**

     * 释放所有资源

     */

    public static void close() {

        try {

            if (null != rs && !rs.isClosed()) {

                rs.close();

            }

            rs = null;

        } catch (SQLException e) {

            throw new RuntimeException(e);

        } finally {

            try {

                if (null != pstmt && !pstmt.isClosed()) {

                    pstmt.close();

                }

                pstmt = null;

            } catch (SQLException e) {

                throw new RuntimeException(e);

            } finally {

                try {

                    System.out.println(conn);

                    if (null != conn && !conn.isClosed()) {

                        conn.close();

                    }

                    conn = null;

                } catch (SQLException e) {

                    throw new RuntimeException(e);

                }

            }

        }

    }



    /**

     * 执行增删改

     * 

     * @param sql

     *            待执行的SQL语句

     * @param params

     *            SQL语句中?占位符的参数

     * @return 受影响的行数

     */

    public static int executeUpdate(String sql, Object... params) {

        try {

            if (null == conn)

                getConnection();

            pstmt = conn.prepareStatement(sql);



            if (null != params) { // 为SQL语句中?占位符设置参数

                for (int i = 0; i < params.length; i++) {

                    pstmt.setObject(i + 1, params[i]);

                }

            }



            return pstmt.executeUpdate();

        } catch (SQLException e) {

            throw new RuntimeException(e);

        }

    }



    /**

     * 实现查询

     * 

     * @param sql

     *            待执行的查询SQL语句

     * @param params

     *            SQL语句中?占位符的参数

     * @return 查询结果集ResultSet对象

     */

    public static ResultSet executeQuery(String sql, Object... params) {

        try {

            if (null == conn)

                getConnection();

            pstmt = conn.prepareStatement(sql);



            if (null != params) { // 为SQL语句中?占位符设置参数

                for (int i = 0; i < params.length; i++) {

                    pstmt.setObject(i + 1, params[i]);

                }

            }



            return rs = pstmt.executeQuery();

        } catch (SQLException e) {

            throw new RuntimeException(e);

        }

    }

}

你可能感兴趣的:(java)