|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
I have made a form in html and am trying to submit 6 values from this form into a database...but I can't work out the code to put in the jsp....I'm new at this stuff so sorry if the coding is poor. Is there a way to send the info straight from clicking "submit" or do I have to somehow return the information back to the page first?
This is what I have so far: <!-- Set the scripting language to java and import the java.sql package --> <%& page language="java" import="java.io.*" import="java.sql.*" %> <% Connection con = null; // The driver class name. String drvName = "org.postgresql.Driver"; // Load the database driver. try { Driver drv = (Driver)Class.forName(drvName).newInstance(); } catch (Exception e) { System.out.println ( "Driver load failed: " + e.getMessage() ); System.exit ( 0 ); } // Use the DriverManager to open a connection to the database try { con = DriverManager.getConnection ( "jdbc ostgresql://penguin/xxx", "xxx", "password" );} catch ( SQLException e ) { System.out.println ( "getConnection failed: " + e.getMessage() ); System.exit ( 0 ); } //create the statement and insert data into the Posts table try { Statement statement = con.createStatement(); statement.executeUpdate("INSERT INTO Users" + "VALUES('email','username','nativeLang','fluentLang','wantedLang','password')"); if ( rs != null ) { while ( rs.next() ) { } } // remember to close the ResultSet rs.close(); } catch ( SQLException e ) { System.out.println ( "create/execute statement failed: " + e.getMessage() ); System.exit ( 0 ); } //close the connection. try{ con.close(); System.out.println("Connection closed."); } catch (Exception e) { System.out.println ( "close connection failed: " + e ); } %> |
|
#2
|
|||
|
|||
|
I do this kind of work with servlets. Here are some missing parts that you might want to consider:
Somewhere in the method that outputs your HTML FORM: out.println ( "<INPUT TYPE=\"TEXT\" NAME=\"email\" VALUE=\"" + email + "\" SIZE=\"30\">" ); ... other fields... out.println ( "<INPUT TYPE=\"SUBMIT\" NAME=\"frmSubmitEdit\" VALUE=\"Save Changes\">" ); Somewhere in the doPost() method: if ( request.getParameter ( "frmSubmitEdit" ) != null ) { frmSubmitEdit ( out, request ); } ... protected void frmSubmitEdit ( PrintWriter out, HttpServletRequest request ) { String email = request.getParameter ( "email" ); String username = request.getParameter ( "username" ); String nativeLang = request.getParameter ( "nativeLang" ); String wantedLang = request.getParameter ( "wantedLang" ); String password = request.getParameter ( "password" ); ... String sql = "insert into users (email, username, nativeLang, wantedLang,password) values ( '" + email + "', '" + username + "', '" + nativeLang + "', '" + wantedLang + "', '" + password + "'"; statement.executeUpdate (sql); Hope this helps. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Java Help > collecting form info and inputting to postgres database |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|