0% found this document useful (0 votes)
25 views1 page

Booktest

This Java code defines a BookTest class that takes book information as input from the user via a Scanner. It uses Hibernate to connect to a database configured in hibernate.cfg.xml, begin a transaction, save a new Book object to the database with the input attributes, and commit the transaction. On success, it prints a message confirming the data was saved.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views1 page

Booktest

This Java code defines a BookTest class that takes book information as input from the user via a Scanner. It uses Hibernate to connect to a database configured in hibernate.cfg.xml, begin a transaction, save a new Book object to the database with the input attributes, and commit the transaction. On success, it prints a message confirming the data was saved.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

package com.

oit;

import java.util.Scanner;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class BookTest


{
public static void main(String[] args)
{
int id,price;
String bname,author;

Scanner sc=new Scanner(System.in);


System.out.println("Enter ID - Name - Author - Price :-");
id=sc.nextInt();
bname=sc.next();
author=sc.next();
price=sc.nextInt();

Configuration config=new Configuration();


config.configure("hibernate.cfg.xml");

SessionFactory sessionfactory=config.buildSessionFactory();
Session session=sessionfactory.openSession();
Transaction tr=session.beginTransaction();

Book b=new Book();

b.setId(id);
b.setBname(bname);
b.setPrice(price);
b.setAuthor(author);

session.save(b);
tr.commit();
System.out.println("DATA SAVE SUCCESSFULLY");

}
}

You might also like