1. 程式人生 > >spring連線MySQL完整例項(附jar包下載)

spring連線MySQL完整例項(附jar包下載)

本文講的是使用spring自帶的DriverManagerDataSource 來實現資料庫的連線。

建立測試主類MySpringTest

package Myspring;
 
import java.sql.*;
 
import javax.sql.DataSource;
 
import org.springframework.context.ApplicationContext;
 
import org.springframework.context.support.ClassPathXmlApplicationContext;

 /**
 * @author zhenqingshi
 *
 * 2018年7月6日
 */

public class MySpringTest {
 
    @SuppressWarnings("resource")//該批註的作用是給編譯器一條指令,告訴它對被批註的程式碼元素(ctx)內部的某些警告保持靜默。詳見另一篇博文:
地址
         public static void main(String args[]) throws Exception{     ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml"); //載入spring配置檔案     DataSource dataSource=ctx.getBean("dataSource",DataSource.class);//載入資料庫驅動類     String sql="select * from Student"; //查詢表為Student,已在資料庫建立     Connection connection=dataSource.getConnection();     Statement stm=connection.createStatement();     ResultSet rs=stm.executeQuery(sql);     while(rs.next())         {             System.out.println("使用者名稱為:");         System.out.println(rs.getString(2));     }     } }
applicationContext.xml程式碼為:
<?xml version="1.0" encoding="UTF-8"?> 
 
<beans xmlns="http://www.springframework.org/schema/beans"
 
xmlns:aop="http://www.springframework.org/schema/aop"
 
xmlns:tx="http://www.springframework.org/schema/tx"
 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 
xmlns:context="http://www.springframework.org/schema/context" 
 
xmlns:p="http://www.springframework.org/schema/p"
 
xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/tx      
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
 
 <!-- 使用XML Schema的p名稱空間配置 -->
 
  <bean name="dataSource"  class="org.springframework.jdbc.datasource.DriverManagerDataSource" 
 
  p:driverClassName="com.mysql.jdbc.Driver" 
 
  p:url="jdbc:mysql://localhost:3306/test"
 
  p:username="root"
 
  p:password="root"  /> 
 
   
 
</beans>

目錄結構為:

            

測試示意圖:

                

遇到的主要錯誤:

    Exception in thread "main" java.lang.UnsupportedClassVersionError: com/mysql/jdbc/Driver : Unsupported major.minor version 52.0?

Exception in thread "main" java.lang.UnsupportedClassVersionError: com/mysql/jdbc/Driver : Unsupported major.minor version 52.0

原因: 主要是jdk版本和MySQL驅動包相容錯誤

  1、jdk7+老版5.0驅動com/mysql/jdbc/Driver  (這裡使用的是jdk7,所以重新下了個MySQL驅動包5.1.10的,替換後就行了)
  2、jdk8+新版6.0驅動com/mysql/cj/jdbc/Driver

附:spring4.2.3.jar,mysql-connector-java-5.1.10.jar和org.springframework.jdbc-3.2.2.RELEASE.jar下載地址:

    連結:https://pan.baidu.com/s/1RNZQsg5Hf4LRSjTFshWHvA 密碼:5wzd

mysql-connector-java-6.0.2.jar下載地址:

    連結:https://pan.baidu.com/s/1U71Kf7SslFeePL4pQh2sPA 密碼:vzhw

最後附上spring連線資料庫的其他幾種方式:

    https://www.cnblogs.com/xiohao/p/3663619.html