Ventris Learchus

Greenhorn
+ Follow
since Apr 10, 2013
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Ventris Learchus

HI guys I want to develop a form which upon clicking submit will add data to a derby database . at the moment I've researched using http://courses.coreservlets.com/Course-Materials/pdf/msajsp/10-JDBC.pdf and using their source code however I am unsure how to add the data , does anyone now how to do this ? the form will pass the data via a simple action = POST

Here is the current code for the database creation that i am using

import java.sql.; import java.util.;

import javax.servlet.http.Cookie;

/** Creates "myDatabase" DB and "Users" table.
* <p>
* From <a href="http://courses.coreservlets.com/Course-Materials/">the
* coreservlets.com tutorials on servlets, JSP, Struts, JSF, Ajax, GWT,
* Spring, Hibernate/JPA, and Java programming</a>.
*/



public class EmbeddedDbCreator {
// Driver class not needed in JDBC 4.0 (Java SE 6)
// private String driver = "org.apache.derby.jdbc.EmbeddedDriver";
private String protocol = "jdbc:derby:";
private String username = "bleh";
private String password = "tomcat";
private String dbName = "myDatabase";
private String tableName = "Users";
private Properties userInfo;

public EmbeddedDbCreator() {
userInfo = new Properties();
userInfo.put("user", username);
userInfo.put("password", password);
}

public void createDatabase() {
User[] Users = {
new User(1, "Graham",1),
new User(2, "Aaron",4),
new User(3, "Dan",2),
new User(4, "James",3),
new User(5, "David",5),
};



try {
String dbUrl = protocol + dbName + ";create=true";
Connection connection = DriverManager.getConnection(dbUrl, userInfo);
Statement statement = connection.createStatement();
String format = "VARCHAR(20)";
String tableDescription =
String.format
("CREATE TABLE %s" +
"(id INT, firstname %s , score INT)",
tableName, format, format, format);
statement.execute(tableDescription);
String template =
String.format("INSERT INTO %s VALUES(?, ?, ?, ?, ?)",
tableName);
PreparedStatement inserter =
connection.prepareStatement(template);
for(User e: Users) {
inserter.setInt(1, e.getUserID());
inserter.setString(2, e.getFirstName());
inserter.setInt(1, e.getscore());
inserter.executeUpdate();
System.out.printf("Inserted %s %s.%n",
e.getFirstName());
}
inserter.close();
connection.close();
} catch (SQLException sqle) {
// If table already exists, then skip everything else
}
}

public void showTable() {
try {
String dbUrl = protocol + dbName;
Connection connection;
connection = DriverManager.getConnection(dbUrl, userInfo);
Statement statement = connection.createStatement();
String query =
String.format("SELECT * FROM %s",tableName);
ResultSet resultSet = statement.executeQuery(query);
while(resultSet.next()) {
int id = resultSet.getInt("id");
String firstName = resultSet.getString("firstname");
System.out.printf("%s %s (%s, id=%s)",
firstName, id);
}
connection.close();
} catch (SQLException sqle) {
sqle.printStackTrace();
}
}

public static void main(String[] args) {
EmbeddedDbCreator tester = new EmbeddedDbCreator();
tester.createDatabase();
tester.showTable();
}
}
IF someone could show me what the function to add a user would look like as well as how to call this in a jsp i'd really appreciate it

nvm , after adding a dopost method i cleaned the solution and that fixed it
5 years ago
a snippet of the servlet code
5 years ago
i made the change you suggested and it seem to have worked however I am now getting the following error
5 years ago
Hi Guys ,

I'm having a problem as shown here http://stackoverflow.com/questions/15908994/call-servlet-from-jsp-and-use-if

Basically I want to send the info from me html form to a servlet , upon reaching the servlet i want to then display a button to let the user proceed to a jsp page .


I'm not sure where I'm going wrong , I've googled several tutorials but can't find anything that works , I'm assuming I've left something simple out , ...in my html the form is declared as this <form id="form" name="form" method="post" action="RegistrationServlet" >


the xml looks like this


5 years ago