Advertisement
aunkang

findRes

Apr 19th, 2015
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.55 KB | None | 0 0
  1. public String[] findRes(String nameRes) {
  2.         try {
  3.             Class.forName("com.mysql.jdbc.Driver");
  4.             String name = "it_56070014";
  5.             String password = "4a3DskLl2b";
  6.             String url = "jdbc:mysql://database.it.kmitl.ac.th:3306/it_56070014?zeroDateTimeBehavior=convertToNull";
  7.             Connection conn;
  8.             conn = DriverManager.getConnection(url, name, password);
  9.             if (nameRes.equals("")) {
  10.                 String res[] = new String[1000];
  11.                 int i = 0;
  12.                 Statement stmt = conn.createStatement();
  13.                 String sql = "SELECT research_name from researchs";
  14.                 ResultSet rs = stmt.executeQuery(sql);
  15.                 while (rs.next()) {
  16.                     res[i] = rs.getString("research_name");
  17.                     i++;
  18.                 }
  19.                 return res;
  20.             } else {
  21.                 String words;
  22.                 Statement stmt = conn.createStatement();
  23.                 String sql = "SELECT research_name from researchs where research_name like '%" +nameRes+ "%'"  ;
  24.                 ResultSet rs = stmt.executeQuery(sql);              
  25.                 int count = 0;
  26.                 String wordsAll[] = new String[100];
  27.                 while (rs.next()) {
  28.                     wordsAll[count] = rs.getString("research_name");  
  29.                     count++;
  30.                 }
  31.                 return wordsAll;
  32.             }
  33.         } catch (Exception e) {
  34.             e.printStackTrace();
  35.         }
  36.         return new String[0];
  37.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement