Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

Updating a derby database

Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 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="">the
* 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 =
"(id INT, firstname %s , score INT)",
tableName, format, format, format);
String template =
String.format("INSERT INTO %s VALUES(?, ?, ?, ?, ?)",
PreparedStatement inserter =
for(User e: Users) {
inserter.setInt(1, e.getUserID());
inserter.setString(2, e.getFirstName());
inserter.setInt(1, e.getscore());
System.out.printf("Inserted %s %s.%n",
} 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( {
int id = resultSet.getInt("id");
String firstName = resultSet.getString("firstname");
System.out.printf("%s %s (%s, id=%s)",
firstName, id);
} catch (SQLException sqle) {

public static void main(String[] args) {
EmbeddedDbCreator tester = new EmbeddedDbCreator();
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

Just let me do the talking. Ahem ... so ... you see ... we have this tiny ad...
We need your help - Coderanch server fundraiser
    Bookmark Topic Watch Topic
  • New Topic