Create Upload Form 2
UploadImage.java
@ManagedBean(name = "uploadImage")
@RequestScoped
public class UploadImage implements Serializable {
private Connection con = null;
private Statement stmt;
public UploadImage() {
try{
String host = "jdbc:mysql://localhost:3306/DATABASE NAME"; //host here
String username = "root"; //username
String password = "a"; //password
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection( host, username, password );
}
catch(SQLException err){
System.out.println(err.getMessage());
} catch (ClassNotFoundException ex) {
//Logger.getLogger(db.class.getSsid()).log(Level.SEVERE, null, ex);
}
}
private static final long serialVersionUID = 1L;
private UploadedFile file;
private String company_name;
private String account_id;
private String address;
public Connection getCon() {
return con;
}
public void setCon(Connection con) {
this.con = con;
}
public Statement getStmt() {
return stmt;
}
public void setStmt(Statement stmt) {
this.stmt = stmt;
}
public String getCompany_name() {
return company_name;
}
public void setCompany_name(String company_name) {
this.company_name = company_name;
}
public String getAccount_id() {
return account_id;
}
public void setAccount_id(String account_id) {
this.account_id = account_id;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public UploadedFile getFile() {
return file;
}
public void setFile(UploadedFile file) {
this.file = file;
}
public void upload() {
if (file != null) {
try {
System.out.println(file.getFileName());
InputStream fin2 = file.getInputstream();
PreparedStatement pre = con.prepareStatement("insert into TABLENAME (company_name, "
+ "account_id, address, image_name,image) values"
+ "(?,?,?,?,?)");
pre.setString(1, company_name);
pre.setString(2, account_id);
pre.setString(3, address);
pre.setString(4, company_name+file.getFileName().toString());
pre.setBinaryStream(5, fin2, file.getSize());
pre.executeUpdate();
System.out.println("Inserting Successfully!");
pre.close();
FacesMessage msg = new FacesMessage("Succesful! Data is Saved into the Database.");
FacesContext.getCurrentInstance().addMessage(null, msg);
//MyEmail e = new MyEmail("IP ADDRESS IN YOUR SERVER","sending mail address",to"reciever");
MyEmail e = new MyEmail("gmail","sender@gmail.com","receiver@gmail.com");
e.SendEmailMsg("Form is completed", " "+company_name+" company.");
} catch (Exception e) {
System.out.println("Exception-File Upload." + e.getMessage());
}
}
else{
FacesMessage msg = new FacesMessage("Please select image!!");
FacesContext.getCurrentInstance().addMessage(null, msg);
}
}
}
@ManagedBean(name = "uploadImage")
@RequestScoped
public class UploadImage implements Serializable {
private Connection con = null;
private Statement stmt;
public UploadImage() {
try{
String host = "jdbc:mysql://localhost:3306/DATABASE NAME"; //host here
String username = "root"; //username
String password = "a"; //password
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection( host, username, password );
}
catch(SQLException err){
System.out.println(err.getMessage());
} catch (ClassNotFoundException ex) {
//Logger.getLogger(db.class.getSsid()).log(Level.SEVERE, null, ex);
}
}
private static final long serialVersionUID = 1L;
private UploadedFile file;
private String company_name;
private String account_id;
private String address;
public Connection getCon() {
return con;
}
public void setCon(Connection con) {
this.con = con;
}
public Statement getStmt() {
return stmt;
}
public void setStmt(Statement stmt) {
this.stmt = stmt;
}
public String getCompany_name() {
return company_name;
}
public void setCompany_name(String company_name) {
this.company_name = company_name;
}
public String getAccount_id() {
return account_id;
}
public void setAccount_id(String account_id) {
this.account_id = account_id;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public UploadedFile getFile() {
return file;
}
public void setFile(UploadedFile file) {
this.file = file;
}
public void upload() {
if (file != null) {
try {
System.out.println(file.getFileName());
InputStream fin2 = file.getInputstream();
PreparedStatement pre = con.prepareStatement("insert into TABLENAME (company_name, "
+ "account_id, address, image_name,image) values"
+ "(?,?,?,?,?)");
pre.setString(1, company_name);
pre.setString(2, account_id);
pre.setString(3, address);
pre.setString(4, company_name+file.getFileName().toString());
pre.setBinaryStream(5, fin2, file.getSize());
pre.executeUpdate();
System.out.println("Inserting Successfully!");
pre.close();
FacesMessage msg = new FacesMessage("Succesful! Data is Saved into the Database.");
FacesContext.getCurrentInstance().addMessage(null, msg);
//MyEmail e = new MyEmail("IP ADDRESS IN YOUR SERVER","sending mail address",to"reciever");
MyEmail e = new MyEmail("gmail","sender@gmail.com","receiver@gmail.com");
e.SendEmailMsg("Form is completed", " "+company_name+" company.");
} catch (Exception e) {
System.out.println("Exception-File Upload." + e.getMessage());
}
}
else{
FacesMessage msg = new FacesMessage("Please select image!!");
FacesContext.getCurrentInstance().addMessage(null, msg);
}
}
}
Post a Comment