0 [main] INFO net.sf.hibernate.cfg.Environment - Hibernate 2.1.8
16 [main] INFO net.sf.hibernate.cfg.Environment - loaded properties from resource hibernate.properties: {hibernate.connection.username=root, hibernate.connection.autocommit=true , hibernate.connection.password=root, hibernate.cglib.use_reflection_optimizer=true, hibernate.dialect=net.sf.hibernate.dialect.MySQLDialect, hibernate.show_sql=true, hibernate.connection.url=jdbc:MySQL://localhost:3306/SAMPLEDB, hibernate.connection.driver_class=com.mysql.jdbc.Driver}
16 [main] INFO net.sf.hibernate.cfg.Environment - using CGLIB reflection optimizer
16 [main] INFO net.sf.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling
31 [main] INFO net.sf.hibernate.cfg.Configuration - Mapping resource: mypack/Customer.hbm.xml
这里应该是主要问题,不知道什么原因!
391 [main] ERROR net.sf.hibernate.cfg.Configuration - Could not configure datastore from input stream
org.dom4j.DocumentException: unknown protocol: htpp Nested exception: unknown protocol: htpp
at org.dom4j.io.SAXReader.read(SAXReader.java:358)
at net.sf.hibernate.cfg.Configuration.addInputStream(Configuration.java:287)
at net.sf.hibernate.cfg.Configuration.addClass(Configuration.java:355)
at mypack.BusinessService.<clinit>(BusinessService.java:18)
Nested exception:
java.net.MalformedURLException: unknown protocol: htpp
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at org.apache.xerces.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
at org.apache.xerces.impl.XMLEntityManager.startEntity(Unknown Source)
at org.apache.xerces.impl.XMLEntityManager.startDTDEntity(Unknown Source)
at org.apache.xerces.impl.XMLDTDScannerImpl.setInputSource(Unknown Source)
at org.apache.xerces.impl.XMLDocumentScannerImpl$DTDDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.dom4j.io.SAXReader.read(SAXReader.java:339)
at net.sf.hibernate.cfg.Configuration.addInputStream(Configuration.java:287)
at net.sf.hibernate.cfg.Configuration.addClass(Configuration.java:355)
at mypack.BusinessService.<clinit>(BusinessService.java:18)
net.sf.hibernate.MappingException: Error reading resource: mypack/Customer.hbm.xml
at net.sf.hibernate.cfg.Configuration.addClass(Configuration.java:358)
at mypack.BusinessService.<clinit>(BusinessService.java:18)
Caused by: net.sf.hibernate.MappingException: org.dom4j.DocumentException: unknown protocol: htpp Nested exception: unknown protocol: htpp
at net.sf.hibernate.cfg.Configuration.addInputStream(Configuration.java:297)
at net.sf.hibernate.cfg.Configuration.addClass(Configuration.java:355)
... 1 more
Caused by: org.dom4j.DocumentException: unknown protocol: htpp Nested exception: unknown protocol: htpp
at org.dom4j.io.SAXReader.read(SAXReader.java:358)
at net.sf.hibernate.cfg.Configuration.addInputStream(Configuration.java:287)
... 2 more
package mypack;
import javax.servlet.*;
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.Configuration;
import java.io.*;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.*;
public static SessionFactory sessionFactory;
/** 初始化Hibernate,创建SessionFactory实例 */
static{
try {
//根据默认位置的Hibernate配置文件的配置信息,创建一个Configuration实例
Configuration config = new Configuration();
config.addClass(Customer.class); 这里为第18行
//创建SessionFactory实例
sessionFactory = config.buildSessionFactory();
} catch(Exception e) {
e.printStackTrace();
}
}
/** 查询所有的Customer对象,然后调用printCustomer()方法打印Customer对象信息 */
public void findAllCustomers(ServletContext context, OutputStream out) throws Exception {
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
try {
List customer = session.find("from Customer as c order by c.name asc");
for(Iterator it = customer.iterator(); it.hasNext();) {
printCustomer(context, out, (Customer)it.next());
}
tx.commit();
} catch(Exception e) {
if(tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
}
/** 持久化一个Customer对象*/
public void saveCustomer(Customer customer) throws Exception {
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
try {
session.save(customer);
tx.commit();
} catch(Exception e) {
if(tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
}
/** 按照OID加载一个Customer对象,然后修改它的属性*/
public void loadAndUpdateCustomer(Long customer_id, String address) throws Exception {
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
try {
Customer c = (Customer)session.load(Customer.class, customer_id);
c.setAddress(address);
tx.commit();
} catch(Exception e) {
if(tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
}
/** 删除所有的Customer对象 */
public void deleteAllCustomers() throws Exception {
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
try {
session.delete("From Customer as c");
tx.commit();
} catch(Exception e) {
if(tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
}
/** 选择向控制台还是动态网页输出Customer对象的信息 */
private void printCustomer(ServletContext context, OutputStream out, Customer customer) throws Exception {
if(out instanceof ServletOutputStream)
printCustomer(context, (ServletOutputStream)out, customer);
else
printCustomer((PrintStream)out, customer);
}
/** 把Customer对象的信息输出到控制台,如DOS控制台 */
private void printCustomer(PrintStream out, Customer customer) throws Exception {
}
/** 把Customer对象的信息输出到动态网页 */
private void printCustomer(ServletContext context, ServletOutputStream out, Customer customer) throws Exception {
//把Customer对象的image属性包含的二进制图片数据保存到1231_copy.jpg文件中
byte[] buffer = customer.getImage();
String path = context.getRealPath("/");
FileOutputStream fout = new FileOutputStream(path + "1231_copy.jpg");
fout.write(buffer);
fout.close();
out.println("-------以下是" + customer.getName() + "的个人信息-------" + "<br>");
out.println("ID: " + customer.getId() + "<br>");
out.println("口令: " + customer.getPassword() + "<br>");
out.println("E-MAIL: " + customer.getEmail() + "<br>");
out.println("电话: " + customer.getPhone() + "<br>");
out.println("地址: " + customer.getAddress() + "<br>");
String sex = customer.getSex() == 'M' ? "男" : "女";
out.println("性别: " + sex + "<br>");
String marriedStatus = customer.isMarried() ? "已婚" : "末婚";
out.println("婚姻状况: " + marriedStatus + "<br>");
out.println("生日: " + customer.getBirthday() + "<br>");
out.println("注册时间: " + customer.getRegisteredTime() + "<br>");
out.println("自我介绍: " + customer.getDescription().substring(0, 25) + "<br>");
out.println("<img src = '1231_copy.jpg' border = 0><p>"); //显示1231_copy.jpg图片
}
public void test(ServletContext context, OutputStream out) throws Exception {
Customer customer = new Customer();
customer.setName("Tom");
customer.setEmail("tom@yahoo.com");
customer.setPassword("1234");
customer.setPhone(55556666);
customer.setAddress("Shanghai");
customer.setSex('M');
customer.setDescription("I am very honest.");
//设置Customer对象的image属性,它是字节数组,存放photo.gif文件中的二进制数据
//photo.gif文件和BusinessService.class文件位于同一个目录下
InputStream in = this.getClass().getResourceAsStream("1231.jpg");
byte[] buffer = new byte[in.available()];
in.read(buffer);
customer.setImage(buffer);
//设置Customer对象的birthday属性,它是java.sql.Date类型
customer.setBirthday(Date.valueOf("1980-05-06"));
saveCustomer(customer);
findAllCustomers(context, out);
loadAndUpdateCustomer(customer.getId(), "Shanghai");
findAllCustomers(context, out);
deleteAllCustomers();
}
public static void main(String[] args) throws Exception {
//new BusinessService().test(null, System.out);
//sessionFactory.close();
}
}
[此贴子已经被作者于2007-6-11 12:55:21编辑过]