Title: RE: Database performance with servlets(writing to a database)

Here is a sample code that inserts to a table called MYTABLE that has three fields; name, surname and no the values that are hardcoded to the variables strName, strSurname and number. (Note the single quotes around values that are strings.)


Statement statement=null;
Connection connection = null;
try{
//get a connection

    connection = ConnectToDB.getConnection();
    //create a statement
    statement = connection.createStatement();

    //execute a query
    String strName="John";
    String strSurame="Smith";
    int number = 1;

int i = statement.executeUpdate("INSERT INTO MYTABLE(name, surname, no) VALUES ( " ' +  strName + " ', ' "  + strSurname + " ' ,"  number)");

    if (i==0)
    Util.printMessage("Could not insert....); statement.close(); statement=null;
    ConnectToDB.freeConnection(connection);

   }
catch(Exception e){
}

Reply via email to