1.POJO班 2.Mapping文件 3.配置文件 4.Client APP 5.Jar文件 6.错误
1.POJO CLASS- Employee.java
public class Employee {
private int eno;
private String ename;
private float esal;
private String eaddr;
public int getEno() {
return eno;
}
public void setEno(int eno) {
this.eno = eno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public float getEsal() {
return esal;
}
public void setEsal(float esal) {
this.esal = esal;
}
public String getEaddr() {
return eaddr;
}
public void setEaddr(String eaddr) {
this.eaddr = eaddr;
}
}
2.Mapping file-Employee.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.durgasoft.hbn.pojo.Employee" table="emp1">
<id name="eno" column="eno"/>
<property name="ename"/>
<property name="esal"/>
<property name="eaddr"/>
</class>
</hibernate-mapping>
3.配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property
name="connection.driver_Class">oracle.jdbc.OracleDriver</property>
<property
name="connection.url">jdbc:oracle:thin;@1521:demodb</property>
<property name="connection.username">system</property>
<property name="connection.password">durga</property>
<property
name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
<mapping resource="com/durgasoft/hbn/mappings/Employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
4.Client APP-ClientApp.java
package com.durgasoft.hbn.test;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
import com.durgasoft.hbn.pojo.Employee;
public class ClientApp {
public static void main(String[] args)throws Exception {
Configuration cfg = new Configuration();
cfg.configure("/com/durgasoft/hbn/cfgs/hibernate.cfg.xml");
SessionFactory session_Factory = cfg.buildSessionFactory();
Session session = session_Factory.openSession();
Transaction tx = session.beginTransaction();
Employee emp = new Employee();
emp.setEno(111);
emp.setEname("AAA");
emp.setEsal(5000);
emp.setEaddr("Hyd");
session.save(emp);
tx.commit();
System.out.println("Employee Record inserted succesfully");
session.close();
session_Factory.close();
}
}
5.jar文件 Hibernate jar文件 - 我)hibernate.jar文件 ⅱ)ANTLR-2.7.6.jar 三)公共的集合-3.1.jar ⅳ)DOM4J-1.6.1.jar V)Javassist进行-3.12.0.GA.jar ⅵ)JTA-1.1.jar ⅶ)SLF4J-API-1.6.1.jar ⅷ)冬眠-JPA-2.0-API-1.0.1.Final.jar Oracle JDBC驱动程序: - 我)ojdbc7.jar I)SLF4J-简单1.6.1-sources.jar
6.错误 -
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further
details.
Exception in thread "main" org.hibernate.exception.GenericJDBCException:
Cannot open connection
at
org.hibernate.exception.SQLStateConverter.handledNonSpecificException
(SQLStateConverter.java:140)
at
org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:128)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:52)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:449)
at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)
at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:160)
at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:81)
at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1473)
at com.durgasoft.hbn.test.ClientApp.main(ClientApp.java:20)
Caused by: java.sql.SQLException: Invalid Oracle URL specified
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:453)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:133)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446)
... 5 more
有人可以帮助我解决这个错误(我刚接触休眠)
答案 0 :(得分:0)
此错误表示您的应用无法连接到主机网址,因为该网址无法理解。在你的第三个文件中试试这个名字=&#34; connection.url&#34;&gt; jdbc:oracle:thin:@localhost:1521:demodb。
答案 1 :(得分:0)
你有一个分号,在线查看几个样本网址。
我想它应该是
alias apps='dpkg -l | awk "{print $2 "\t" $3}" | fzf'