簡體   English   中英

在Struts + JSP中為下拉列表創建Mysql SELECT語句的邏輯是什么?

[英]What is the logic of creating the Mysql SELECT Statement for drop down in Struts + JSP?

你可以幫助我糾正下面的代碼,我正在嘗試在Eclipse中的struts 2中創建一個填充的下拉列表作為我的IDE。 這是我第一次使用'STRUTS'以及'IDE ECLIPSE'。 由SELECT語句具體來說,我不知道如何編寫代碼,當用戶選擇汽車的“Make”時,數據庫會提取該make的不同“模型”。 但是其他選擇項目(如“顏色”)應該是可選的,因為用戶可以繼續搜索“Make”減去從中選擇一個選項。 請幫助我成為ActionClass和DataBase的新手。 Thanx提前。

package drive;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.opensymphony.xwork2.ActionSupport;

public class CarSearch extends ActionSupport {

private String model;
private String modification;
private String engine;
private String color;
private String bodyType;
private String minPrice;
private String maxPrice;
private String mileage;
private int minYear;
private int maxYear;
private String make;

public String execute () {
    String ret = NONE;
    Connection conn = null;

    try {
        String URL = "jdbc:mysql://localhost/Cars";
        Class.forName("com.mysql.jdbc.Driver");
        conn = DriverManager.getConnection(URL, "root", "$jademedia247");
        String sql = "SELECT make FROM type WHERE";
        sql+=" model = ? AND modification = ? ";
        PreparedStatement ps = conn.prepareStatement (sql);
        ps.setString(1, model);
        ps.setString(2, modification);
        ResultSet rs = ps.executeQuery();

        while (rs.next()){
            make = rs.getString(1);
            ret = SUCCESS;
        }
    } catch (Exception e) {
        ret = ERROR;
    } finally { 
        if (conn != null) {
            try {
                conn.close();
            } catch (Exception e) {
            }
        }
    }
    return ret;
}

public String getModel() {
    return model;
}

public void setModel(String model) {
    this.model = model;
}

public String getModification() {
    return modification;
}

public void setModification (String modification) { 
    this.modification = modification;
}

public String getEngine() {
    return engine;
}

public void setEngine (String engine) {
    this.engine = engine;
}

public String getColor() {
    return color;
}

public void setColor (String color) {
    this.color = color;
}

public String getBodyType() {
    return bodyType;
}

public void setBodyType(String bodyType) {
    this.bodyType = bodyType;
}

public String getMinPrice() {
    return minPrice;
}

public void setMinPrice(String minPrice) {
    this.minPrice = minPrice;
}

public String getMaxPrice () {
    return maxPrice;
}

public void setMaxPrice (String maxPrice) {
    this.maxPrice = maxPrice;

}


public String getMileage () {
    return mileage;
}

public void setMileage (String mileage) {
    this.mileage = mileage ;
}

public int getMinYear() {
    return minYear;
}
public void setMinYear(int minYear) {
    this.minYear = minYear;
}

public int getMaxYear() {
    return maxYear;
}
public void setMaxYear(int maxYear) {
    this.maxYear = maxYear;
}

public String getMake() {
return make;
}
public void setMake(String make){
    this.make = make;
}
}
    PreparedStatement ps = conn.prepareStatement ("SELECT field_name FROM table_name WHERE model = ? AND modification = ? ");
    ps.setString(1, model);
    ps.setString(2, modification);
        ResultSet rs = ps.executeQuery(); 

//它會幫助你

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM