nested exception is org.hibernate.exception.GenericJDBCException: Cannot open connection

nested exception is org.hibernate.exception.GenericJDBCException: 

Cannot open connection


网页上大致错误信息:





首先说明一下,在本地上是可以的:


但是在将这个WAR包放到公司服务器上就不行了,老是报这个错误:
java.net.UnknownHostException: www.hongshanwang.com

这是为什么呢?
很明显是数据库连接语言出错,因为在本地可以,在外网就不行了,这里要说一下,局域网访问数据库和外网访问还是有很多区别的。

出错的时,数据库连接语言如下:
jdbc.url= jdbc:mysql://www.hongshanwang.com:3306/tdjdb?zeroDateTimeBehavior=convertToNull&characterEncoding=UTF-8&characterSetResults=UTF-8
jdbc.username=tdj*****
jdbc.password=xwh********

解决这个问题是方法是:
jdbc.url= jdbc:mysql://220.231.193.7:3306/tdjdb?zeroDateTimeBehavior=convertToNull&characterEncoding=UTF-8&characterSetResults=UTF-8
jdbc.username=tdj*****
jdbc.password=xwh********

其实,也就是把域名www.hongshanwang.com,ping成IP地址,220.231.193.7,而已。这样这个问题就解决了。KOS。






hibernate执行保存对象时出现异常: org.hibernate.exception.GenericJDBCException: could not insert: [com.hibernate.domain.User] Caused by: java.sql.SQLException: Field 'user_id' doesn't have a default value 具体的代码和配置如下: **hibernate.cfg.xml:** <hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/hibernate3</property> <property name="connection.username">root</property> <property name="connection.password">ytkj</property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- Enable Hibernate's automatic session context management --> <!-- <property name="current_session_context_class">thread</property> --> <!-- Disable the second-level cache --> <!-- <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> --> <!-- Echo all executed SQL to stdout --> <!-- show sql dml --> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <!-- <property name="hbm2ddl.auto">update</property> --> <mapping resource="com/hibernate/domain/User.hbm.xml"/> </session-factory> </hibernate-configuration> ------------------------------------------------------------------------ 建表语句(成功): CREATE TABLE `t_user` ( `user_id` int(11) NOT NULL, `user_name` varchar(20) default NULL, `user_age` int(11) default NULL, PRIMARY KEY (`user_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ------------------------------------------------------------------ **测试类:** package com.hibernate.manager; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import com.hibernate.domain.User; import com.hibernate.util.HibernateUtil; public class UserManager { public static void main(String[] args) { SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); Session session = sessionFactory.openSession(); Transaction ts = session.beginTransaction(); User user = new User(); user.setId(1); user.setName("zhangsan"); user.setAge(21); session.save(user); ts.commit(); session.close(); sessionFactory.close(); } } ----------------------------------------------------------------------- **User.hbm.xml:** <hibernate-mapping package="com.hibernate.domain"> <class name="User" table="t_user"> <id name="id" column="user_id"> <generator class="native"/> </id> <!-- <id name="id" type="int" column="user_id"/> --> <property name="name" type="string" column="user_name"/> <property name="age" type="int" column="user_age"/> </class> </hibernate-mapping> ------------------------------------------------------------------------- 以上代码执行过后,就包上面的异常。 如果不使用id生成策略(及时没有generator标签),则可以insert成功。 请问这问题是出在哪里?高手指点。
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页