GreetingService.java
package com.client;
import com.google.gwt.user.client.rpc.RemoteService;
importcom.google.gwt.user.client.rpc.RemoteServiceRelativePath;
/**
* The client side stub for the RPC service.
*/
@RemoteServiceRelativePath("greet")
public interface GreetingService extends RemoteService {
String upload(String topic, String stream, String Tag, String plink, String fpath );
}
GreetinServiceAsync.java
package com.client;
import com.google.gwt.user.client.rpc.AsyncCallback;
/**
* The async counterpart of <code>GreetingService</code>.
*/
public interface GreetingServiceAsync
{
void upload(String topic, String stream, String Tag, String plink, String fpath, AsyncCallback<String> callback );
}
TestExample.java
package com.client;
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.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
/**
* Entry point classes define <code>onModuleLoad()</code>.
*/
public class TestExample implements EntryPoint
{
private final GreetingServiceAsync a1 = GWT.create(GreetingService.class);
RootPanel rt;
AbsolutePanel absolutePanel;
TextBox textBox;
ListBox comboBox;
TextBox textBox_1;
TextBox textBox_2;
FileUpload fileUpload;
Button button;
String s1,s2,s3,s4,s5;
public void onModuleLoad()
{
rt=RootPanel.get();
absolutePanel = new AbsolutePanel();
rt.add(absolutePanel, 218, 24);
absolutePanel.setSize("445px", "434px");
textBox = new TextBox();
absolutePanel.add(textBox, 143, 39);
Label lblTopic = new Label("Topic");
absolutePanel.add(lblTopic, 86, 42);
comboBox = new ListBox();
comboBox.addItem("Computer Science");
comboBox.addItem("Environment");
comboBox.addItem("Physics");
comboBox.addItem("Chemistry");
comboBox.addItem("Biology");
absolutePanel.add(comboBox, 143, 88);
comboBox.setSize("148px", "24px");
Label lblStreams = new Label("Streams");
absolutePanel.add(lblStreams, 69, 88);
textBox_1 = new TextBox();
absolutePanel.add(textBox_1, 143, 139);
Label lblTags = new Label("Tags");
absolutePanel.add(lblTags, 87, 139);
textBox_2 = new TextBox();
absolutePanel.add(textBox_2, 143, 194);
Label lblPublicLink = new Label("Public Link");
absolutePanel.add(lblPublicLink, 49, 194);
fileUpload = new FileUpload();
absolutePanel.add(fileUpload, 143, 254);
button = new Button("New button");
absolutePanel.add(button, 156, 311);
button.addClickHandler(new ClickHandler()
{
public void onClick(ClickEvent event)
{
Window.alert("onclick");
String topic=textBox.getText();
String strm=comboBox.getItemText(comboBox.getSelectedIndex());
String tag=textBox_1.getText();
String plink=textBox_2.getText();
String fpath=fileUpload.getFilename();
a1.upload(topic, strm, tag, plink, fpath, newAsyncCallback<String>()
{
@Override
public void onSuccess(String result)
{
// TODO Auto-generated method stub
String f="yes";
System.out.println(result);
// TODO Auto-generated method stub
if(result.equals(f))
{
System.out.println("onsuccess");
Window.alert("File Upload");
}
else
{
Window.alert("Filis not uploaded");
}
}
@Override
public void onFailure(Throwable caught)
{
// TODO Auto-generated method stub
Window.alert("Failure");
}
});
}
});
button.setText("Uplod lctr notes");
button.setSize("133px", "28px");
Label lblFile = new Label("File");
absolutePanel.add(lblFile, 98, 254);
lblFile.setSize("27px", "21px");
}
}
GreetingServiceimp.java
package com.server;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.client.GreetingService;
importcom.google.gwt.user.server.rpc.RemoteServiceServlet;
/**
* The server side implementation of the RPC service.
*/
@SuppressWarnings("serial")
public class GreetingServiceImpl extendsRemoteServiceServlet implements
GreetingService
{
Connection con=null;
Statement st=null;
ResultSet rs=null;
String query;
String url="jdbc:mysql://localhost:3306/gwtlogin";
public void call()
{
try
{
Class.forName("com.mysql.jdbc.Driver");
}
catch(ClassNotFoundException e)
{
System.out.print(e.getMessage());
}
try
{
con=DriverManager.getConnection(url, "root","");
st=con.createStatement();
}
catch(SQLException e)
{
System.out.println(e.getMessage());
}
}
@Override
public String upload(String topic, String stream, String Tag, String plink,
String fpath) {
// TODO Auto-generated method stub
call();
String ss="no";
int k=0;
FileInputStream fis = null;
PreparedStatement pstmt = null;
System.out.println(fpath);
try
{
System.out.println("Hello try");
con.setAutoCommit(false);
File file = new File(fpath);
fis = new FileInputStream(file);
pstmt = con.prepareStatement("insert into uploadfile(Topic, Stream, Tag, PublicLink, FileBody ) values ( ?, ?, ?, ?, ?)");
//pstmt.setString(1, id);
pstmt.setString(1, topic);
pstmt.setString(2, stream);
pstmt.setString(3, Tag);
pstmt.setString(4, plink);
pstmt.setAsciiStream(5, fis, (int) file.length());
k=pstmt.executeUpdate();
System.out.println("Bye");
if(k==1)
{
ss="yes";
}
con.commit();
}
catch (Exception e)
{
System.err.println("Error: " + e.getMessage());
e.printStackTrace();
}
finally {
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return ss;
}
}
I hope you will able to store file into database. If any error has occurred or any question comes in your mind. Freely ask your problem by comment we just solve your problem within 1 hour.
No comments:
Post a Comment