JAVA內嵌數據庫H2的使用入門


H2數據庫是開源的,非常適合做嵌入式數據庫使用,尤其用java編碼的時候。

H2的優勢:
    1、h2采用純Java編寫,因此不受平台的限制。
    2、h2只有一個jar文件,十分適合作為嵌入式數據庫試用。
    3、h2提供了一個十分方便的web控制台用於操作和管理數據庫內容。

一、所需工具:

  JDK

  h2-1.4.x.jar

二、寫代碼如下:

package com.my.enter;

import java.sql.Connection;
import java.sql.SQLException;

import org.h2.jdbcx.JdbcConnectionPool;

public class ConnectionPool {
    private static ConnectionPool cp = null;
    private JdbcConnectionPool jdbcCP = null;

    private ConnectionPool() {
        String dbPath ="./config/test";
        jdbcCP = JdbcConnectionPool.create("jdbc:h2:" + dbPath, "sa", "");
        jdbcCP.setMaxConnections(50);
    }

    public static ConnectionPool getInstance() {
        if (cp == null) {
            cp = new ConnectionPool();
        }
        return cp;
    }

    public Connection getConnection() throws SQLException {
        return jdbcCP.getConnection();
    }
}

實例化時若數據庫test.mv.db不存在,則會創建,路徑是src的同級目錄config/test.mv.db;

三、使用數據庫:

package com.my.enter;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class CommonDao {

    public static void crateTable() throws SQLException {
        Connection conn = null;
        Statement stmt = null;
        try {
            conn = ConnectionPool.getInstance().getConnection();
            DatabaseMetaData meta = conn.getMetaData();

            ResultSet rsTables = meta.getTables(null, null, "WEATHERINFO",
                    new String[] { "TABLE" });
            if (!rsTables.next()) {
                stmt = conn.createStatement();
                stmt.execute("CREATE TABLE WEATHERINFO(WEATHERSTR VARCHAR(1024),LASTMODIFYTIME VARCHAR(1024),STATUS VARCHAR(1024),PRIMARY KEY(WEATHERSTR,LASTMODIFYTIME))");
            }
            rsTables.close();
        } finally {
            releaseConnection(conn, stmt, null);
        }
    }

    public static void addInfo(String str, long lastModifyTime,
            String status) throws SQLException {
        Connection conn = null;
        PreparedStatement stmt = null;
        try {
            conn = ConnectionPool.getInstance().getConnection();

            stmt = conn
                    .prepareStatement("INSERT INTO WEATHERINFO VALUES(?,?,?)");
            stmt.setString(1, str);
            stmt.setString(2, String.valueOf(lastModifyTime));
            stmt.setString(3, status);
            stmt.execute();

        } finally {
            releaseConnection(conn, stmt, null);
        }
    }

    public static boolean isInfoExits(String filePath, long lastModifyTime)
            throws SQLException {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            conn = ConnectionPool.getInstance().getConnection();
            stmt = conn
                    .prepareStatement("SELECT WEATHERSTR FROM WEATHERINFO WHERE STATUS=? AND LASTMODIFYTIME=?");
            stmt.setString(1, filePath);
            stmt.setString(2, String.valueOf(lastModifyTime));
            rs = stmt.executeQuery();
            return rs.next();
        } finally {
            releaseConnection(conn, stmt, rs);
        }
    }

    private static void releaseConnection(Connection conn, Statement stmt,
            ResultSet rs) throws SQLException {
        if (rs != null) {
            rs.close();
        }
        if (stmt != null) {
            stmt.close();
        }
        if (conn != null) {
            conn.close();
        }
    }

}

實現對數據庫的操作 包括建表,新增數據,查詢等操作;

以上,應該是入門了!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM