|
| 1 | +# Setting Up SQLite + JDBC on macOS (Apple Silicon) with IntelliJ. |
| 2 | + |
| 3 | +This is a clear, practical guide for setting up **SQLite CLI tools** and the **Xerial ****\`\`**** driver** on an **Apple Silicon Mac** with **IntelliJ IDEA**. It is written to be copy-paste friendly, including for project paths that contain spaces (like `Java SE`). |
| 4 | + |
| 5 | +> Target audience: macOS (Apple Silicon / arm64) users running Java 11+ with IntelliJ IDEA (Community or Ultimate). |
| 6 | +
|
| 7 | +--- |
| 8 | + |
| 9 | +## Overview |
| 10 | + |
| 11 | +By the end of this guide you will have: |
| 12 | + |
| 13 | +- SQLite **command-line tools** (arm64 precompiled binaries) in your project folder for direct database inspection. |
| 14 | +- The **sqlite-jdbc** JAR added to your IntelliJ module so Java can connect to SQLite. |
| 15 | +- A working Java test that creates `test.db`, inserts data, and reads it back. |
| 16 | +- Tips to prevent duplicate inserts, run in-memory tests, and silence JDK 24 native-access warnings. |
| 17 | + |
| 18 | +--- |
| 19 | + |
| 20 | +## Prerequisites |
| 21 | + |
| 22 | +- macOS on **Apple Silicon** (M1/M2/M3). If you are on Intel, use the x64 binaries instead. |
| 23 | +- Java JDK 11+ (JDK 17 or 21 recommended; JDK 24 is supported with an extra VM option). |
| 24 | +- IntelliJ IDEA installed. |
| 25 | + |
| 26 | +Project directory example used throughout: |
| 27 | + |
| 28 | +```bash |
| 29 | +PROJECT_DIR="/Users/somesh/Java SE/JavaEvolution-Learning-Growing-Mastering/Section28JDBCusingSQLite" |
| 30 | +``` |
| 31 | + |
| 32 | +> Since this path contains spaces, commands are quoted. |
| 33 | +
|
| 34 | +--- |
| 35 | + |
| 36 | +## 1. Install SQLite CLI Tools (ARM64) |
| 37 | + |
| 38 | +Download the arm64 precompiled binaries (e.g. `sqlite-tools-osx-arm64-3500400.zip`) from the [SQLite downloads page](https://www.sqlite.org/download.html). |
| 39 | + |
| 40 | +Unzip, and you should see: |
| 41 | + |
| 42 | +- `sqlite3` |
| 43 | +- `sqldiff` |
| 44 | +- `sqlite3_analyzer` |
| 45 | +- `sqlite3_rsync` |
| 46 | + |
| 47 | +--- |
| 48 | + |
| 49 | +## 2. Add CLI Tools to Your Project |
| 50 | + |
| 51 | +Copy the tools into your project: |
| 52 | + |
| 53 | +```bash |
| 54 | +cd "$PROJECT_DIR" |
| 55 | +mkdir -p sqlite-tools |
| 56 | +cp -R ~/Downloads/sqlite-tools-osx-arm64-3500400/* sqlite-tools/ |
| 57 | +``` |
| 58 | + |
| 59 | +Make them executable and verify: |
| 60 | + |
| 61 | +```bash |
| 62 | +cd "$PROJECT_DIR/sqlite-tools" |
| 63 | +chmod +x sqlite3 sqldiff sqlite3_analyzer sqlite3_rsync || true |
| 64 | +"$PROJECT_DIR/sqlite-tools/sqlite3" --version |
| 65 | +``` |
| 66 | + |
| 67 | +--- |
| 68 | + |
| 69 | +## 3. Download the JDBC Driver |
| 70 | + |
| 71 | +Get the Xerial `sqlite-jdbc` JAR (example version `3.50.3.0`): |
| 72 | + |
| 73 | +```bash |
| 74 | +cd "$PROJECT_DIR" |
| 75 | +curl -L -o sqlite-jdbc-3.50.3.0.jar "https://downloads.sourceforge.net/project/sqlite-jdbc-driver.mirror/3.50.3.0/sqlite-jdbc-3.50.3.0.jar" |
| 76 | +ls -lh sqlite-jdbc-3.50.3.0.jar |
| 77 | +``` |
| 78 | + |
| 79 | +--- |
| 80 | + |
| 81 | +## 4. Add the JAR in IntelliJ |
| 82 | + |
| 83 | +1. Open IntelliJ → `File` → `Project Structure...` (⌘;). |
| 84 | +2. Under **Modules**, select your module. |
| 85 | +3. Open the **Dependencies** tab → click `+` → **JARs or Directories...**. |
| 86 | +4. Choose `sqlite-jdbc-3.50.3.0.jar`. |
| 87 | +5. Set Scope = **Compile**. Apply → OK. |
| 88 | + |
| 89 | +--- |
| 90 | + |
| 91 | +## 5. Mark `src` as Source Root |
| 92 | + |
| 93 | +In Project Structure → Modules → **Sources** tab, select your `src` folder and click **Mark as Sources** (blue folder). IntelliJ will now compile your `.java` files. |
| 94 | + |
| 95 | +--- |
| 96 | + |
| 97 | +## 6. Example Java Test Program |
| 98 | + |
| 99 | +Place `SqliteJdbcTest.java` inside `src/`: |
| 100 | + |
| 101 | +```java |
| 102 | +import java.nio.file.Path; |
| 103 | +import java.nio.file.Paths; |
| 104 | +import java.sql.*; |
| 105 | + |
| 106 | +public class SqliteJdbcTest { |
| 107 | + public static void main(String[] args) { |
| 108 | + Path dbPath = Paths.get("/Users", "somesh", "Java SE", "JavaEvolution-Learning-Growing-Mastering", "Section28JDBCusingSQLite", "test.db"); |
| 109 | + String url = "jdbc:sqlite:" + dbPath; |
| 110 | + |
| 111 | + System.out.println("JDBC URL: " + url); |
| 112 | + |
| 113 | + try { |
| 114 | + Class.forName("org.sqlite.JDBC"); |
| 115 | + try (Connection conn = DriverManager.getConnection(url)) { |
| 116 | + System.out.println("Connected (driver: " + conn.getMetaData().getDriverName() + ")"); |
| 117 | + try (Statement st = conn.createStatement()) { |
| 118 | + st.executeUpdate("CREATE TABLE IF NOT EXISTS student (id INTEGER PRIMARY KEY, name TEXT);"); |
| 119 | + st.executeUpdate("INSERT INTO student(name) VALUES('Diwan');"); |
| 120 | + try (ResultSet rs = st.executeQuery("SELECT id, name FROM student;")) { |
| 121 | + while (rs.next()) { |
| 122 | + System.out.println("row -> id: " + rs.getInt("id") + ", name: " + rs.getString("name")); |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + } catch (Exception e) { |
| 128 | + e.printStackTrace(); |
| 129 | + } |
| 130 | + } |
| 131 | +} |
| 132 | +``` |
| 133 | + |
| 134 | +--- |
| 135 | + |
| 136 | +## 7. Run from Terminal |
| 137 | + |
| 138 | +```bash |
| 139 | +cd "$PROJECT_DIR" |
| 140 | +javac src/SqliteJdbcTest.java |
| 141 | +java -cp ".:sqlite-jdbc-3.50.3.0.jar" SqliteJdbcTest |
| 142 | +``` |
| 143 | + |
| 144 | +Expected output: connection success + printed rows. |
| 145 | + |
| 146 | +--- |
| 147 | + |
| 148 | +## 8. Run in IntelliJ |
| 149 | + |
| 150 | +1. Run → Edit Configurations... → `+` → Application. |
| 151 | +2. Name: `SqliteJdbcTest`. |
| 152 | +3. Main class: `SqliteJdbcTest`. |
| 153 | +4. Use classpath of module: your module name. |
| 154 | +5. Working directory: `$PROJECT_DIR`. |
| 155 | + |
| 156 | +Run and confirm output matches. |
| 157 | + |
| 158 | +--- |
| 159 | + |
| 160 | +## 9. Handle JDK 24 Warnings |
| 161 | + |
| 162 | +Add this VM option to silence restricted method warnings: |
| 163 | + |
| 164 | +``` |
| 165 | +--enable-native-access=ALL-UNNAMED |
| 166 | +``` |
| 167 | + |
| 168 | +Add via Run Configurations → Modify options → VM options. |
| 169 | + |
| 170 | +--- |
| 171 | + |
| 172 | +## 10. Inspect Database with CLI |
| 173 | + |
| 174 | +```bash |
| 175 | +"$PROJECT_DIR/sqlite-tools/sqlite3" test.db ".tables" |
| 176 | +"$PROJECT_DIR/sqlite-tools/sqlite3" test.db "SELECT * FROM student;" |
| 177 | +"$PROJECT_DIR/sqlite-tools/sqlite3" test.db ".schema" |
| 178 | +``` |
| 179 | + |
| 180 | +--- |
| 181 | + |
| 182 | +## 11. Prevent Duplicate Inserts |
| 183 | + |
| 184 | +**Option A — UNIQUE + OR IGNORE:** |
| 185 | + |
| 186 | +```sql |
| 187 | +CREATE TABLE IF NOT EXISTS student (id INTEGER PRIMARY KEY, name TEXT UNIQUE); |
| 188 | +INSERT OR IGNORE INTO student(name) VALUES('Diwan'); |
| 189 | +``` |
| 190 | + |
| 191 | +**Option B — UPSERT:** |
| 192 | + |
| 193 | +```sql |
| 194 | +INSERT INTO student(name) VALUES('Diwan') ON CONFLICT(name) DO NOTHING; |
| 195 | +``` |
| 196 | + |
| 197 | +**Option C — Check in Java:** (Use `SELECT COUNT(*)` before inserting.) |
| 198 | + |
| 199 | +--- |
| 200 | + |
| 201 | +## 12. Reset the Test DB |
| 202 | + |
| 203 | +Delete file: |
| 204 | + |
| 205 | +```bash |
| 206 | +rm -f "$PROJECT_DIR/test.db" |
| 207 | +``` |
| 208 | + |
| 209 | +Or drop table: |
| 210 | + |
| 211 | +```bash |
| 212 | +"$PROJECT_DIR/sqlite-tools/sqlite3" test.db "DROP TABLE IF EXISTS student;" |
| 213 | +``` |
| 214 | + |
| 215 | +--- |
| 216 | + |
| 217 | +## 13. JUnit 5 In-Memory Tests |
| 218 | + |
| 219 | +Use `jdbc:sqlite::memory:` to avoid touching disk. |
| 220 | + |
| 221 | +```java |
| 222 | +import org.junit.jupiter.api.*; |
| 223 | +import java.sql.*; |
| 224 | +import static org.junit.jupiter.api.Assertions.*; |
| 225 | + |
| 226 | +class SqliteInMemoryTest { |
| 227 | + private Connection conn; |
| 228 | + |
| 229 | + @BeforeEach |
| 230 | + void openConnection() throws Exception { |
| 231 | + Class.forName("org.sqlite.JDBC"); |
| 232 | + conn = DriverManager.getConnection("jdbc:sqlite::memory:"); |
| 233 | + } |
| 234 | + |
| 235 | + @AfterEach |
| 236 | + void closeConnection() throws SQLException { |
| 237 | + if (conn != null && !conn.isClosed()) conn.close(); |
| 238 | + } |
| 239 | + |
| 240 | + @Test |
| 241 | + void createAndQuery() throws SQLException { |
| 242 | + try (Statement st = conn.createStatement()) { |
| 243 | + st.executeUpdate("CREATE TABLE student (id INTEGER PRIMARY KEY, name TEXT);"); |
| 244 | + st.executeUpdate("INSERT INTO student(name) VALUES('Alice');"); |
| 245 | + try (ResultSet rs = st.executeQuery("SELECT id, name FROM student")) { |
| 246 | + assertTrue(rs.next()); |
| 247 | + assertEquals("Alice", rs.getString("name")); |
| 248 | + } |
| 249 | + } |
| 250 | + } |
| 251 | +} |
| 252 | +``` |
| 253 | + |
| 254 | +--- |
| 255 | + |
| 256 | +## 14. Add sqlite-tools to PATH (Optional) |
| 257 | + |
| 258 | +Edit `~/.zshrc`: |
| 259 | + |
| 260 | +```bash |
| 261 | +export PATH="/Users/somesh/Java SE/JavaEvolution-Learning-Growing-Mastering/Section28JDBCusingSQLite/sqlite-tools:$PATH" |
| 262 | +``` |
| 263 | + |
| 264 | +Reload: |
| 265 | + |
| 266 | +```bash |
| 267 | +source ~/.zshrc |
| 268 | +sqlite3 --version |
| 269 | +``` |
| 270 | + |
| 271 | +--- |
| 272 | + |
| 273 | +## 15. Troubleshooting |
| 274 | + |
| 275 | +- **No suitable driver found:** Ensure JAR is on classpath or added to module dependencies. |
| 276 | +- **ClassNotFoundException: org.sqlite.JDBC:** Wrong or missing jar. |
| 277 | +- **UnsatisfiedLinkError:** Use Xerial driver and supported JDK. |
| 278 | +- **Permission errors:** Check file ownership/permissions. |
| 279 | +- **Paths with spaces:** Quote in shell; use `Paths.get(...)` in Java. |
| 280 | +- **JDK 24 warnings:** Add `--enable-native-access=ALL-UNNAMED`. |
| 281 | + |
| 282 | +--- |
| 283 | + |
| 284 | +*Happy coding and reproducible builds!* |
0 commit comments