Created
March 28, 2025 06:50
-
-
Save vladdu/3622e6295b030a76b55b28de994e222a to your computer and use it in GitHub Desktop.
db testcontainer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dev.vlad.messenger; | |
import org.junit.jupiter.api.BeforeAll; | |
import org.junit.jupiter.api.Test; | |
import org.testcontainers.containers.Db2Container; | |
import org.testcontainers.images.builder.Transferable; | |
import org.testcontainers.junit.jupiter.Testcontainers; | |
import java.sql.*; | |
@Testcontainers | |
public class DbTests { | |
public static Db2Container db2; | |
@BeforeAll | |
public static void prepare() { | |
db2 = new Db2Container("ibmcom/db2:latest") | |
.acceptLicense().withUsername("db2inst1").withPassword("password") | |
.withDatabaseName("TESTDB") | |
.withInitScript("init.sql"); | |
} | |
@Test | |
public void someTestMethod() { | |
db2.start(); | |
String url = db2.getJdbcUrl(); | |
try { | |
// Load the driver | |
Class.forName("com.ibm.db2.jcc.DB2Driver"); | |
System.out.println("**** Loaded the JDBC driver"); | |
// Create the connection using the IBM Data Server Driver for JDBC and SQLJ | |
String user = "db2inst1"; | |
String password = "password"; | |
Connection con = DriverManager.getConnection(url, user, password); | |
// Commit changes manually | |
con.setAutoCommit(false); | |
System.out.println("**** Created a JDBC connection to the data source"); | |
// Create the Statement | |
Statement stmt; | |
stmt = con.createStatement(); | |
System.out.println("**** Created JDBC Statement object"); | |
// Execute a query and generate a ResultSet instance | |
ResultSet rs; | |
rs = stmt.executeQuery("SELECT EMPNO FROM EMPLOYEE"); | |
System.out.println("**** Created JDBC ResultSet object"); | |
// Print all of the employee numbers to standard output device | |
while (rs.next()) { | |
String empNo = rs.getString(1); | |
System.out.println("Employee number = " + empNo); | |
} | |
System.out.println("**** Fetched all rows from JDBC ResultSet"); | |
// Close the ResultSet | |
rs.close(); | |
System.out.println("**** Closed JDBC ResultSet"); | |
// Close the Statement | |
stmt.close(); | |
System.out.println("**** Closed JDBC Statement"); | |
// Connection must be on a unit-of-work boundary to allow close | |
con.commit(); | |
System.out.println("**** Transaction committed"); | |
// Close the connection | |
con.close(); | |
System.out.println("**** Disconnected from data source"); | |
System.out.println("**** JDBC Exit from class EzJava - no errors"); | |
} catch (ClassNotFoundException e) { | |
System.err.println("Could not load JDBC driver"); | |
System.out.println("Exception: " + e); | |
e.printStackTrace(); | |
} catch (SQLException ex) { | |
System.err.println("SQLException information"); | |
while (ex != null) { | |
System.err.println("Error msg: " + ex.getMessage()); | |
System.err.println("SQLSTATE: " + ex.getSQLState()); | |
System.err.println("Error code: " + ex.getErrorCode()); | |
ex.printStackTrace(); | |
ex = ex.getNextException(); // For drivers that support chained exceptions | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment