spring中配置MySql数据源,怎样配置数据库信息

问题描述

我安装的Mysql5.1,进入命令模式时需要密码:自己设置的(mysql)有一个默认的数据库test,我想连接这个库,或者其他库怎么连接呢?spring中的配置如下:<bean id ="dataSource" class ="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/test" /> <property name="username" value="" /> //关键是用户名和密码不知道怎么写 <property name="password" value=""/> //关键是用户名和密码不知道怎么写</bean> 严重: Cannot create PoolableConnectionFactory (Access denied for user 'root'@'localhost' (using password: YES))2010-10-25 14:08:20 org.hibernate.cfg.SettingsFactory buildSettings警告: Could not obtain connection metadataorg.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Access denied for user 'root'@'localhost' (using password: YES))at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1225)at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:880)at org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider.getConnection(LocalDataSourceConnectionProvider.java:81)at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:84)at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2009)at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1292)at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:805)at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:745)at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:134)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1202)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1172)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:428)at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:251)at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:156)at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:248)at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:160)at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:284)at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:352)at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:244)at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:187)at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:49)at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3972)at org.apache.catalina.core.StandardContext.start(StandardContext.java:4467)at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:546)at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1041)at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:964)at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:502)at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1277)at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:321)at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)at org.apache.catalina.core.StandardHost.start(StandardHost.java:785)at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)at org.apache.catalina.core.StandardService.start(StandardService.java:519)at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)at org.apache.catalina.startup.Catalina.start(Catalina.java:581)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)at java.lang.reflect.Method.invoke(Method.java:585)at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)Caused by: java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3515)at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3447)at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:911)at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3953)at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1276)at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2048)at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:723)at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:298)at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:282)at org.apache.commons.dbcp.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:38)at org.apache.commons.dbcp.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:294)at org.apache.commons.dbcp.BasicDataSource.validateConnectionFactory(BasicDataSource.java:1247)at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1221)... 44 more2010-10-25 14:08:20 org.hibernate.dialect.Dialect <init>信息: Using dialect: org.hibernate.dialect.MySQLDialect 问题补充:安装完MySql然后进行配置时设置密码,如图附加current root password:rootnew rootpassword: testconfirm password: test然后我再配置我的spring配置文件:spring中的配置如下: <bean id ="dataSource" class ="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/test" /> <property name="username" value="test" /> <property name="password" value="test"/> </bean> 还是报错:警告: Could not obtain connection metadataorg.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Access denied for user 'test'@'localhost' (using password: YES))at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1225)at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:880)at org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider.getConnection(LocalDataSourceConnectionProvider.java:81)at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:84)at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2009)at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1292)at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:805)at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:745)at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:134)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1202)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1172)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:428)at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:251)at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:156)at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:248)at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:160)at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:284)at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:352)at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:244)at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:187)at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:49)at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3972)at org.apache.catalina.core.StandardContext.start(StandardContext.java:4467)at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:546)at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1041)at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:964)at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:502)at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1277)at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:321)at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)at org.apache.catalina.core.StandardHost.start(StandardHost.java:785)at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)at org.apache.catalina.core.StandardService.start(StandardService.java:519)at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)at org.apache.catalina.startup.Catalina.start(Catalina.java:581)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)at java.lang.reflect.Method.invoke(Method.java:585)at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)Caused by: java.sql.SQLException: Access denied for user 'test'@'localhost' (using password: YES)at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3515)at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3447)at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:911)at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3953)at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1276)at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2048)at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:723)at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:298)at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:282)at org.apache.commons.dbcp.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:38)at org.apache.commons.dbcp.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:294)at org.apache.commons.dbcp.BasicDataSource.validateConnectionFactory(BasicDataSource.java:1247)at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1221)... 44 more2010-10-26 14:36:17 org.hibernate.dialect.Dialect <init>信息: Using dialect: org.hibernate.dialect.MySQLDialect

解决方案

可能还是你的用户和密码的问题吧。在试一试我的办法。先关闭mysql # service mysqld stop把权限屏蔽 # mysqld_safe --skip-grant-table 会出现这样的字样: Starting demo from .....新开起一个终端输入 # mysql -u root mysql mysql> UPDATE user SET Password=PASSWORD('newpassword') where USER='root'; mysql> FLUSH PRIVILEGES;//记得要这句话,否则如果关闭先前的终端,又会出现原来的错误 mysql> q这样看看。
解决方案二:
引用Caused by: java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES) 没分配权限 或者数据库 用户名密码错误!用户名密码 你安装的时候会填的什么就写什么! 默认 root
解决方案三:
用户名用root.你在安装mysql数据库的时候, 会让你设置密码,把那个你设置的输入进去。上面是我的mysql数据库,我设置的密码是root.
解决方案四:
<bean id ="dataSource" class ="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/test" /> <property name="username" value="root" /> <property name="password" value="root"/> </bean> 要连其他库,把test改成库名就可以了~
解决方案五:
<bean id ="dataSource" class ="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://192.168.10.60:3309/test" /> <property name="username" value="root" /> <property name="password" value="root"/> </bean> 用户名和密码,就是你连接数据库的用户名和密码。

时间: 2025-01-13 02:49:59

spring中配置MySql数据源,怎样配置数据库信息的相关文章

jboss4。0下mysql数据源的配置

花了一个小时的时间,搞了一下jboss4.0下mysql数据源的配置.下面是一些具体过程1.首先安装mysql数据库并将其驱动程序考到jboss的serverdefaultlib下面2.将mysql-ds.xml文件放置到serverdefaultdeploy下面 内容如下: <datasources> <local-tx-datasource> <jndi-name>MySqlDS</jndi-name> <connection-url>jdb

JBoss AS7配置MySQL数据源

AS7默认的数据源是H2,虽然H2方便,但是一般的生产环境都是MySQL.ORACLE.SQLServer居多,本文描 述如何在AS7配置MySQL数据源. 根据Jboss社区的文档,配置一个新的数据源大致分为两步.描述如 下: 加入JDBC驱动文件 在JBOSS_HOME/modules目录下加入如下目录结构及文件: JBOSS_HOME +- modules +- com +- mysql +- main +- module.xml +- mysql-connector-java-5.1.1

spring jar包冲突-为什么我在spring中只要添加一段配置就会报jar包冲突的错误?

问题描述 为什么我在spring中只要添加一段配置就会报jar包冲突的错误? 在application.xml中只要添加<!-- class=""org.springframework.jms.listener.DefaultMessageListenerContainer""> --> <!-- 事物管理 --> <!-- --> <!-- --> 这段配置就会报 java.lang.NoSuchMethodE

怎样在tomcat5中配置mysql数据源

一.配置环境变量: softe version:tomcat5.0.12/mysql4.1.7/mysql_driver---mysql-connector-java-3.1.4-beta-bin.jar mysql驱动程序可从www.mysql.com官方网站下载 路径: tomcat5在d:\myweb\tomcat5: mysql在C:\Program Files\MySQL\MySQL Server 4.1 Path(在原来的基础上加上): d:\myweb\tomcat5\bin;d:

(4) Spring中定时任务Quartz集群配置学习

原 来配置的Quartz是通过spring配置文件生效的,发现在非集群式的服务器上运行良好,但是将工程部署到水平集群服务器上去后改定时功能不能正常运 行,没有任何错误日志,于是从jar包.JDK版本.cronExpression到服务器类型,甚至查到了服务器操作系统的类型,都没能找到解决的办 法,后来才知道是集群惹的祸! 详细步骤如下: 1. 按照Quartz集群工作原理 图:表示了每个节点直接与数据库通信,若离开数据库将对其他节点一无所知 在数据库中建表.建表模版在Quartz包下docs/d

Spring中bean的基本xml配置

xml   在spring容器内拼凑bean叫作装配.装配bean的时候,你是在告诉容器,需要哪些bean,以及容器如何使用依赖注入将它们配合在一起.    理论上,bean装配可以从任何资源获得,包括属性文件,关系数据库等,但xml是最常见的spring 应用系统配置源.Spring中的几种容器都支持使用xml装配bean,包括:    XmlBeanFactory ,    ClassPathXmlApplicationContext ,    FileSystemXmlApplicatio

spring中关于“transactionAttributes”的相关配置

<bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" lazy-init="true" abstract="true"> <property name="transactionManager"> <ref be

spring中设置动态数据源

Spring 可以设置动态数据源,这样可以对程序来透明的支持切 换操作不同的数据库. http://oiote.blog.sohu.com/74596942.html 这篇文章写得非 常不错.测试通过,并准备在项目中使用这个方法.还有几个问题 希望大家给点意见: 首先说一下我的需求:数据库结构都是一样,但具体有多少个 数据库不确定(视具体用户而定),用户操作那个数据库要根据登 录的时候进行选择,或根据用户的权限而定.这个信息肯定是保存 在用户的登录信息中(例如:session) 问题1.当有不同

mysql不预读数据库信息(use dbname 更快,不会卡)—Reading table information for completion of table and column names

原文:http://blog.163.com/xychenbaihu@yeah/blog/static/13222965520113152619796/ mysql> use dbname Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A 让后就卡在这里.     上面卡住的原因::