mysql与tomcat_mysql数据库与tomcat服务器的一些细节问题
java程序使用數據庫的方法
1.使用jndi數據源
核心代碼:
initCtx = new InitialContext()
DataSource ds;
ds = (DataSource) initCtx.lookup("java:comp/env/jdbc/mysql1");
conn = ds.getConnection();
----------配置服務器信息web項目web.xml中配置
MySQL DB Connection Pool
jdbc/mysql1
javax.sql.DataSource
Container
Shareable
----------------
配置全局數據源方法有三種分別是單個項目比如jkj這個web項目配置在這里
方法二,配置在服務器的context.xml中
文件地址:
這種方法最方便,最適用
方法三:配置整個服務器通用的
首先要在context.xml文件中配置鏈接server.xml的信息
然后再server.xml中配置
注意配置地點在global jndi resources下面
----------------------------------------
java數據庫操作核心代碼
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection(url, user, password)
connection.prepareStatement(sql);
prepareStatement.execute();//增
prepareStatement.executeupdate();//改,刪
prepareStatement.executequery();//查
---------------------------------------------------------
tomcat運行和服務器相關的可能錯誤控制臺提示什么password(true)錯誤
----〉處理:將mysql文件中的my.ini最后面加上
然后重啟服務中的mysql
2.可能是沒配置這個jar包到服務器的lib中
或者是服務器和編譯軟件eclipse等同時含有不同版本的mysql的jar包
3.上面這個包版本的高低和其他框架的配合
因為有的版本低的有些類是沒有的!!!!!要注意jar包的協調。哭惹
----------------------------------------------------------------------------
spring框架如何利用beans.xml中德配置使用不同的數據源
直接發這個內容好了
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">
org.hibernate.dialect.MySQL5Dialect
true
true
true
classpath:hibernate.cfg.xml
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
PROPAGATION_REQUIRED
---------------如何在java程序中使用上面的datasource的bean
applicationContext.xml文件放在src下,并且這個文件就是beans,xml
上面的文件中有兩種數據源,一種是jdbc,一種是spring的數據源
其中使用jdbc數據遠需要配置jdbc.properties文件,這個文件放在src下
內容如下
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/hibernate
username=root
password=123456
jdbcPoolInitSize=10
核心代碼如下:
ApplicationContext xmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
DataSource dataSource = (DataSource) xmlApplicationContext.getBean("testDataSource");
---------------------
當然還有很多其他的方法,比如一般的c3p0的數據源的xml文件配置和調用
c3p0-config.xml如下《放在src下
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/jdbcStudy
root
123456
5
10
5
20
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/jdbcStudy
root
123456
5
10
5
20
d***的數據源和調用
d***config.properties配置如下
另外spring的beans.xml中也可以配置c3p0,d***的數據遠的?bean,可以百度。。。
---------------------------------------------一般來說web項目通常使用jndi數據源
尋常java項目看喜好調用spring的各種bean當數據源可以,也可以自己使用c3p0,d***等其他的jar包來處理數據源,或者使用原始的DriverManager.getConnection()獲取數據庫鏈接也行
-----------------------------------------------
如果配置過程中出現什么問題,可以查jar包是否在服務器的lib中,是否和eclipse中的包沖突,jar包版本對不對,beans.xml中的聲明(頭部位置)是否正確,mysql服務是否打開
總結
以上是生活随笔為你收集整理的mysql与tomcat_mysql数据库与tomcat服务器的一些细节问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 正则表达式 table_Jav
- 下一篇: C/C++队列与循环队列