passing java string variable in mysql query

前端 未结 4 1974
时光取名叫无心
时光取名叫无心 2021-01-05 02:03

How to pass java string variable in sql query .I have done all the JDBC connection .

My sql database query is

sql = \"Select * 
       from productio         


        
相关标签:
4条回答
  • 2021-01-05 02:23
    String locationnames = "taplejung";
    
    String sql = "Select * from production AS cust INNER JOIN location AS comp ON cust.location_id = comp.location_id where comp.name ='"+ locationnames +"' AND crop_id =1";
    
    0 讨论(0)
  • 2021-01-05 02:27

    Use a PreparedStatement and bind the String parameter,

    final String sql = "select * from production AS cust INNER JOIN location"
        + " AS comp ON cust.location_id = comp.location_id where "
        + "comp.name = ? AND crop_id = 1";
    PreparedStatement ps = null;
    try {
      ps = conn.prepareStatement(sql);
      ps.setString(1, "taplejung");
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (ps != null) {
        try {
          ps.close();
        } catch (Exception ignored) {
        }
      }
    }
    

    Edit (Based on your additional code, change it to something like)

    PreparedStatement ps = null;
    
    String sql = null;
    if (cropnames.equals("paddy")) {
      // System.out.println();
      sql = "SELECT * FROM `production` AS cust INNER JOIN location AS comp "
          + "ON cust.location_id = comp.location_id WHERE comp.name = "
          + "? AND crop_id = 1";
    } else {
      sql = "SELECT * FROM `production` WHERE crop_id = 4 AND location_id = 10";
    }
    ps = conn.prepareStatement(sql);
    if (cropnames.equals("paddy")) {
      ps.setString(1, locationnames);
    }
    System.out.println(sql);
    ResultSet rs = ps.executeQuery();
    
    0 讨论(0)
  • 2021-01-05 02:37

    Passing variable is quiet simple in mysql query using java.

    • Write your query
    • and write the variable in ""
    • In my case i am passing 'conition' and 'tablename' dynamically.
    • Thank you very much have a good day.

      @Override public LinkedList getNameList(String condition, String tableName, String projectName) { // TODO Auto-generated method stub

      String query = "select distinct("+condition+") as name  from "+tableName+" ";
      
      //System.out.println(query);
      
      ResultSet rs = null;
      PreparedStatement preparedStatement = null;
      Connection connection = null;
      
      LinkedList finalList = new LinkedList();
      try{
          connection = dataSourceAbacus.getConnection();
          preparedStatement = connection.prepareStatement(query);
          rs= preparedStatement.executeQuery();
          while(rs.next()){
      
              finalList.add(rs.getString("name"));
          }
      }catch(Exception e){
          e.printStackTrace();
      }finally{
          if(connection !=null){
              try {
                  connection.close();
              } catch (SQLException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
          }
          if(preparedStatement != null){
              try{
                  preparedStatement.close();
              }catch(Exception e){
                  e.printStackTrace();
              }
          }
          if(rs != null){
              try{
                  rs.close();
              }catch(Exception e){
                  e.printStackTrace();
              }
          }
      }
      
      return finalList;
      

      }

    0 讨论(0)
  • 2021-01-05 02:47

    Whenever I have to make sql queries I use a library like jdbi to do it. This will allow you to create an interface with different queries. All you have to do is define the interface, create a POJO, and create a mapper between a SQL table and a Java POJO.

    The interface would look something like this.

    @RegisterMapper(ProductionMapper.class)
    public interface ProductionDAO {
         @SqlQuery("Select * from production AS cust INNER JOIN location AS comp ON cust.location_id = comp.location_id where comp.name = :name AND crop_id =1")
         Production findRow(@Bind("name") String name);
        } 
    

    The POJO would look something like this.

    public class Production {
       private VariableTypeA variableA;
       // other variables
       public Production(VariableTypeA variableA ....) {
          this.variableA = variableA;
          // set everything else
       }
       // getters and setters
    }
    

    The mapper would look something like this.

    public class ProductionMapper implements ResultSetMapper<Production> {
      public Production map(int index, ResultSet r, StatementContext ctx) throws SQLException {
        return new Production(r.getSomeType("columnName"), ...);
      }
    }
    

    This design makes it really simple to interact with your database and pass variables as well as making it so that your classes dont violate the SRP

    http://jdbi.org/sql_object_overview/

    0 讨论(0)
提交回复
热议问题