Advertisement
Guest User

Untitled

a guest
Feb 16th, 2017
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. package testJDBC;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9. import java.util.Date;
  10. import java.util.LinkedList;
  11. import java.util.List;
  12.  
  13. public class MySQLAccess {
  14. private Connection connect = null;
  15. private Statement statement = null;
  16. private PreparedStatement preparedStatement = null;
  17. private ResultSet resultSet = null;
  18.  
  19. public MySQLAccess() throws ClassNotFoundException, SQLException{
  20. Class.forName("com.mysql.jdbc.Driver");
  21. // Setup the connection with the DB
  22. connect = DriverManager.getConnection("jdbc:mysql://localhost/lector?user=root&password=root");
  23.  
  24. }
  25. public List<String> getYagoRelations(String subj,String obj) throws Exception {
  26. List<String>relations=new LinkedList<String>();
  27.  
  28. // This will load the MySQL driver, each DB has its own driver
  29.  
  30.  
  31. preparedStatement = connect.prepareStatement("select * from lector.yagofacts where subj='"+subj+"' and obj='"+obj+"';");
  32. resultSet = preparedStatement.executeQuery();
  33. while(resultSet.next())
  34. relations.add(resultSet.getString("rel"));
  35.  
  36.  
  37. return relations;
  38.  
  39.  
  40. }
  41.  
  42.  
  43. public void persistRelPhraseCount(String rel,String phrase) throws Exception {
  44.  
  45.  
  46.  
  47. preparedStatement = connect.prepareStatement("select exists(select * from lector.relPhraseCount where rel='"+rel+"' and phrase='"+phrase+"') as isPresent;");
  48. resultSet = preparedStatement.executeQuery();
  49.  
  50. resultSet.next();
  51.  
  52. String isPresent = resultSet.getString("isPresent");
  53.  
  54. if(isPresent.equals("1")){
  55.  
  56. preparedStatement = connect.prepareStatement("select sum as s from lector.relPhraseCount where rel='"+rel+"' and phrase='"+phrase+"';");
  57. resultSet = preparedStatement.executeQuery();
  58.  
  59. resultSet.next();
  60.  
  61. String sum=resultSet.getString("s");
  62.  
  63. Integer f=new Integer(sum);
  64. f++;
  65.  
  66. preparedStatement = connect.prepareStatement("update lector.relPhraseCount set sum='"+f.toString()+"' where rel='"+rel+"' and phrase='"+phrase+"';");
  67. preparedStatement.executeUpdate();
  68.  
  69. }
  70.  
  71. else{
  72. preparedStatement = connect.prepareStatement("insert into lector.relPhraseCount values('"+rel+"','"+phrase+"',1);");
  73. preparedStatement.execute();
  74. }
  75.  
  76.  
  77.  
  78. }
  79.  
  80.  
  81. // You need to close the resultSet
  82. private void close() {
  83. try {
  84. if (resultSet != null) {
  85. resultSet.close();
  86. }
  87.  
  88. if (statement != null) {
  89. statement.close();
  90. }
  91.  
  92. if (connect != null) {
  93. connect.close();
  94. }
  95. } catch (Exception e) {
  96.  
  97. }
  98. }
  99.  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement