Advertisement
mixeila

Untitled

Apr 21st, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 KB | None | 0 0
  1. package assign3;
  2.  
  3.  
  4. import java.io.InputStreamReader;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9.  
  10. import javax.swing.table.AbstractTableModel;
  11.  
  12. import com.mysql.jdbc.Statement;
  13.  
  14. public class MyDB extends AbstractTableModel{
  15.  
  16. private static String user = MyDBInfo.MYSQL_USERNAME;
  17. private static String password = MyDBInfo.MYSQL_PASSWORD;
  18. private static String server = MyDBInfo.MYSQL_DATABASE_SERVER;
  19. private static String database = MyDBInfo.MYSQL_DATABASE_NAME;
  20.  
  21. private Statement state;
  22. private ResultSet set;
  23.  
  24. public MyDB() {
  25. getConnectionStatement();
  26. //InputStreamReader reader = new InputStreamReader(metropolises.sql);
  27. }
  28.  
  29. private void getConnectionStatement() {
  30. try {
  31. Class.forName("com.mysql.jdbc.Driver");
  32. try {
  33. Connection con = DriverManager.getConnection("jdbc:mysql://"+server, user, password);
  34. state = (Statement) con.createStatement();
  35. state.executeQuery("use "+database);
  36. } catch (SQLException e) {
  37. e.printStackTrace();
  38. }
  39. } catch (ClassNotFoundException e) {
  40. e.printStackTrace();
  41. }
  42. }
  43.  
  44. public static final char GREATER = '>';
  45. public static final char LESS = '<';
  46. public static final char EQUAL = '=';
  47. public static final int EMPTY_POPULATION = -1;
  48.  
  49.  
  50.  
  51. public void search(String metropolis, String continent, int population, char greater, boolean exact) {
  52. String selectString = getSelectString(metropolis, continent, population, greater, exact);
  53. try {
  54. set = state.executeQuery(selectString);
  55. fireTableDataChanged();
  56. } catch (SQLException e) {
  57. e.printStackTrace();
  58. }
  59. }
  60.  
  61. private String getSelectString(String metropolis, String continent, int population, char greater, boolean exact) {
  62. String ex = "%";
  63. if(exact) ex = "";
  64. if(metropolis.length()==0) {
  65. metropolis = "metropolis";
  66. }else metropolis = "\""+ex+metropolis+ex+"\"";
  67. if(continent.length()==0) {
  68. continent = "continent";
  69. }else continent = "\""+ex+continent+ex+"\"";
  70. String result = "select * from metropolises where metropolis like "+metropolis+" and continent like "+continent;
  71. if(population!=EMPTY_POPULATION)
  72. result += " and population"+greater+population;
  73. return result;
  74. }
  75.  
  76. public void add(String metropolis, String continent, int population) {
  77. String insertString = "insert into metropolises values("+"\""+metropolis+"\","+"\""+continent+"\","+population+")";
  78. try {
  79. state.executeUpdate(insertString);
  80. search(metropolis, continent, population, MyDB.EQUAL, true);
  81. } catch (SQLException e) {
  82. e.printStackTrace();
  83. }
  84. }
  85.  
  86. public static void main(String[] args) {
  87. // MyDB db = new MyDB();
  88. // search("mumbai", "Asia", 5, '0', false);
  89. // try {
  90. // result.first();
  91. // } catch (SQLException e) {
  92. // e.printStackTrace();
  93. // }
  94. // try {
  95. // System.out.println(result.getString(2));
  96. System.out.println("dradadam");
  97. // } catch (SQLException e) {
  98. // e.printStackTrace();
  99. // }
  100. // db.add("xashuri", "chixirtmuli", 88);
  101. //
  102. }
  103.  
  104. @Override
  105. public Object getValueAt(int rowIndex, int columnIndex) {
  106. Object result = null;
  107. try {
  108. set.absolute(rowIndex+1);
  109. result = set.getObject(columnIndex+1);
  110. } catch (SQLException e) {
  111. // TODO Auto-generated catch block
  112. e.printStackTrace();
  113. }
  114. return result;
  115. }
  116.  
  117. @Override
  118. public int getRowCount() {
  119. int result = 0;
  120. try {
  121. if(set != null) {
  122. set.last();
  123. result = set.getRow();
  124. }
  125. } catch (SQLException e) {
  126. e.printStackTrace();
  127. }
  128. return result;
  129. }
  130.  
  131. private static final int COLUMN_COUNT = 3;
  132. @Override
  133. public int getColumnCount() {
  134. int result = COLUMN_COUNT;
  135. if(set!=null) {
  136. try {
  137. result = set.getMetaData().getColumnCount();
  138. } catch (SQLException e) {
  139. // TODO Auto-generated catch block
  140. e.printStackTrace();
  141. }
  142. }
  143. return result;
  144.  
  145. }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement