servlet's People
servlet's Issues
clase padre para los dos repository
haz una clase repository padre que contenga estos metodos compartidos y asi podemos dejar repositoryCountry y repositoryLanguage mas pequeñito
private static final String jdbcUrl = "jdbc:h2:file:./src/main/resources/test";
ConnectionManager manager = new ConnectionH2();
private void close(PreparedStatement prepareStatement) {
try {
prepareStatement.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
private void close(ResultSet resultSet) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
var en lowercase
private String Language;
should be
private String language;
gigantesco refactoriza
este metodo sigue siendo gigantesco
hace varias cosas busca en la base de datos y despues inserta
debe hacer lo que dice y solo lo que dice, insert.....
debes crear otros metodos tipo search o lo que necesites y luego en el service, si que llamas a cada una de la piezas pequeñitas del repository
service une las piezas del puzle que esta compuesto por metodos PEQUEÑITOS de repository
// insert
public void insert(Form formulary) {
Connection conn = manager.open(jdbcUrl);
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
int id = 0;
try {
preparedStatement = conn.prepareStatement("SELECT * FROM IDIOMA WHERE IDIOMA = ?");
preparedStatement.setString(1, formulary.getLanguage());
System.out.println(preparedStatement);
System.out.println(formulary.getLanguage());
resultSet = preparedStatement.executeQuery();
if (!resultSet.next()) {
preparedStatement = conn.prepareStatement("INSERT INTO IDIOMA (IDIOMA)" + "VALUES (?)");
preparedStatement.setString(1, formulary.getLanguage());
preparedStatement.executeUpdate();
}
preparedStatement = conn.prepareStatement("SELECT * FROM IDIOMA WHERE IDIOMA = ?");
preparedStatement.setString(1, formulary.getLanguage());
System.out.println(preparedStatement);
System.out.println(formulary.getLanguage());
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
id = resultSet.getInt(1);
System.out.println(id);
}
preparedStatement = conn.prepareStatement("INSERT INTO PAIS (PAIS,IDIOMA)" + "VALUES (?,?)");
preparedStatement.setString(1, formulary.getCountry());
preparedStatement.setInt(2, id);
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
close(preparedStatement);
manager.close(conn);
}
elimina los comentarios todo
// TODO Auto-generated catch block
indica cosas por hacer
si estan hechos quitalos del codigo
remove unnecesary files
quita las carpetas
.settings
.gitignore
de github
usa git rm
nombre de las jsp en ingles
datos.jsp debe llamarse index.jsp
y fin.jsp -> end.jsp
architecture concerns
cada llamada del reposotory debe hacerse sobre una tabla de la base de datos
si tu tienes que realizar dos acciones, una sobre pais y otra sobre idiomas, el service es el encargado de hacerlo, el service llamara a dos repository, uno de pais y otro de idiomas
name forms2
for (Form form2 : forms) {
debe ser
for (Form form : forms) {
web xml en ingles
el fichero web.xml tiene que estar en completo ingles
y el
<welcome-file-list>
<welcome-file>datos.jsp</welcome-file>
</welcome-file-list>
debe estar despues de la declaracion de los servlet, no a mitad
try catch no te hace falta
try {
close(resultSet);
close(prepareStatement);
} catch (Exception e) {
}
refactoriza este engendro y hazlo mas corto
// insert
public void insert(Form formulary) {
Connection conn = manager.open(jdbcUrl);
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
int id=0;
try {
preparedStatement = conn.prepareStatement("SELECT * FROM IDIOMA WHERE IDIOMA = ?");
preparedStatement.setString(1, formulary.getLanguage());
System.out.println(preparedStatement);
System.out.println(formulary.getLanguage());
resultSet = preparedStatement.executeQuery();
if(!resultSet.next()){
preparedStatement = conn.prepareStatement("INSERT INTO IDIOMA (IDIOMA)" +
"VALUES (?)");
preparedStatement.setString(1, formulary.getLanguage());
preparedStatement.executeUpdate();
}
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}finally {
try {
preparedStatement = conn.prepareStatement("SELECT * FROM IDIOMA WHERE IDIOMA = ?");
preparedStatement.setString(1, formulary.getLanguage());
System.out.println(preparedStatement);
System.out.println(formulary.getLanguage());
resultSet = preparedStatement.executeQuery();
while(resultSet.next()){
id = resultSet.getInt(1);
System.out.println(id);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
preparedStatement = conn.prepareStatement("INSERT INTO PAIS (PAIS,IDIOMA)" +
"VALUES (?,?)");
preparedStatement.setString(1, formulary.getCountry());
preparedStatement.setInt(2, id);
preparedStatement.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
close(preparedStatement);
manager.close(conn);
}
formulary spanglish
formulary do not even exist, told by a native teacher
name matters
for (Language languages2 : languages) {
deberia ser
for (Language language : languages) {
tu iteraras sobre un monton de lenguajes que vas cogiendo de lenguaje en lenguaje
de ahi el singular y el plural
o los pones o lo quitas
tienes que tener 2 clases repository
// Borrado
public void deleteLanguage(int IdIdioma) {
Connection conn = manager.open(jdbcUrl);
PreparedStatement preparedStatement = null;
try {
preparedStatement = conn.prepareStatement("DELETE FROM IDIOMA WHERE IDIDIOMA = ?");
preparedStatement.setInt(1, IdIdioma);
preparedStatement.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
manager.close(conn);
}
public void deleteCountry(int IdIdioma) {
Connection conn = manager.open(jdbcUrl);
PreparedStatement preparedStatement = null;
try {
preparedStatement = conn.prepareStatement("DELETE FROM PAIS WHERE IDIOMA = ?");
preparedStatement.setInt(1, IdIdioma);
preparedStatement.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
manager.close(conn);
}
separa en 2 clasees repository, una para paises y otra para idiomas
correct names
protected void redirect(String destino
protected void redirect(String page
try catch de try catch de try catch
refactor this
try {
preparedStatement = conn.prepareStatement("SELECT * FROM IDIOMA WHERE IDIOMA = ?");
preparedStatement.setString(1, formulary.getLanguage());
System.out.println(preparedStatement);
System.out.println(formulary.getLanguage());
resultSet = preparedStatement.executeQuery();
if(!resultSet.next()){
preparedStatement = conn.prepareStatement("INSERT INTO IDIOMA (IDIOMA)" +
"VALUES (?)");
preparedStatement.setString(1, formulary.getLanguage());
preparedStatement.executeUpdate();
}
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}finally {
try {
preparedStatement = conn.prepareStatement("SELECT * FROM IDIOMA WHERE IDIOMA = ?");
preparedStatement.setString(1, formulary.getLanguage());
System.out.println(preparedStatement);
System.out.println(formulary.getLanguage());
resultSet = preparedStatement.executeQuery();
while(resultSet.next()){
id = resultSet.getInt(1);
System.out.println(id);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
preparedStatement = conn.prepareStatement("INSERT INTO PAIS (PAIS,IDIOMA)" +
"VALUES (?,?)");
preparedStatement.setString(1, formulary.getCountry());
preparedStatement.setInt(2, id);
preparedStatement.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
close(preparedStatement);
sobra
public RepositoryLanguage() {
super();
}
form no form2
for (Form form2 : forms) {
nunca nunca nunca
try{
close(resultSet);
close(prepareStatement);
}catch(Exception e){}
nunca se captura un excepcion y no se hace nada con ella
este try catch deberia estar dentro de cada metodo
sigue estando
private vars on model classes
si usas getter y setter las variables de los model hazlas private
String country,language;
private String country,language;
not plural for each single element
for (Language languages2 : languages) {
String data = "<option value=""+languages2.getLanguage()+"">"+languages2.getLanguage()+"";
out.println(data);
}
languages2 -> language
nombres de jsp en ingles
todo en ingles
git rm
.project
.classpath me sobran
eliminalos con git rm .project
do something with exceptions
nunca escondas los errores
try{
close(resultSet);
close(prepareStatement);
}catch(Exception e){}
debe ser al menos
try{
close(resultSet);
close(prepareStatement);
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException(e);
}
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.