Web.xml:
xml version="1.0" encoding="UTF-8"?>
DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>greetServletservlet-name>
<servlet-class>gwtmysql.server.GreetingServiceImplservlet-class>
servlet>
<servlet-mapping>
<servlet-name>greetServletservlet-name>
<url-pattern>/gwtmysql/greeturl-pattern>
servlet-mapping>
<welcome-file-list>
<welcome-file>GWTMYSQL.htmlwelcome-file>
welcome-file-list>
web-app>
GETMYSQL.html:
doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link type="text/css" rel="stylesheet" href="GWTMYSQL.css">
<title>Web Application Starter Projecttitle>
<script type="text/javascript" language="javascript" src="gwtmysql/gwtmysql.nocache.js">script>
head>
<body>
<iframe src="javascript:''" id="__gwt_historyFrame" tabIndex='-1' style="position:absolute;width:0;height:0;border:0">iframe>
<noscript>
<div style="width: 22em; position: absolute; left: 50%; margin-left: -11em; color: red; background-color: white; border: 1px solid red; padding: 4px; font-family: sans-serif">
Your web browser must have JavaScript enabled
in order for this application to display correctly.
div>
noscript>
<h1>GWT With MYSQL Insert Update Delete Projecth1>
<table align="center">
<tr>
<td colspan="2" style="font-weight:bold;">######################################td>
tr>
<tr>
<td id="idLabelContainer">td>
<td id="idFieldContainer">td>
<td id="nameLabelContainer">td>
<td id="nameFieldContainer">td>
<td id="insertButtonContainer">td>
<td id="updateButtonContainer">td>
<td id="deleteButtonContainer">td>
tr>
<tr>
<td colspan="2" style="color:red;" id="errorLabelContainer">td>
tr>
table>
body>
html>
GETMYSQL.gwt.xml:
xml version="1.0" encoding="UTF-8"?>
<module rename-to='gwtmysql'>
<inherits name='com.google.gwt.user.User'/>
<inherits name='com.google.gwt.user.theme.standard.Standard'/>
<entry-point class='gwtmysql.client.GWTMYSQL'/>
<source path='client'/>
<source path='shared'/>
module>
GreetingService:
package gwtmysql.client;
import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;
/**
* The client side stub for the RPC service.
*/
@RemoteServiceRelativePath("greet")
public interface GreetingService extends RemoteService {
String greetServer(String id,String name,String methodName) throws IllegalArgumentException;
}
GreetingServiceAsync:
package gwtmysql.client;
import com.google.gwt.user.client.rpc.AsyncCallback;
/**
* The async counterpart of GreetingService
.
*/
public interface GreetingServiceAsync {
public void greetServer(String input1,String input2,String methodName, AsyncCallback
throws IllegalArgumentException;
}
GWTMYSQL:
package gwtmysql.client;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
/**
* Entry point classes define onModuleLoad()
.
*/
public class GWTMYSQL implements EntryPoint {
/**
* The message displayed to the user when the server cannot be reached or
* returns an error.
*/
private static final String SERVER_ERROR = "An error occurred while "
+ "attempting to contact the server. Please check your network "
+ "connection and try again.";
/**
* Create a remote service proxy to talk to the server-side Greeting service.
*/
private final GreetingServiceAsync greetingService = GWT
.create(GreetingService.class);
/**
* This is the entry point method.
*/
public void onModuleLoad() {
final Button insertButton = new Button("Insert");
final Button updateButton = new Button("Update");
final Button deleteButton = new Button("Delete");
final Label idLabel = new Label();
idLabel.setText("ID:");
RootPanel.get("idLabelContainer").add(idLabel);
final TextBox idField = new TextBox();
RootPanel.get("idFieldContainer").add(idField);
final Label nameLabel = new Label();
nameLabel.setText("NAME:");
RootPanel.get("nameLabelContainer").add(nameLabel);
final TextBox nameField = new TextBox();
RootPanel.get("nameFieldContainer").add(nameField);
insertButton.addStyleName("Insert");
updateButton.addStyleName("Update");
deleteButton.addStyleName("Delete");
final Label errorLabel = new Label();
RootPanel.get("insertButtonContainer").add(insertButton);
RootPanel.get("updateButtonContainer").add(updateButton);
RootPanel.get("deleteButtonContainer").add(deleteButton);
RootPanel.get("errorLabelContainer").add(errorLabel);
// Focus the cursor on the name field when the app loads
idField.setFocus(true);
idField.selectAll();
// Create a handler for the sendButton and nameField
class MyHandler implements ClickHandler {
/**
* Fired when the user clicks on the sendButton.
*/
public void onClick(ClickEvent event) {
if(event.getSource().equals(insertButton))
{
insertToMYSQL();
}
else if(event.getSource().equals(updateButton))
{
updateToMYSQL();
}
else if (event.getSource().equals(deleteButton))
{
deleteToMYSQL();
}
}
/**
* Send the name from the nameField to the server and wait for a response.
*/
private void insertToMYSQL() {
String idText = idField.getText();
String nameText = nameField.getText();
String methodname = "Insert";
greetingService.greetServer(idText,nameText,methodname,
new AsyncCallback
public void onFailure(Throwable caught) {
// Show the RPC error message to the user
RootPanel.get().add(new HTML("RPC call failed. :-("));
}
public void onSuccess(String result) {
RootPanel.get().add(new HTML("RPC call Success:"));
}
});
}
private void updateToMYSQL() {
String idText = idField.getText();
String nameText = nameField.getText();
String methodname = "Update";
greetingService.greetServer(idText,nameText,methodname,
new AsyncCallback
public void onFailure(Throwable caught) {
// Show the RPC error message to the user
RootPanel.get().add(new HTML("RPC call failed. :-("));
}
public void onSuccess(String result) {
RootPanel.get().add(new HTML("RPC call Success:"));
}
});
}
private void deleteToMYSQL() {
String idText = idField.getText();
String nameText = nameField.getText();
String methodname = "Delete";
greetingService.greetServer(idText,nameText,methodname,
new AsyncCallback
public void onFailure(Throwable caught) {
// Show the RPC error message to the user
RootPanel.get().add(new HTML("RPC call failed. :-("));
}
public void onSuccess(String result) {
RootPanel.get().add(new HTML("RPC call Success:"));
}
});
}
}
// Add a handler to send the name to the server
MyHandler handler = new MyHandler();
insertButton.addClickHandler(handler);
updateButton.addClickHandler(handler);
deleteButton.addClickHandler(handler);
//nameField.addKeyUpHandler(handler);
}
}
GreetingServiceImpl:
package gwtmysql.server;
import gwtmysql.client.GreetingService;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
/**
* The server side implementation of the RPC service.
*/
@SuppressWarnings("serial")
public class GreetingServiceImpl extends RemoteServiceServlet implements
GreetingService {
String dbUrl = "jdbc:mysql://localhost/siddhutest_db";
String dbClass = "com.mysql.jdbc.Driver";
public String greetServer(String id,String name,String methodName) throws IllegalArgumentException {
// Verify that the input is valid.
/*
if (!FieldVerifier.isValidName(input)) {
// If the input is not valid, throw an IllegalArgumentException back to
// the client.
throw new IllegalArgumentException(
"Name must be at least 4 characters long");
}
String serverInfo = getServletContext().getServerInfo();
String userAgent = getThreadLocalRequest().getHeader("User-Agent");
return "Hello, " + input + "!
I am running " + serverInfo
+ ".
It looks like you are using:
" + userAgent;
*/
if(methodName.equals("Insert"))
{
try {
Class.forName(dbClass).newInstance();
Connection con = DriverManager.getConnection(dbUrl,"root","");
Statement stmt = con.createStatement();
String query = "insert into test_table (id, name) values ("+id+",'"+name+"')";
int i = stmt.executeUpdate(query);
con.close();
return "success";
} //end try
catch(Exception e) {
e.printStackTrace();
}
}
else if(methodName.equals("Delete"))
{
try {
Class.forName(dbClass).newInstance();
Connection con = DriverManager.getConnection(dbUrl,"root","");
Statement stmt = con.createStatement();
String query = "delete from test_table where id ="+id;
int i = stmt.executeUpdate(query);
con.close();
return "success";
} //end try
catch(Exception e) {
e.printStackTrace();
}
}else if (methodName.equals("Update"))
{
try {
Class.forName(dbClass).newInstance();
Connection con = DriverManager.getConnection(dbUrl,"root","");
Statement stmt = con.createStatement();
String query = "update test_table set name = '"+name+"' where id ="+id;
int i = stmt.executeUpdate(query);
con.close();
return "success";
} //end try
catch(Exception e) {
e.printStackTrace();
}
}
return "failure";
}
}
FieldVerifier:
package gwtmysql.shared;
/**
*
* FieldVerifier validates that the name the user enters is valid.
*
*
* This class is in the shared
packing because we use it in both
* the client code and on the server. On the client, we verify that the name is
* valid before sending an RPC request so the user doesn't have to wait for a
* network round trip to get feedback. On the server, we verify that the name is
* correct to ensure that the input is correct regardless of where the RPC
* originates.
*
*
* When creating a class that is used on both the client and the server, be sure
* that all code is translatable and does not use native JavaScript. Code that
* is note translatable (such as code that interacts with a database or the file
* system) cannot be compiled into client side JavaScript. Code that uses native
* JavaScript (such as Widgets) cannot be run on the server.
*
*/
public class FieldVerifier {
/**
* Verifies that the specified name is valid for our service.
*
* In this example, we only require that the name is at least four
* characters. In your application, you can use more complex checks to ensure
* that usernames, passwords, email addresses, URLs, and other fields have the
* proper syntax.
*
* @param name the name to validate
* @return true if valid, false if invalid
*/
public static boolean isValidName(String name) {
if (name == null) {
return false;
}
return name.length() > 3;
}
No comments:
Post a Comment