online dating site with instant chat - Updating database using servlet

In this document, I will guide step by step how to create a simple web application with the combiantion of Servlet + JSP + Filter + JSP EL + JDBC. "; Prepared Statement pstm = conn.prepare Statement(sql); String(1, code); Result Set rs = pstm.execute Query(); while (rs.next()) { String name = String("Name"); float price = Float("Price"); Product product = new Product(code, name, price); return product; } return null; } public static void update Product(Connection conn, Product product) throws SQLException { String sql = "Update Product set Name =? = /* means that all requests of users have go through this filter.

updating database using servlet-39

In this example, I hide the jsp files in the WEB-INF/views. "; Prepared Statement pstm = conn.prepare Statement(sql); String(1, user Name); Result Set rs = pstm.execute Query(); if (rs.next()) { String password = String("Password"); String gender = String("Gender"); User Account user = new User Account(); User Name(user Name); Password(password); Gender(gender); return user; } return null; } public static List query Product(Connection conn) throws SQLException { String sql = "Select a.

When the user requests to a Servlet, it will dispose user's requirements, such insert, update and query the data, eventually forward to the JSP page to display the data.

Thus, each servlet has 0 or multiple corresponding JSP pages (Usually only need 1).

-- Create table create table USER_ACCOUNT ( USER_NAME VARCHAR2(30) not null, GENDER VARCHAR2(1) not null, PASSWORD VARCHAR2(30) not null, primary key (USER_NAME) ); -- Create table create table PRODUCT ( CODE VARCHAR2(20) not null, NAME VARCHAR2(128) not null, PRICE FLOAT not null, primary key (CODE) ) ; -- Insert data: --------------------------------------------------------------- insert into user_account (USER_NAME, GENDER, PASSWORD) values ('tom', 'M', 'tom001'); insert into user_account (USER_NAME, GENDER, PASSWORD) values ('jerry', 'M', 'jerry001'); insert into product (CODE, NAME, PRICE) values ('P001', 'Java Core', 100); insert into product (CODE, NAME, PRICE) values ('P002', 'C# Core', 90); -- Commit Commit; -- Create table create table USER_ACCOUNT ( USER_NAME VARCHAR(30) not null, GENDER VARCHAR(1) not null, PASSWORD VARCHAR(30) not null, primary key (USER_NAME) ); -- Create table create table PRODUCT ( CODE VARCHAR(20) not null, NAME VARCHAR(128) not null, PRICE FLOAT not null, primary key (CODE) ) ; -- Insert data: --------------------------------------------------------------- insert into user_account (USER_NAME, GENDER, PASSWORD) values ('tom', 'M', 'tom001'); insert into user_account (USER_NAME, GENDER, PASSWORD) values ('jerry', 'M', 'jerry001'); insert into product (CODE, NAME, PRICE) values ('P001', 'Java Core', 100); insert into product (CODE, NAME, PRICE) values ('P002', 'C# Core', 90); -- Create table create table USER_ACCOUNT ( USER_NAME VARCHAR(30) not null, GENDER VARCHAR(1) not null, PASSWORD VARCHAR(30) not null, primary key (USER_NAME) ); -- Create table create table PRODUCT ( CODE VARCHAR(20) not null, NAME VARCHAR(128) not null, PRICE FLOAT not null, primary key (CODE) ) ; -- Insert data: --------------------------------------------------------------- insert into user_account (USER_NAME, GENDER, PASSWORD) values ('tom', 'M', 'tom001'); insert into user_account (USER_NAME, GENDER, PASSWORD) values ('jerry', 'M', 'jerry001'); insert into product (CODE, NAME, PRICE) values ('P001', 'Java Core', 100); insert into product (CODE, NAME, PRICE) values ('P002', 'C# Core', 90); You have to download JDBC library to driving the connection with the Database. "; Prepared Statement pstm = conn.prepare Statement(sql); String(1, user Name); String(2, password); Result Set rs = pstm.execute Query(); if (rs.next()) { String gender = String("Gender"); User Account user = new User Account(); User Name(user Name); Password(password); Gender(gender); return user; } return null; } public static User Account find User(Connection conn, String user Name) throws SQLException { String sql = "Select a.

In this document, I download both of 3 JDBC libraries for Oracle, My SQL, SQL Server, in practice, you only need JDBC library corresponding to the type of database you are using. SQLException; public class My SQLConn Utils { public static Connection get My SQLConnection() throws Class Not Found Exception, SQLException { // Note: Change the connection parameters accordingly.

package org.o7planning.simplewebapp.beans; public class User Account { public static final String GENDER_MALE ="M"; public static final String GENDER_FEMALE = "F"; private String user Name; private String gender; private String password; public User Account() { } public String get User Name() { return user Name; } public void set User Name(String user Name) { Name = user Name; } public String get Gender() { return gender; } public void set Gender(String gender) { this.gender = gender; } public String get Password() { return password; } public void set Password(String password) { this.password = password; } } package org.o7planning.simplewebapp.beans; public class Product { private String code; private String name; private float price; public Product() { } public Product(String code, String name, float price) { = code; = name; this.price = price; } public String get Code() { return code; } public void set Code(String code) { = code; } public String get Name() { return name; } public void set Name(String name) { = name; } public float get Price() { return price; } public void set Price(float price) { this.price = price; } } package org.o7planning.simplewebapp.conn; import String host Name = "localhost"; String db Name = "mytest"; String user Name = "root"; String password = "12345"; return get My SQLConnection(host Name, db Name, user Name, password); } public static Connection get My SQLConnection(String host Name, String db Name, String user Name, String password) throws SQLException, Class Not Found Exception { // Declare the class Driver for My SQL DB // This is necessary with Java 5 (or older) // Java6 (or newer) automatically find the appropriate driver. SQLException; public class Oracle Conn Utils { public static Connection get Oracle Connection() throws Class Not Found Exception, SQLException { // Note: Change the connection parameters accordingly.

// If you use Java 5, then this line is not needed. Driver"); // URL Connection for My SQL // Example: jdbc:mysql://localhost:3306/simplehr String connection URL = "jdbc:mysql://" + host Name + ":3306/" + db Name; Connection conn = Driver Connection(connection URL, user Name, password); return conn; } } package org.o7planning.simplewebapp.conn; import String host Name = "localhost"; String sid = "db11g"; String user Name = "mytest"; String password = "12345"; return get Oracle Connection(host Name, sid, user Name, password); } public static Connection get Oracle Connection(String host Name, String sid, String user Name, String password) throws Class Not Found Exception, SQLException { // Declare the class Driver for ORACLE DB // This is necessary with Java 5 (or older) // Java6 (or newer) automatically find the appropriate driver. SQLException; public class SQLServer Conn Utils_JTDS { // Connect to SQLServer // (Using JDBC Driver of JTDS library) public static Connection get SQLServer Connection_JTDS() throws SQLException, Class Not Found Exception { // Note: Change the connection parameters accordingly.

// If you use Java 5, then this line is not needed. Oracle Driver"); // URL Connection for Oracle // Example: jdbc:oracle:thin:@localhost:1521:db11g String connection URL = "jdbc:oracle:thin:@" + host Name + ":1521:" + sid; Connection conn = Driver Connection(connection URL, user Name, password); return conn; } } package org.o7planning.simplewebapp.conn; import String host Name = "localhost"; String sql Instance Name = "SQLEXPRESS"; String database = "mytest"; String user Name = "sa"; String password = "12345"; return get SQLServer Connection_JTDS(host Name, sql Instance Name, database, user Name, password); } // Connect to SQLServer, using JTDS library private static Connection get SQLServer Connection_JTDS(String host Name, String sql Instance Name, String database, String user Name, String password) throws Class Not Found Exception, SQLException { // Declare the class Driver for SQLServer DB // This is necessary with Java 5 (or older) // Java6 (or newer) automatically find the appropriate driver. SQLException; public class SQLServer Conn Utils_SQLJDBC { // Connect to SQLServer // (Using JDBC Driver: SQLJDBC) public static Connection get SQLServer Connection_SQLJDBC() throws Class Not Found Exception, SQLException { // Note: Change the connection parameters accordingly. Http Session; import org.o7planning.simplewebapp.beans. User Account; public class My Utils { public static final String ATT_NAME_CONNECTION = "ATTRIBUTE_FOR_CONNECTION"; private static final String ATT_NAME_USER_NAME = "ATTRIBUTE_FOR_STORE_USER_NAME_IN_COOKIE"; // Store Connection in request attribute. User Account; public class DBUtils { public static User Account find User(Connection conn, String user Name, String password) throws SQLException { String sql = "Select a.

// If you use Java 5, then this line is not needed. Driver"); // Example: // jdbc:jtds:sqlserver://localhost:1433/simplehr;instance=SQLEXPRESS String connection URL = "jdbc:jtds:sqlserver://" + host Name + ":1433/" + database + ";instance=" + sql Instance Name; Connection conn = Driver Connection(connection URL, user Name, password); return conn; } } package org.o7planning.simplewebapp.conn; import String host Name = "localhost"; String sql Instance Name = "SQLEXPRESS"; String database = "mytest"; String user Name = "sa"; String password = "12345"; return get SQLServer Connection_SQLJDBC(host Name, sql Instance Name, database, user Name, password); } //Connect to SQLServer, using SQLJDBC Library. // (Information stored only exist during requests) public static void store Connection(Servlet Request request, Connection conn) { Attribute(ATT_NAME_CONNECTION, conn); } // Get the Connection object has been stored in one attribute of the request.

Tags: , ,