Es decir, cuando el usuario accede al sistema el usuario puede crear una idea y esta se registra en la base datos pero no puedo obtener el id del usuario que inicio sesión.
Este es el código de las dos tablas.
Código MySQL:
[/HIGHLIGHT]Ver original
) )[HIGHLIGHT="SQL"]
Este es el jsp donde se extraen los datos para insertar la idea.
Código HTML:
<form id="formIdea" method="post" action="insertarVoto"> <input type="text" required title="Ingrese un nombre para su idea" name="nombre" placeholder="Nombre"> </br> </br> <div class="row uniform"> <div class="12u"> <div class="select-wrapper"> <select name="area" required title="Selecciona una área" id="category"> <option value="">- Área -</option> <option value="Sistemas Computacionales ">Sistemas Computacionales</option> <option value="Tic´s">Tic´s</option> <option value="Gestion Empresarial">Gestión Empresarial</option> <option value="Arquitectura">Arquitectura</option> <option value="Electromecanica">Electromecanica</option> </select> </div> </div> </div> <br> <br> <div> <ul class="actions"> <li> <input type="file" class="button" name="photo" size="50" required title="Seleccione una imagen"> </li> </ul> </div> <div class="row uniform"> <div class="12u"> <textarea name="descripcion" required title="Ingrese la descripción" id="message" placeholder="Descripcion" rows="6"></textarea> </div> </div> <br> <br> <div> <input type="text" required title="Ingrese la URL de un video de Youtube" name="video" placeholder="Ingresa la URL de tu video de youtube"> </div> <br> <br> [COLOR="red"]<input type="text" name="id_usuarios" value="" > en esta parte no se como obtener el id_usuario [/COLOR] </br> </br> <br> <br> <div class="row uniform"> <div class="12u"> <ul class="actions"> <li><input type="submit" value="Publicar" /></li> <li><input type="reset" value="Reset" class="alt" /></li> </ul> </div> </div> </form>
Este es el servlet que manda a llamar el jsp.
Código:
public class InsertarImg extends HttpServlet {
// database connection settings
private final String dbURL = "jdbc:mysql://localhost:3306/db_proyecto?zeroDateTimeBehavior=convertToNull";
private final String dbUser = "root";
private final String dbPass = "12345";
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// gets values of text fields
String nombre = request.getParameter("nombre");
String area = request.getParameter("area");
String descripcion = request.getParameter("descripcion");
String video = request.getParameter("video");
String usu = request.getParameter("id_usuarios");
Integer idusuario = Integer.parseInt(usu);
InputStream inputStream = null; // input stream of the upload file
// obtains the upload file part in this multipart request
Part filePart = request.getPart("photo");
if (filePart != null) {
// prints out some information for debugging
System.out.println(filePart.getName());
System.out.println(filePart.getSize());
System.out.println(filePart.getContentType());
// obtains input stream of the upload file
inputStream = filePart.getInputStream();
}
Connection conn = null; // connection to the database
String message = null; // message will be sent back to client
try {
// connects to the database
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
conn = DriverManager.getConnection(dbURL, dbUser, dbPass);
// constructs SQL statement
String sql = "INSERT INTO ideas (nombre, area, photo, descripcion,video,id_usuarios) values (?, ?, ?, ?, ?, ?)";
PreparedStatement statement = conn.prepareStatement(sql);
statement.setString(1, nombre);
statement.setString(2, area);
if (inputStream != null) {
// fetches input stream of the upload file for the blob column
statement.setBinaryStream(3, inputStream,(int) filePart.getSize());
}
statement.setString(4, descripcion);
statement.setString(5, video);
statement.setInt(6, idusuario);
// sends the statement to the database server
int row = statement.executeUpdate();
if (row > 0) {
message = "Su idea fue insertada correctamente";
}
} catch (SQLException ex) {
message = "ERROR: " + ex.getMessage();
} finally {
if (conn != null) {
// closes the database connection
try {
conn.close();
} catch (SQLException ex) {
}
}
// sets the message in request scope
request.setAttribute("Message", message);
// forwards to the message page
getServletContext().getRequestDispatcher("/idea_1_1.jsp").forward(request, response);
}
}
}


