-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: why always come up with the problem Cannot open connection
PostPosted: Sat Jan 24, 2009 9:43 am 
Newbie

Joined: Sat Jan 24, 2009 7:26 am
Posts: 4
Hibernate version 3.3.1.GA

hibernate.cfg.xml like following:

<?xml version='1.0' encoding='gb2312'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.dirver_class">com.mysql.jdbc.Driver
</property>

<property name="hibernate.connection.url">
jdbc:mysql://localhost/joblog
</property>

<property name="hibernate.connection.username">root
</property>
<property name="hibernate.connection.password">root123</property>
<property name="hibernate.connection.pool.size">20</property>

<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.use_sql_comments">true</property>


<property name="jdbc.fetch_size">50</property>
<property name="jdbc.batch_size">20</property>
<property name="jdbc.use_scrollable_resultset">false</property>

<property name="connection.useUnicode">true</property>
<property name="connection.characterEncoding">gb2312</property>

<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>

<mapping resource="com/hp/test/UserInfo.hbm.xml" />
</session-factory>
</hibernate-configuration>

UserInfo.hbm.xml like following:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.hp.test.UserInfo" table="login">
<id name="id" type="integer" >
<column name="id" />
<generator class="identity" />
</id>
<property name="userName" type="string">
<column name="name" length="100"/>
</property>
<property name="password" type="string">
<column name="password" length="100"/>
</property>
</class>
</hibernate-mapping>

My Test Class like following:
package com.hp.test;

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

public class MyFirstHibernate {
public static void main(String[] args) {
SessionFactory sessions = new Configuration().configure()
.buildSessionFactory();
Session session = sessions.openSession();
Transaction tx = null;

tx = session.beginTransaction();
UserInfo user = new UserInfo();
user.setUserName("Zhangzhexin");
user.setPassword("123");

session.save(user);
tx.commit();
}
}

when run the test code, in my console, "org.hibernate.exception.JDBCConnectionException" occured immediately.
The complete Exception message is :

Exception in thread "main" org.hibernate.exception.JDBCConnectionException: Cannot open connection
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:97)
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:142)
at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:85)
at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1353)
at com.hp.test.MyFirstHibernate.main(MyFirstHibernate.java:15)
Caused by: java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getConnection(DriverManager.java:545)
at java.sql.DriverManager.getConnection(DriverManager.java:140)
at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:133)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446)

but if my test code is like this:
package com.hp.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class MyFirstHibernate {
public static void main(String[] args) {

Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost/joblog?user=root&password=root123";
conn = DriverManager.getConnection(url);
PreparedStatement pst = null;
pst = conn.prepareStatement("insert into login (name, password) values (?,?)");
pst.setString(1, "zzx");
pst.setString(2, "zzx");
pst.executeUpdate();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
I can insert the data into DB, I do not know this is why , why when i use hibernate framework , it occured the Exception above. who can help me? thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 24, 2009 11:05 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Maybe it is just a typo in the post... but you have:

Code:
<property name="hibernate.connection.dirver_class">


Notice the incorrect spelling of dirver_class. It should be driver_class.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 24, 2009 8:46 pm 
Newbie

Joined: Sat Jan 24, 2009 7:26 am
Posts: 4
nordborg wrote:
Maybe it is just a typo in the post... but you have:

Code:
<property name="hibernate.connection.dirver_class">


Notice the incorrect spelling of dirver_class. It should be driver_class.


very thanks. ^_^


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.