Advertisement
Guest User

Untitled

a guest
Oct 3rd, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.06 KB | None | 0 0
  1. @Test
  2. public void DatabaseTable() {
  3. setUp();
  4.  
  5. String f = "/tmp/test.db";
  6. db.createNewEmptyDbFile(f);
  7. Connection dbConn = db.establishConnection("jdbc:sqlite:", f);
  8. [...]
  9. }
  10.  
  11. package voc;
  12.  
  13. import org.junit.Test;
  14.  
  15. import java.sql.Connection;
  16. import java.util.ArrayList;
  17.  
  18. import static org.junit.Assert.assertEquals;
  19. import static org.junit.Assert.assertFalse;
  20.  
  21. public class DatabaseTest {
  22.  
  23. private Database db = null;
  24.  
  25. public void setUp() {
  26. db = null;
  27. db = new Database();
  28. }
  29.  
  30. @Test
  31. public void DatabaseTable() {
  32. setUp();
  33.  
  34. String f = "/tmp/test.db";
  35. db.createNewEmptyDbFile(f);
  36. Connection dbConn = db.establishConnection("jdbc:sqlite:", f);
  37.  
  38. db.createBasicSqlTable(dbConn);
  39.  
  40. ArrayList<String> SQLTableNames = db.showTables(dbConn);
  41. assertFalse(SQLTableNames.isEmpty());
  42.  
  43. String FirstSQLTable = SQLTableNames.get(0);
  44. assertEquals(FirstSQLTable, "vocabulary");
  45. }
  46.  
  47. @Test
  48. public void TestDataInsertion() {
  49. setUp();
  50.  
  51. String f = "/tmp/test.db";
  52. db.createNewEmptyDbFile(f);
  53. Connection dbConn = db.establishConnection("jdbc:sqlite:", f);
  54.  
  55. db.createBasicSqlTable(dbConn);
  56.  
  57. db.insertWordPairIntoTable(dbConn,"time", "zeit");
  58. assertEquals(db.getGermanTranslation("time"), "zeit");
  59. }
  60. }
  61.  
  62. package voc;
  63.  
  64. import java.io.File;
  65. import java.sql.*;
  66. import java.util.ArrayList;
  67.  
  68. public class Database {
  69. private PreparedStatement pstmt = null;
  70. private Connection conn = null;
  71.  
  72. public void createNewEmptyDbFile(String DBfilename) {
  73. try {
  74. File file = new File(DBfilename);
  75.  
  76. } catch (Exception e) {
  77. throw new RuntimeException("Cannot create a new database file.",e);
  78. }
  79. }
  80.  
  81. public Connection establishConnection(String databaseType, String DBfilename) {
  82. try {
  83. // create database connection
  84. conn = DriverManager.getConnection(databaseType + DBfilename);
  85.  
  86. return conn;
  87.  
  88. } catch (SQLException e) {
  89. throw new RuntimeException("Cannot create a connection to the SQL database.",e);
  90. }
  91. }
  92.  
  93.  
  94. public void createBasicSqlTable(Connection pConn) {
  95. try {
  96. pstmt = pConn.prepareStatement("create table vocabulary (english string, german string);");
  97. pstmt.executeUpdate();
  98.  
  99. } catch (SQLException e) {
  100. throw new RuntimeException("Cannot create a basic SQL Table.", e);
  101. }
  102. }
  103.  
  104. /**
  105. * @returns the names of the SQL tables.
  106. */
  107. public ArrayList<String> showTables(Connection pConn) {
  108. try {
  109. pstmt = pConn.prepareStatement("SELECT name FROM sqlite_master WHERE type='table';");
  110. ResultSet rs = pstmt.executeQuery();
  111.  
  112. pstmt.executeUpdate();
  113.  
  114. ArrayList<String> SQLTableNames = new ArrayList<String>();
  115. while (rs.next())
  116. {
  117. SQLTableNames.add(rs.getString(1));
  118. }
  119.  
  120. return SQLTableNames;
  121.  
  122. } catch (SQLException e) {
  123. throw new RuntimeException("SQL command to select all tables failed", e);
  124. }
  125. }
  126.  
  127. /**
  128. * @param pConn: database connection object
  129. * @param englishVoc : given word we want to learn
  130. * @param germanTranslation : translation of the given word
  131. */
  132. public void insertWordPairIntoTable(Connection pConn, String englishVoc, String germanTranslation) {
  133. try {
  134. pstmt = pConn.prepareStatement("insert into vocabulary values(?,?);");
  135. pstmt.setString(1,englishVoc);
  136. pstmt.setString(2,germanTranslation);
  137.  
  138. pstmt.executeUpdate();
  139.  
  140. } catch (SQLException e) {
  141. throw new RuntimeException("Cannot insert word pair into SQL Database.", e);
  142. }
  143. }
  144.  
  145. /**
  146. * @param pEnglishVoc: english word we want to learn (1st column of db)
  147. * @return the german translation of the parameter (2nd column of db)
  148. */
  149. public String getGermanTranslation(String pEnglishVoc) {
  150. ResultSet rs;
  151. try {
  152. pstmt.executeQuery("select german from vocabulary where english=?;");
  153. pstmt.setString(1, pEnglishVoc);
  154.  
  155. rs = pstmt.executeQuery();
  156. return rs.getString(1);
  157.  
  158. } catch (SQLException e) {
  159. throw new RuntimeException("Cannot get german translation for given english word.", e);
  160. }
  161. }
  162. }
  163.  
  164. package voc;
  165.  
  166. import org.apache.commons.csv.CSVFormat;
  167. import org.apache.commons.csv.CSVParser;
  168. import org.apache.commons.csv.CSVRecord;
  169.  
  170. import java.io.IOException;
  171. import java.net.MalformedURLException;
  172. import java.net.URL;
  173. import java.nio.charset.Charset;
  174. import java.sql.Connection;
  175.  
  176. import static java.nio.charset.Charset.forName;
  177.  
  178. public class Control {
  179.  
  180. private Database theDB;
  181.  
  182. public Control() {
  183. theDB = new Database();
  184.  
  185.  
  186. theDB.createNewEmptyDbFile("database.db");
  187. Connection dbConn = theDB.establishConnection("jdbc:sqlite:", "database.db");
  188.  
  189. theDB.createBasicSqlTable(dbConn);
  190.  
  191. this.insertVocabularyListIntoDB(dbConn);
  192. }
  193.  
  194.  
  195.  
  196.  
  197. public void insertVocabularyListIntoDB(Connection pConn) {
  198. URL url = null;
  199.  
  200. try {
  201. url = new URL("https://raw.githubusercontent.com/toogley/voc-data/master/technology_and_society");
  202. } catch (MalformedURLException e) {
  203. throw new RuntimeException("URL of VocabularyList(" + url +") is malformed", e);
  204. }
  205.  
  206. // CSVParser.parse(url,...) needs a Charset object
  207. Charset charset = forName("UTF-8");
  208.  
  209. CSVParser parser = null;
  210. try {
  211. parser = CSVParser.parse(url, charset, CSVFormat.RFC4180);
  212. } catch (IOException e) {
  213. throw new RuntimeException("Failed to read from" + url, e);
  214. }
  215.  
  216. for (CSVRecord csvRecord : parser) {
  217. String englishWord = csvRecord.get(0);
  218. String germanWord = csvRecord.get(1);
  219.  
  220. theDB.insertWordPairIntoTable(pConn, englishWord, germanWord);
  221. }
  222. }
  223.  
  224.  
  225. public static void main(String[] args) {
  226. new Control();
  227. }
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement