2017-02-23 42 views
0

我的方法目前选择所有的教师和他们的ID从数据base.In我selectDBT可变的,我要定义这样一种方式,即要求用户输入串并返回匹配用的ID从数据库

SELECT name, id from instructor where name like substring; 

并且这substring应该是用户可以输入的一些'%sub%'子字符串。

private static void selectInstructor() throws SQLException { 

     Connection dataConnection = null; 
     Statement statement = null; 

     String selectDBT = "SELECT name, id from instructor"; 

     try { 
      dataConnection = getConnection(); 
      statement = dataConnection.createStatement(); 

      System.out.println(selectDBT); 


      ResultSet result_set = statement.executeQuery(selectDBT); 

      while (result_set.next()) { 

       String ins_name = result_set.getString("name"); 
       String ins_id = result_set.getString("id"); 


       System.out.println("Instructor Name : " + ins_name); 
       System.out.println("Instructor ID : " + ins_id); 


      } 

     } catch (SQLException e) { 

      System.out.println(e.getMessage()); 

     } finally { 

      if (statement != null) { 
       statement.close(); 
      } 

      if (dataConnection != null) { 
       dataConnection.close(); 
      } 

     } 

    } 

到目前为止,我能够让用户把他们的用户名和密码用java swing界面输入数据库。现在我想创建另一个界面,用户可以输入substring,这应该参考一些%sub%字符串模式匹配,我的输出将是与子字符串匹配的名称和ID。现在我',获取所有名称和ID的无论如何,因为我不让用户输入子字符串匹配。也硬编码将无法正常工作,因为我希望用户动态选择。

编辑:用事先准备好的声明:

private static void selectInstructor() throws SQLException { 

     Connection dataConnection = null; 
     Statement statement = null; 





     // String selectDBT = "SELECT name, id from instructor"; 

     try { 
      dataConnection = getConnection(); 

PreparedStatement preparedStatement = 
     dataConnection.prepareStatement("SELECT name, id from instructor where name like ?"); 
     preparedStatement.setString(1, substring); 


      // statement = dataConnection.createStatement(); 

      System.out.println(preparedStatement); 


      ResultSet result_set = statement.executeQuery(preparedStatement); 

      while (result_set.next()) { 

       String ins_name = result_set.getString("name"); 
       String ins_id = result_set.getString("id"); 


       System.out.println("Instructor Name : " + ins_name); 
       System.out.println("Instructor ID : " + ins_id); 


      } 

     } catch (SQLException e) { 

      System.out.println(e.getMessage()); 

     } finally { 

      if (statement != null) { 
       statement.close(); 
      } 

      if (dataConnection != null) { 
       dataConnection.close(); 
      } 

     } 

    } 

但是,我得到相同的输出。请注意,我在SQL中非常新。我很乐意使用来自答案建议的准备好的语句。

+0

问题在哪里,您是否试图解决错误或想要知道如何实现? –

+0

在第二次尝试中,您需要使用'preparedStatement.executeQuery()',而不是'statement.executeQuery(preparedStatement)';那甚至不会编译。 –

回答

0

有运营商like你可以使用它像这样:

String selectDBT = "SELECT name, id from instructor like '" + sub + "'"; 
//... 

但是,这可以使错误的语法和worste SQL注入,也有一些聪明的用户,所以我建议使用PrepapredStatement,而不是:

PreparedStatement preparedStatement = 
     connection.prepareStatement("SELECT name, id from instructor where name like ?"); 
preparedStatement.setString(1, substring); 

这可以是更好的和更安全的。

编辑

你可以完成你的程序是这样的:

PreparedStatement preparedStatement = 
     connection.prepareStatement("SELECT name, id from instructor where name like ?"); 
preparedStatement.setString(1, substring); 

//get result from your prepapredStatement 
ResultSet result_set = preparedStatement.executeQuery(); 

while (result_set.next()) { 
    String ins_name = result_set.getString("name"); 
    String ins_id = result_set.getString("id"); 

    System.out.println("Instructor Name : " + ins_name); 
    System.out.println("Instructor ID : " + ins_id); 
} 
+0

老实说,我很新,我不明白你的意思。我看到你建议替换声明,但是你没有提到我需要在代码中做出的其他更改因此。 –

+0

第一个解决方案是否与您合作@HagaDhorse? –

+0

请参阅我尝试使用准备好的语句的编辑。但是,它给了我相同的输出,即所有名称和ID,并且不会提示我输入要搜索的字符串。 –

0

你可以写在数据库中的一个简单的程序;

CREATE OR REPLACE PROCEDURE get_instructor_cursor 
    (p_search_string IN VARCHAR2, 
    p_cursor_var  OUT SYS_REFCURSOR) AS 
BEGIN 
    OPEN p_cursor_var FOR 
    SELECT name, 
     id 
    FROM instructor 
    WHERE name like '%'||p_search_string||'%'; 

END Get_instructor_cursor; 

传入您的search_string并简单地通过返回的结果光标显示您的结果。具有可以从任何地方轻松地重新使用该搜索的优势。

相关问题