We provide complete mobile and web apps development solutions

Showing posts with label database. Show all posts
Showing posts with label database. Show all posts

Wednesday, October 3, 2012

Steps to develop Hibernate Application:


Steps to develop Hibernate Application:
1.     Develop entity class for each table.
2.     For each entity , develop a mapping file.
3.     Develop configuration file.
4.     Add hibernate framework jar files to class path.
5.     Using hibernate api, perform persistent operations.

How to use Hibernate Api to perform persistent operations:


We have some steps to use hibernate api

1.     Create configuration object
Configuration configuration=new configuration();

2.     Read configuration file along with mapping file using configure method.
Configuration.configure();

3.     Build SessionFactory from configuration.

SessionFactory factory=configuration.buildSessionFactory();

SessionFactory is thread safe immutable cache of compiled

4.     Get Session from SessionFactory object.

Session session=factory.openSession();

         Session (org.hibernate.Session)

        A single-threaded  Short lived object representing a conversation      between app and persistent store.


5.     Perform persistent operations.
Session save(s)
Session update(s)
Session load(s)
Session delete(s)
6.     Close the session.

Session.close();

Tuesday, October 2, 2012

Hibernate First Program


Create a Java Project and give name

Steps:
1.Click file in the menu-bar / New / Project .
2.Select Java / Java Project
3. Give name for project
4.  Click Finish

Add Hibernate Jar files


To build Hibernate Application, we need

1.     Persistent classes, for representing database entity tables.
2.     Mapping files.
3.     Hibernate configuration settings. This will show  how to connect and manage the database.

Create the persistent class for entity class name.

Public class className
{
private int ID;
 private String firstName;
 private String lastName;
public int getID() {
 return ID;
}
public void setID(int id)
 {
ID = id;
}
public String getFirstName()
{
return firstName;
}
public void setFirstName(String firstName)
{
this.firstName = firstName;
 }
public String getLastName()
{
 return lastName;
} public void setLastName(String lastName)
 {
 this.lastName = lastName;
}
 }

Create a mapping for for table

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
 <hibernate-mapping>
<class name="Lecturer" table="TableName">
<id name="ID" type="int">
 <generator class="increment"/> </id>
 <property name="firstName" column="FirstName" type="string"/>
<property name="lastName" column="LastName" type="string"/>
</class> </hibernate-mapping>

Create hibernate.cfg.xml

<?xml version='1.0' encoding='UTF-8'?>
 <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory>
<property name="connection.url">jdbc_URL</property>
<property name="connection.username">jdbc_Username</property>
<property name="connection.password">jdbc_Password</property>
 <property name="connection.driver_class"> sun.jdbc.odbc.JdbcOdbcDriver </property> <property name="dialect"> org.hibernate.dialect.SQLServerDialect </property> <property name="current_session_context_class">thread</property>
<!-- ___ Defining the Mapping Files ___________ -->
<mapping resource="mappingfilename.xml" /> </session-factory> </hibernate-configuration>

import org.hibernate.Session;
 import org.hibernate.SessionFactory;
 import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class SimpleTest
{
 public static void main(String[] args)
{
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
 Session session = sessionFactory.getCurrentSession();
Transaction transx = session.beginTransaction();
classname refclass = new classname();
refclass.setFirstName("srinivas"); refclass.setLastName(“N"); session.save(refclass);
transx.commit(); System.out.println ("The " + refclass.getFirstName()+ " " + refclass.getLastName()+" is successfully added to your database"); } }
Seo Directory List

Monday, September 17, 2012

Data Storage in Android

Data Storage:

Android stores data in 5 ways depends on the data storage

sharedpreferences
internal storage
external storage
sqlite database
network connection


sharedpreferences: it will store the data as a key value pairs

to get SharedPreferences of ur app, use these

getSharedPreferences()  - use this if u need multiple preferences

getPreferences()- use this if u need single preference.

you can find shared preferences example here 


https://www.4shared.com/zip/AEe4gg63/SharedPreferences.html?

Internal Storage:


InputStream()- for reading the data
OutputStream()- for writing the data to the stream


1.openFileOutput() - we call the particular file.

2. write to the file with  write()
3. close the file after writing -   close()

String FILENAME= "hello_file";
Strig string="helloworld";

FileOutputStream fos=openFileOutput(FILENAME, Context.MODE_PRIVATE);

fos.write(string.getBytes());

fos.close();



getDir()- create directory in the internal storage

deleteFile()- deletes the directory



External Storage-


boolean mExternalStorageAvilable= false;
boolean mExternalStorageWritable= false;


if(Environment.MEDIA_MOUNTED.equals(state)) {

mExternalStorageAvilable=mExternalStorageWritable= true;
}
else
if(Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)){

mExternalStorageAvilable=true;
mExternalStorageWritable= false;











 









SqliteOpenHelper



public class namedir extends SqliteOpenHelper
{

private static final int DATABASE_VERSION=2;
private static final String NAME_TABL="namedir";



@override


public void onCreate(SqliteDatabase db)
{
db.execSQL("CREATE TABLE NAMEDIR(id varchar(20), image BLOB, caption varchar(30), description varchar(100))");
}
private void VersionUpdation(SQLiteDatabase db)
{
}
public boolean CheckDataBase(String db)
{
SQLiteDatabase checkDB=null;
}






Online Training

Your Name :
Your Email: (required)
Your Message: (required)

Powered by Blogger.

Recent Posts

Find Us On Facebook

Popular Posts