List of usage examples for org.hibernate SessionFactory openSession
Session openSession() throws HibernateException;
From source file:doc_profile.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods./* www . j a v a 2 s .c o m*/ * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { SessionFactory sf = HibernateUtil.getSessionFactory(); Session ss1 = sf.openSession(); Transaction tr1 = ss1.beginTransaction(); // tr1.begin(); HttpSession hs = request.getSession(true); if (hs.getAttribute("doctor") != null) { Doctor pi = (Doctor) hs.getAttribute("doctor"); if (request.getParameter("btn_update") != null) { Doctor dd = (Doctor) ss1.get(Doctor.class, pi.getDId()); Addressdetails ad = dd.getAddressid(); String line = request.getParameter("line1"); ad.setLine1(line.trim()); ad.setLine2(request.getParameter("line2").trim()); ad.setPincode(request.getParameter("pincode").trim()); ss1.saveOrUpdate(ad); dd.setDFirstname(request.getParameter("p_firstname").trim()); dd.setDLastname(request.getParameter("p_lastname").trim()); dd.setGender(request.getParameter("gender").trim()); dd.setBirthDate(request.getParameter("birth_date").trim()); dd.setContactnum(request.getParameter("contact_num").trim()); dd.setEmailId(request.getParameter("email_id").trim()); ss1.saveOrUpdate(dd); pi = dd; } tr1.commit(); request.setAttribute("pdata", pi); RequestDispatcher rd = request.getRequestDispatcher("Doctor_myprofile.jsp"); rd.forward(request, response); } else { tr1.commit(); RequestDispatcher rd = request.getRequestDispatcher("login.jsp"); rd.forward(request, response); } } catch (HibernateException he) { out.println(he.getMessage()); } finally { out.close(); } }
From source file:searchprop_byname.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods.//from ww w . j ava 2s. c om * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { SessionFactory sf = NewHibernateUtil.getSessionFactory(); Session ss = sf.openSession(); String searchedprop = request.getParameter("propname"); Criteria cr = ss.createCriteria(PropDetail.class); cr.add(Restrictions.like("pName", "%" + searchedprop + "%")); ArrayList<PropDetail> pd = (ArrayList<PropDetail>) cr.list(); /* for(PropDetail p: pd) { out.print(p.getPId()+" type:"+p.getPType()+" bhk:"+p.getPBhk()); out.println(" "); } */ if (pd.isEmpty()) { out.print("no such property"); String msg = "Sorry, No results found."; request.setAttribute("error", msg); } else { request.setAttribute("proplist", pd); } RequestDispatcher rd = request.getRequestDispatcher("getstate?id=9"); rd.forward(request, response); } catch (Exception e) { out.println(e.getMessage()); } }
From source file:a.A.java
/** * @param args the command line arguments *//*w w w . j a va 2s . c o m*/ public static void main(String[] args) { // TODO code application logic here String URL = "jdbc:mysql://localhost/MyBD"; String USER = "root"; String PASSWORD = "root"; String DRIVER_CLASS = "com.mysql.jdbc.Driver"; try { Class.forName(DRIVER_CLASS); } catch (ClassNotFoundException e) { e.printStackTrace(); } Connection connection = null; try { connection = DriverManager.getConnection(URL, USER, PASSWORD); } catch (SQLException e) { System.out.println("ERROR: Unable to Connect to Database."); } Configuration cfg = new Configuration(); cfg.configure("a/hibernate.cfg.xml");//populates the data of the configuration file //creating seession factory object ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()) .build(); SessionFactory factory = cfg.buildSessionFactory(serviceRegistry); String line; String csv = "C:\\Users\\hp\\Desktop\\GeoLiteCity-Location.csv"; try (BufferedReader br = new BufferedReader(new FileReader(csv))) { count = 0; City e1 = new City(); while ((line = br.readLine()) != null) { if (count > 1) { Session session = factory.openSession(); // creating transaction object Transaction t = session.beginTransaction(); System.out.println(line); String[] s = line.split("[,]"); System.out.println(s[0]); System.out.println(s[1]); System.out.println(Float.parseFloat(s[5])); System.out.println(Float.parseFloat(s[6])); e1.setId(Integer.parseInt(s[0])); e1.setName(s[1]); e1.setC(s[3]); e1.setlat(Float.parseFloat(s[5])); e1.setlon(Float.parseFloat(s[6])); session.save(e1); t.commit();//transaction is commited session.close(); } else { count++; } } // Session currentSession = factory.openSession(); // List <City> list=null; // // while(list==null){ // list = currentSession.createCriteria(City.class) // .add(Restrictions.eq("Name", "NL")) // .list(); // for(int i=0;i<list.size();i++){ // // System.out.println(list.get(i)); // } } catch (IOException e) { e.printStackTrace(); } String a, b; float lats = 0; float lats2 = 0; float longs = 0; float longs2 = 0; System.out.print("Enter name of first city"); Distance d1 = new Distance(); Scanner scanner = new Scanner(System.in); a = scanner.nextLine(); a = "\"" + a + "\""; System.out.print("Enter name of second city"); //Scanner scanner=new Scanner(System.in); b = scanner.nextLine(); b = "\"" + b + "\""; Transaction tx; Session session = factory.openSession(); try { // int index=0; tx = session.beginTransaction(); List employees = session.createQuery("FROM City").list(); for (Iterator iterator = employees.iterator(); iterator.hasNext();) { City c = (City) iterator.next(); if (a.contentEquals(c.getC())) { System.out.print("Name: " + c.getName()); System.out.print(" City: " + c.getC()); lats = c.getlon(); longs = c.getlat(); System.out.print(" Longitude: " + c.getlon()); System.out.println(" Latitude: " + c.getlat()); break; } } List<String> citynames = new ArrayList<String>(); List<Double> doubleList = new ArrayList<Double>(); System.out.println("Enter number of cities"); Scanner s = new Scanner(System.in); int num = Integer.parseInt(s.nextLine()); Distance obj = new Distance(); int i = 0; for (Iterator iterator = employees.iterator(); iterator.hasNext();) { City c = (City) iterator.next(); if (i < num) { doubleList.add(obj.GreatD(lats, longs, c.getlat(), c.getlon())); citynames.add(c.getC()); i++; } else { //get max value from list Integer j = 0, maxIndex = -1; Double max = null; for (Double x : doubleList) { if ((x != null) && ((max == null) || (x > max))) { max = x; maxIndex = j; } j++; } double dare = obj.GreatD(lats, longs, c.getlat(), c.getlon()); if (dare < max) { citynames.set(maxIndex, c.getC()); doubleList.set(maxIndex, dare); //list } } // For first n values add to list } for (int k = 0; k < citynames.size(); k++) { System.out.println(citynames.get(k)); } // int check = 0; for (Iterator iterator = employees.iterator(); iterator.hasNext();) { City c = (City) iterator.next(); if (check == 2) { break; } else { if (b.contentEquals(c.getC())) { System.out.print("Name: " + c.getName()); System.out.print(" City: " + c.getC()); longs = c.getlon(); lats = c.getlat(); System.out.print(" Longitude: " + c.getlon()); System.out.println(" Latitude: " + c.getlat()); check++; } if (a.contentEquals(c.getC())) { System.out.print("Name: " + c.getName()); System.out.print(" City: " + c.getC()); longs2 = c.getlon(); lats2 = c.getlat(); System.out.print(" Longitude: " + c.getlon()); System.out.println(" Latitude: " + c.getlat()); check++; } } } System.out.println(d1.GreatD(lats, longs, lats2, longs2)); tx.commit(); return; } catch (Exception e) { e.printStackTrace(); } }
From source file:A.NET.DAO.IMPL.UsuarioDaoImpl.java
@Override public Usuario validarusuario(String usuario, String password) { Usuario user = null;// w w w.j a v a2s .c o m Session session = null; SessionFactory sf = null; try { sf = HibernateUtil.getSessionFactory(); session = sf.openSession(); Query query = session.createQuery( "from Usuario where usuario='" + usuario + "' and password='" + password + "' and estado='1'"); user = (Usuario) query.uniqueResult(); session.close(); } catch (Exception e) { System.out.println("Error!!!!!:" + e.getMessage()); e.printStackTrace(); session.close(); } return user; }
From source file:A.NET.DAO.IMPL.VentaDaoImpl.java
@Override public Persona buscarPersona(String dni) { Persona persona = null;//from w w w . j av a2s. c o m SessionFactory sf = null; Session session = null; try { sf = HibernateUtil.getSessionFactory(); session = sf.openSession(); Query query = session.createQuery("from Persona where nro_d='" + dni + "'"); persona = (Persona) query.uniqueResult(); session.close(); } catch (Exception e) { e.printStackTrace(); session.close(); } return persona; }
From source file:addCE.ExcelReader.java
public String certcode() { List<Attendee> attendees = new ArrayList<Attendee>(); SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); Session session = sessionFactory.openSession(); StringBuilder sb = new StringBuilder(); String candidateChars = "abcdefghijklmnopqrstuvwxyz0123456789"; do {/*w ww . j av a 2 s .c om*/ Random random = new Random(); for (int i = 0; i < 30; i++) { sb.append(candidateChars.charAt(random.nextInt(candidateChars.length()))); } String sql = "from Attendee p where p.certCode = :certCode"; attendees = session.createQuery(sql).setParameter("certCode", sb.toString()).list(); } while (!attendees.isEmpty()); session.close(); return sb.toString(); }
From source file:addCE.ExcelReader.java
public static void main(String args[]) { ExcelReader creader = new ExcelReader(); creader.read();/*from ww w . ja v a 2 s. co m*/ SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); Session session = sessionFactory.openSession(); int id = 0; for (ArrayList<String> l1 : creader.attendees) { if (l1.get(0) != "None") { for (String n : l1) { System.out.print(n + " "); } System.out.println(); Attendee attendee = new Attendee(); attendee.setId(id); attendee.setFirstName(l1.get(0)); attendee.setLastName(l1.get(1)); attendee.setProfession(l1.get(2)); attendee.setLicenseNum(l1.get(3)); attendee.setCountry(l1.get(4)); attendee.setAccredDate(l1.get(5)); attendee.setCreditType(l1.get(6)); attendee.setNumCredits(l1.get(7)); attendee.setEmailAddr(l1.get(8)); attendee.setCertCode(creader.certcode()); session.beginTransaction(); session.save(attendee); session.getTransaction().commit(); id += 1; } } System.out.println(creader.attendees.size()); session.close(); }
From source file:agenda_hibernate.controlador.Operaciones.java
public void altaUsuarios(Usuarios user) { SessionFactory sesion = NewHibernateUtil.getSessionFactory(); Session session;/*from ww w. j av a 2 s . c om*/ session = sesion.openSession(); Transaction tx = session.beginTransaction(); session.save(user); tx.commit(); session.close(); JOptionPane.showMessageDialog(null, "Insertado correctamente."); }
From source file:agenda_hibernate.controlador.Operaciones.java
public void altaNoticias(Noticias noti) { SessionFactory sesion = NewHibernateUtil.getSessionFactory(); Session session;/*from w w w . j ava2 s. c o m*/ session = sesion.openSession(); Transaction tx = session.beginTransaction(); session.save(noti); tx.commit(); session.close(); JOptionPane.showMessageDialog(null, "Insertado correctamente."); }
From source file:agenda_hibernate.controlador.Operaciones.java
public Usuarios buscaUser(String usuario) { Usuarios usr;//from www. j av a 2 s . c o m SessionFactory sesion = NewHibernateUtil.getSessionFactory(); Session session; session = sesion.openSession(); Transaction tx = session.beginTransaction(); usr = (Usuarios) session.get(Usuarios.class, usuario); tx.commit(); session.close(); return usr; }