Created
November 21, 2017 22:32
-
-
Save juananpe/64e17b2cbfac7862f8a1e3e5b1e3e577 to your computer and use it in GitHub Desktop.
SQLIte JDBC Proba + JSON
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 datubasea; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
public class Proba { | |
public Proba() { | |
} | |
public static void main(String[] args) { | |
try { | |
new Proba().go2(); | |
} catch (SQLException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
private void go2() throws SQLException { | |
DBKudeatzaile dbk = DBKudeatzaile.getInstantzia(); | |
dbk.execSQL("CREATE TABLE IF NOT EXISTS json_test_table (id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR UNIQUE,json JSON)"); | |
dbk.execSQL("INSERT INTO json_test_table (name, json) VALUES ('test_name_2',json('{\"key_for_int\":2,\"key_for_str\":\"test_str2\",\"key_for_arr\":[5, \"e\", 6, \"f\"],\"key_for_obj\":{\"g\":7, \"h\":8},\"attr_b\":\"good_attr\"}'))"); | |
ResultSet rs = dbk.execSQL("select json_extract(json, '$.key_for_int') as zenbakia ," | |
+ " json_extract(json, '$.key_for_arr') as arraya from json_test_table"); | |
while(rs.next()) { | |
int zenbakia = rs.getInt("zenbakia"); | |
// String izena = rs.getString("name"); | |
String arraya = rs.getString("arraya"); | |
System.out.println("Zenbakia:"+zenbakia + "Arraya:" + arraya); | |
} | |
} | |
private void go() { | |
DBKudeatzaile dbk = DBKudeatzaile.getInstantzia(); | |
String query = "select animaliKodea, izena, ganadutegiaId, altuera, pisua, adarrarenLuzera " | |
+ " from Zezena"; | |
// String query = "SELECT name FROM sqlite_master WHERE type='table'"; | |
ResultSet rs = dbk.execSQL(query); | |
try { | |
while(rs.next()) { | |
int animaliKodea = rs.getInt("animaliKodea"); | |
String izena = rs.getString("izena"); | |
// String izena = rs.getString("name"); | |
System.out.println("Id:"+animaliKodea + " Izena:" + izena ); | |
} | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment