robim teraz projekt do skoly - jsp stranka, posle cez "post" veci do servletu, ten to odovza triede, ktora by to mala zapisat do databazy.
(je to sice jak skrabat sa okolo hlavy ale tot su poziadavky
)
build zbehne bez chyby, po vyplneni formulara na jsp. a submit-nuti ale k zapisu do db nedojte, su tam furt len veci, kere som hodil
sql prikazmi... error to nehadze ziaden, tvari sa to ze zapis dat zbehol??
Zdrojaky
regis.jsp (len fragment)
<form action="JavaAction" method="post">
<table cellspacing="2" cellpadding="3" border="0">
<tr><td>Meno: </td>
<td><input type="text" name="meno"></td>
</tr>
<tr><td>Priezvisko: </td>
<td><input type="text" name="priezvisko"></td>
</tr>
<tr><td>Nick: </td>
<td><input type="text" name="nick"></td>
</tr>
</table>
<button type="submit">Over dostupnost</button>
<table cellspacing="2" cellpadding="3" border="0">
<tr><td>Password: </td>
<td><input type="password" name="heslo"></td>
</tr>
<tr><td>E-mail: </td>
<td><input type="text" name="email"></td>
</tr>
</table>
<input type="submit" value="Odoslat">
<input type="reset" value="Vymaz formular">
</form>
JavaAction.java (cely kod) - servlet
package mypackage;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class JavaAction extends HttpServlet {
private static final long serialVersionUID = 1L;
public JavaAction() {super();}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String me, pri, nik, hes, eml;
Registracia reg = new Registracia();
me=request.getParameter("meno");
pri=request.getParameter("priezvisko");
nik=request.getParameter("nick");
hes=request.getParameter("heslo");
eml=request.getParameter("email");
reg.insertData(me, pri, nik, hes, eml);
request.getRequestDispatcher("regis.jsp").forward(request, response);
}
}
Registracia.java (cely kod) - trieda
package mypackage;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
public class Registracia {
static Connection conn;
public String vysledok;
public void insertData(String me, String pri, String nck, String hsl, String eml){
String driver = "org.apache.derby.jdbc.ClientDriver";
String connUrl = "jdbc:derby://localhost:1527/pouzivatelia";
try{
Class.forName(driver);
conn = DriverManager.getConnection(connUrl,"admin","Banan230");
Statement stmt = conn.createStatement();
PreparedStatement psInsert = conn.prepareStatement("insert into users values (?,?,?,?,?,?)");
psInsert.setString(1, me);
psInsert.setString(2, pri);
psInsert.setString(3, nck);
psInsert.setString(4, hsl);
psInsert.setString(5, eml);
psInsert.setInt(6, 01);
stmt.executeUpdate("insert into users values(");
// -- Inserting data
psInsert.executeUpdate();
conn.close();
}catch (Exception e) {e.printStackTrace();
}
}
}
Nemam tam vela komentov, nakolko som to uz x-krat prepisoval, ale dufam, ze vam z toho dojde funkcnost....
Ak by ste niekto vedeli helpnut, bol by som rad