在java准备语句中使用不同值的SQL select请求

在java准备语句中使用不同值的SQL select请求,java,eclipse,postgresql,model-view-controller,junit,Java,Eclipse,Postgresql,Model View Controller,Junit,大家好 public class CustomerDao extends DAO<Customer>{ private static final String SQL_SELECT_Plus = "SELECT * FROM customers WHERE lastname =' AND city ='"; public CustomerDao(Connection c) { super(c); } @Override public

大家好

public class CustomerDao extends DAO<Customer>{


 private static final String SQL_SELECT_Plus = "SELECT * FROM customers WHERE lastname =' AND city ='";



 public CustomerDao(Connection c) {
        super(c);
    }

 @Override
    public List<Customer> findByPlus(String lastname, String city) throws DAOException {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        ArrayList<Customer> customers = new ArrayList<Customer>();
        System.out.println("test1");
        try {
            System.out.println("test2");

            /* Récupération d'une connexion depuis la Factory */

           System.out.println("test32");
            preparedStatement = this.initRequest(this.connection, SQL_SELECT_Plus + lastname + "'" + city + "'", false);
            System.out.println("test3");

            resultSet = preparedStatement.executeQuery();
            System.out.println("test4");

            /* Parcours de la ligne de données de l'éventuel ResulSet retourné */
            while (resultSet.next()) {
                customers.add(map(resultSet));
                System.out.println("test5");

            }

        } catch (SQLException e) {
            throw new DAOException(e);
        } finally {
            System.out.println("EREUR");

            this.silentCloses(resultSet, preparedStatement, connection);
        }
        return customers;
        }
我使用的是JSP、mvc和JAVA,我试图用select*显示数据库中的信息,其中的where中有不同的值:“select*from customers,其中lastname='和city='

问题是,当我用JUnit测试这个请求时,我在WHERE中的第一个参数附近或处有一个错误

PS:如果我使用where子句中的一个参数执行select请求,它就会工作

你可以在下面找到我的代码

我的班级和请求:

public class CustomerDao extends DAO<Customer>{


 private static final String SQL_SELECT_Plus = "SELECT * FROM customers WHERE lastname =' AND city ='";



 public CustomerDao(Connection c) {
        super(c);
    }

 @Override
    public List<Customer> findByPlus(String lastname, String city) throws DAOException {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        ArrayList<Customer> customers = new ArrayList<Customer>();
        System.out.println("test1");
        try {
            System.out.println("test2");

            /* Récupération d'une connexion depuis la Factory */

           System.out.println("test32");
            preparedStatement = this.initRequest(this.connection, SQL_SELECT_Plus + lastname + "'" + city + "'", false);
            System.out.println("test3");

            resultSet = preparedStatement.executeQuery();
            System.out.println("test4");

            /* Parcours de la ligne de données de l'éventuel ResulSet retourné */
            while (resultSet.next()) {
                customers.add(map(resultSet));
                System.out.println("test5");

            }

        } catch (SQLException e) {
            throw new DAOException(e);
        } finally {
            System.out.println("EREUR");

            this.silentCloses(resultSet, preparedStatement, connection);
        }
        return customers;
        }
和JUnit错误:

public class AccountControllerTest {

   CustomerController customerController = new CustomerController();


    @Test
    public void shouldReturnErrorInModel() {
       customerController.selectAction4("Dupont","Paris");

    }
测试1测试2测试32测试3 EREUR fr.esipe.ing2.esibank.dao.DAOException:>org.postgresql.util.PSQLException:错误:语法错误位于或接近> “杜邦”位置:54在fr.esipe.ing2.esibank.customer.dao.CustomerDao.findByPlus(CustomerDao.java:164) 在 fr.esipe.ing2.esibank.customer.controller.CustomerController.selectAction4(CustomerController.java:97) 在 fr.esipe.ing2.esibank.account.AccountControllerTest.shouldReturnErrorInModel(AccountControllerTest.java:15) 位于的sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法) 位于的sun.reflect.NativeMethodAccessorImpl.invoke(未知源) sun.reflect.DelegatingMethodAccessorImpl.invoke(未知源)位于 java.lang.reflect.Method.invoke(未知源代码)位于 org.junit.runners.model.FrameworkMethod$1.runReflectVeCall(FrameworkMethod.java:50) 在 org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 在 org.junit.runners.model.FrameworkMethod.invokeeexplosive(FrameworkMethod.java:47) 在 org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 位于org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) 在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) 位于org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)位于 org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)位于 org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)位于 org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)位于 org.junit.runners.ParentRunner.run(ParentRunner.java:363)位于 org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 在 org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) 原因:org.postgresql.util.psql异常:错误:语法错误 或接近“杜邦”位置:54 at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2270) 在 org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1998) 在 org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255) 在 org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:570) 在 org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:420) 在 org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:305)位于 fr.esipe.ing2.esibank.customer.dao.CustomerDao.findByPlus(CustomerDao.java:153) ... 25多


字符串连接会产生一条SQL语句:“SELECT*FROM customers,其中lastname=”和city='Dupont'Paris'”

决不应该对SQL执行字符串连接。SQL注入就是这样发生的

您应该在SQL字符串中使用参数

String query = "SELECT * FROM customers WHERE lastname=:lastName AND city=:city";
NamedParameterStatement ps = new NamedParameterStatement(connection, query);
ps.setString("lastName", lastname);
ps.setString("city", city);

字符串连接会产生一条SQL语句:“SELECT*FROM customers,其中lastname=”和city='Dupont'Paris'”

决不应该对SQL执行字符串连接。SQL注入就是这样发生的

您应该在SQL字符串中使用参数

String query = "SELECT * FROM customers WHERE lastname=:lastName AND city=:city";
NamedParameterStatement ps = new NamedParameterStatement(connection, query);
ps.setString("lastName", lastname);
ps.setString("city", city);

嗨,谢谢你的快速回答。实际上,我的目标是通过在搜索栏中键入一些内容来进行搜索,这就是为什么我的where是这样的。我在JUnit测试中加入了“Dupont”、“Paris”,以确保selectAction正常工作。preparedStatement=this.initRequest(this.connection,SQL\u SELECT\u Plus+lastname+“'”+city+“'”,false);用户将输入姓氏和城市。确定。不会改变我的答案。使用准备好的语句参数。永远不要使用字符串连接。阅读更多内容:当我尝试按照您告诉我的方式执行操作时,方法setString出现此错误,类型PreparedStatement中的方法setString(int,String)不适用于参数(String,String)。对不起,我不是java方面的专家……对不起,我的语法错了。请阅读我发布的文章链接,它详细解释了它。好的,没问题,我会检查它。但是通过使用连接,我需要改变什么才能使它工作?我只是想测试我的搜索是否可以使用不同的参数:)。您好,谢谢您的快速回答。实际上,我的目标是通过在搜索栏中键入一些内容来进行搜索,这就是为什么我的where是这样的。我在JUnit测试中加入了“Dupont”、“Paris”,以确保selectAction正常工作。preparedStatement=this.initRequest(this.connection,SQL\u SELECT\u Plus+lastname+“'”+city+“'”,false);用户将输入姓氏和城市。确定。不会改变我的答案。使用准备好的语句参数。永远不要使用字符串连接。阅读更多:当我试图按照你告诉我的去做时,我有这个错误