Will you any plz tell me..
How to insert data into mysql using jsf with all coding..
Thank you...
Printable View
Will you any plz tell me..
How to insert data into mysql using jsf with all coding..
Thank you...
It is good practice to separate the presentation and data access aspects of your program.
This way, should you ever need to change the database implementation, you don't have to re-write all your presentation code as well.
Try http://download.oracle.com/javase/tutorial/jdbc/ to learn the basics of JDBC - you will need to use this to access the database.
I was asked to you how to insert data into mysql db using JSF three tier architecture... ???? ?
Plz if any one knw this ansr... then only reply me...
Thanks
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<f:view>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h:form>
<table border="1" width="770" align="center">
<tr>
<td align="center">
<h:outputText value="Registration Form" style="font-size:25px;"></h:outputText>
</td>
</tr>
</table>
<table border="0" width="770" align="center" >
<tr>
<td>
<h:outputText value="Application Number"></h:outputText>
</td>
<td colspan="2"><h:inputText id="apply_no" size="25" maxlength="50" value="#{applicationFormBean.application_id }"></h:inputText></td>
<td align="right" ><h:inputTextarea rows="10" cols="15" id="photo" ></h:inputTextarea></td>
</tr>
</table>
<table border="1" width="770" align="center">
<tr>
<td><b><h:outputText value="Name of The Applicant:"></h:outputText></b></td>
<td colspan="2"><h:inputText id="stuname" size="25" maxlength="100" value="#{applicationFormBean.stuname }"></h:inputText></td>
</tr>
<td align="center">
<h:commandButton value="Submit" action="#{applicationFormBean.InsertData }">
</h:commandButton>
<h:commandButton value="Reset" ></h:commandButton>
</td>
</tr>
</table>
</h:form>
//////////////This is the BeanClass ............
package bean;
import DAO.UserConnect;
public class ApplicationFormBean
{
private String application_id;
private String stuname;
public String getApplication_id() {
return application_id;
}
public void setApplication_id(String application_id) {
this.application_id = application_id;
}
public String getStuname() {
return stuname;
}
public String InsertData()
{
UserConnect dot = new UserConnect();
System.out.println("Connection =====" + dot);
String st ="";
try
{
st =dot.InsertApplicationData( );
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return st;
}
}
///////////This is the java connectivity Class
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 java.util.Locale;
import java.util.ResourceBundle;
import org.omg.CORBA.Request;
import bean.ApplicationFormBean;
public class UserConnect {
ResourceBundle bundl = null;
String sum = "sum";
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
public UserConnect() {
String driver = "org.gjt.mm.mysql.Driver";
bundl = ResourceBundle.getBundle("datasource");
String test = bundl.getString("ds.localhostrmm-ds.driver");
try {
Class.forName(driver).getInterfaces();
String url = "jdbc:mysql://localhost/test?user=root&password=root";
try {
con = DriverManager.getConnection(url);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
} catch (Exception e) {
// TODO: handle exception
}
}
public String InsertApplicationData()
{
try
{
ApplicationFormBean f = null;
String url = "jdbc:mysql://localhost/test?user=root&password=root";
PreparedStatement st = null;
ResultSet rs = null;
if(rs.next())
{
/*String id =rs.getString("apply_no");
System.out.println("id ====");
String name=rs.getString("stuname");*/
String query = "insert into application_table(appli_id,name) values( ? ,?)";
st = con.prepareStatement(query);
System.out.println("Before Values "+ st);
st.setString(1, f.getApplication_id());
System.out.println("Values ====" + f.getApplication_id());
st.setString(2, f.getStuname());
rs=st.executeQuery();
con.close();
}
}
catch (Exception e) {
System.out.println("Unsuccess........");
}
return "Success";
}
public String savedata()
{
return "ab";
}
}
/// Main problem is i am not able to understand how to get values from jsf page and how to set values in to bean then insert the values...
Plz any one Give right solution ....
Thank you.....
You have to create a bean class and reference it in the faces-config.xml. Create the bean class (nothing special unless you are using some tag library).
Referencing it in the faces-config.xml is simple, just add a managed bean:
The MyBean is the Java class that holds the properties you want to be used.Code:<managed-bean>
<managed-bean-name>mybean</managed-bean-name>
<managed-bean-class>com.package.MyBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
If your class has a field 'apply_no' it will be populated when the bean is submitted. The values of this bean can then be used and accessed.Code:
<h:form id="myform">
<h:inputText value="#{mybean.apply_no}" />
<h:commandButton action="submit" value="Submit" />
</h:form>
Try reading this simple tutorial: http://exadel.com/web/portal/jsftutorial-kickstart
Hi...
Thanks a lot... This code was helped me ...
There is another problem ... in jsf dropdwon box...
<td><h:selectOneMenu id="birthday_day" value="#{applicationFormBean.birthday_day }">
<f:selectItem itemLabel="Day" itemValue="Day" />
<f:selectItem itemLabel="1" itemValue="1" />
<f:selectItem itemLabel="2" itemValue="2" />
<f:selectItem itemLabel="3" itemValue="3" />
<f:selectItem itemLabel="4" itemValue="4" />
<f:selectItem itemLabel="5" itemValue="5" />
<f:selectItem itemLabel="6" itemValue="6" />
<f:selectItem itemLabel="7" itemValue="7" />
<f:selectItem itemLabel="8" itemValue="8" />
<f:selectItem itemLabel="9" itemValue="9" />
<f:selectItem itemLabel="10" itemValue="10" />
</h:selectOneMenu>
I wrote this code... Drop down box is populating.. all values 1 .. to 10 ..
My question is i want to give limitation for display in dropdown.. As i can say i want to see only five values...
How to give the limitation in dropdown ? when the our jsf page is runing.
Plz any one help me...
thanks
Sorry for getting back to you late on this, I have been away.
In order to regulate the options you need to do it on the back end (inside of the managed bean). What you would normally do is either set a class level variable that can be accessed on the page (via mybean.myvariable), or you add it to the request and access the values like that. Personally option 1 is probably the best way to go.