У меня есть страница jsf (jsf + primefaces 4.0). На странице есть элемент ввода текста и элемент кнопки команды.Поиск по разделам mysql
Вот мой JSF файл:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
>
<h:head>
<title>Keresés</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link rel="stylesheet" type="text/css" href="style.css"/>
</h:head>
<ui:debug hotkey="1" />
<h:form>
<h:panelGrid columns="2">
<h:outputText value="ID:"/>
<p:inputText value="#{Kereses.id}" required="true" maxlength="20"/>
<p:commandButton id="addCategoryButton" value="Kereses" onclick="categoryDialog.show();" action="#{Kereses.selectTesztTable}"/>
</h:panelGrid>
<p:dialog id="categoryDialog" header="Category Detail" widgetVar="categoryDialog" closeOnEscape="true" resizable="false" style="width:1000px; height: 500px;" showEffect="explode" hideEffect="bounce">
<p:dataTable value="#{Kereses.values}" var="c"
styleClass="order-table" scrollable="true" scrollWidth="1500" scrollHeight="550">
<p:column style="width: 130px; text-align: center">
<f:facet name="header">OM kód:</f:facet>
<h:outputText value="#{c.omkod}" style="width: 130px; text-align: center"/>
</p:column>
</p:dataTable>
</p:dialog>
</h:form>
</html>
Вот мой Java-код:
import java.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean @SessionScoped
public class Kereses implements Serializable {
int id;
String omkod;
String URL = "jdbc:mysql://localhost:3306/osszesito?useUnicode=yes&characterEncoding=UTF-8";
String USER = "User";
String PASSWORD = "Password";
String DRIVER = "com.mysql.jdbc.Driver";
private List<Kereses> values;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getOmkod() {
return omkod;
}
public void setOmkod(String omkod) {
this.omkod = omkod;
}
public List<Kereses> getValues() throws SQLException{
values = selectTesztTable();
System.out.println("++++++++++++++++++++++++++++++++++++++++++++values erteke:" + values + "+++++++++++++++++++++++++++++++");
return values;
}
public Connection getDBConnection() {
Connection dbConnection = null;
try {
Class.forName(DRIVER);
dbConnection= DriverManager.getConnection(URL, USER, PASSWORD);
System.out.println("Connection completed.");
} catch (SQLException e) {
System.out.println(e.getMessage());
}catch(ClassNotFoundException cnfe){
cnfe.printStackTrace();
System.out.println(cnfe.getMessage());
System.exit(-1);
}
return dbConnection;
}
public List<Kereses> selectTesztTable() throws SQLException{
Kereses keres = new Kereses();
List<Kereses> lista = new ArrayList<Kereses>();
ResultSet rs = null;
PreparedStatement pst = null;
Connection con = getDBConnection();
String stmQuery = "SELECT * FROM teljes_osszesito where id like '"+ id + "'";
try {
pst = con.prepareStatement(stmQuery);
rs = pst.executeQuery();
while(rs.next()){
keres.setId(rs.getInt("id"));
keres.setOmkod(rs.getString("omkod"));
System.out.println("id: "+ getId() +" OM kód: " + getOmkod() +"---------------------vege-------------------");
}
return lista;
}catch (SQLException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}finally{
rs.close();
pst.close();
con.close();
}
return lista;
}
}
Вот моя проблема:
Когда я в поисках, я пишу id во входном тексте и нажмите кнопку командной строки. Но страница не найдена. Если я вижу server.log, я вижу, что inputtext значение вправо и выберите правый запрос, но моя страница JSF не отображает значения :(
Почему?
спасибо,
спасибо, спасибо, это работает :) – fuge007
добро пожаловать. Вы должны взглянуть на PreparedStatement в java, чтобы избежать SQL Injection. Здесь: http://javarevisited.blogspot.fr/2012/03/why-use-preparedstatement-in-java-jdbc.html –