Spring连接数据库的几种常用的方式
先在本地mysql创建数据库luo,再建立表user。
1,建立maven项目HelloSpring
2,pom.xml添加需要用到的Jar包
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.aluo</groupId> <artifactId>HelloSpring</artifactId> <version>0.0.1-SNAPSHOT</version> <build /> <dependencies> <!-- Spring Core --> <!-- http://mvnrepository.com/artifact/org.springframework/spring-core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.1.4.RELEASE</version> </dependency> <!-- Spring Context --> <!-- http://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.1.4.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.15</version> </dependency> <!-- https://mvnrepository.com/artifact/c3p0/c3p0 --> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-dbcp/commons-dbcp --> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.2.2</version> </dependency> </dependencies> </project>
其中导入mysql-connector-java总是有问题,后面是先下载mysql-connector-java-5.1.15.jar到本地仓库。
3,beans2.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 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.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--first spring自带的DriverManagerDataSource --> <!-- <bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/luo"></property> <property name="username" value="root"></property> <property name="password" value="123456"></property> </bean> --> <!--second c3p0--> <!-- <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver" /> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/luo" /> <property name="user" value="root" /> <property name="password" value="123456" /> </bean> --> <!--third apache.commons.dbcp--> <bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/luo" /> <property name="username" value="root" /> <property name="password" value="123456" /> </bean> </beans>
4,创建类MainJdbcTest
package com.aluo.spring; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.sql.DataSource; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainJdbcTest { public static void main(String[] args) throws SQLException { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans2.xml"); DataSource dataSource = (DataSource) ctx.getBean("dataSource"); String sql = "select * from user"; Connection connection = dataSource.getConnection(); System.out.println(connection); Statement stm = connection.createStatement(); ResultSet rs = stm.executeQuery(sql); while (rs.next()) { System.out.println("手机号码为:"); System.out.println(rs.getString(2)); } } }
运行output:
jdbc:mysql://localhost:3306/luo, UserName=root@localhost, MySQL-AB JDBC Driver
手机号码为:
123456
手机号码为:
111
手机号码为:
112
参考:https://www.cnblogs.com/xiohao/p/3663619.html
https://www.jb51.net/article/126694.htm