You are on page 1of 3

Pojo class:

package Anno;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "studet")
public class Anno1 {
@Column(name = "username")
public String name;
@Id
@Column(name = "id")
@GeneratedValue(strategy=GenerationType.AUTO)
public int id;
@Column(name = "rollno")
public String rollno;
@Column(name = "phno")
public String phno;
@Column(name = "address")
public String address;
@Column(name = "dob")
public String dob;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getRollno() {
return rollno;
}
public void setRollno(String rollno) {
this.rollno = rollno;
}
public String getPhno() {
return phno;
}
public void setPhno(String phno) {
this.phno = phno;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}

Main class:
package Anno;
import org.hibernate.*;
import org.hibernate.cfg.Configuration;
import java.util.*;

public class Anno2 {


public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String name=sc.next();
int id=sc.nextInt();
String rollno=sc.next();
String phno=sc.next();
String address=sc.next();
String dob=sc.next();
Anno1 ob=new Anno1();
ob.setAddress(address);
ob.setDob(dob);
ob.setId(id);
ob.setName(name);
ob.setPhno(phno);
ob.setRollno(rollno);
try
{
@SuppressWarnings("deprecation")
SessionFactory sessionFactory = new
Configuration().configure("Student.cfg.xml").buildSessionFactory();
Session s = sessionFactory.openSession();
Transaction tx = s.beginTransaction();
s.save(ob);
tx.commit();
s.close();
}
catch (Exception e)
{
System.out.println(e.getMessage());
System.err.println("Initial SessionFactory creation
failed." + e);
}
System.out.println("Added to Database");
}
}

Configuration class:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="Studentsessionfactory">
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<property name="connection.pool_size">5</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<mapping resource="Anno1.java" />
</session-factory>
</hibernate-configuration>

You might also like