SpringCloud连接多数据库方式(后续继续更新)

  • Post author:
  • Post category:其他


明确需求:


需要用Java连接多个数据库源进行CRUD


话不多说,直接开干!

第一步:

建立工具类:

public class JDBCUtil {

public static int sqlService(String name) throws Exception {
        String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
        String dbURL = "jdbc:sqlserver:///*此处填写你的数据库IP地址*/:/*此处填写数据库的端口号*/;DatabaseName=/*此处填写对应的数据库名*/";
        String userName = "/*此处填写数据库的用户名*/";
        String userPwd = "/*此处填写数据库的密码*/";
        try {
            Class.forName(driverName);
            dbConn = DriverManager.getConnection(dbURL, userName, userPwd);//此处进行连接数据库!
            System.out.println("数据库连接成功");
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            System.out.println("连接失败");
        }

        Statement sta = null;

        String altDateBase1 = name;

        try {
            sta = dbConn.createStatement();
            int count = sta.executeUpdate(altDateBase1);//此处执行传入的sql语句
            if (count>0){
                return 1;
            }else {
                return 0;
            }
        } catch (SQLException e) {
            e.printStackTrace();
           throw new Exception("SQL执行失败!");
        }
    }
}

第二步:

String sql = "exec sp_TB_SCTLRK";
int respSQL = JDBCUtil.sqlService(sql);
if (respSQL==0){
    throw new Exception("调用ERP失败!");
 }

即可实现多数据库配置!


Having the code,Having the world!


后续继续更新通过配置类实现多数据库配置!



版权声明:本文为weixin_46816552原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。