scala链接mysql

方法一:链接并操作mysql

//链接Mysqlval driver = "com.mysql.jdbc.Driver"val url = "jdbc:mysql://10.1.2.190:8066/mq_sale_disc"val username = "kr.user"val password = "[email protected]"var connectionMqcrm: Connection = nullClass.forName(driver)connectionMqcrm = DriverManager.getConnection(url, username, password)val statement = connectionMqcrm.createStatement()
//查询、插入var count = 0val sqlname = "insert into tmp_enterprise( ENTERPRISE_NAME ) values (\‘" + coname + "\‘) "val sqlcount = "select count(*) from  tmp_enterprise  where  ENTERPRISE_NAME  =\‘" + coname + "\‘ "var ecusqlcount = statement.executeQuery(sqlcount)while (ecusqlcount.next()){    count = ecusqlcount.getInt(1)}if (count >0) {    val ecusqlname = statement.executeUpdate(sqlname)}//插入征信分数var countnum =0var companyID = 0val sqlID = "select  ENTERPRISE_ID  from  tmp_enterprise  where  ENTERPRISE_NAME= \‘" + coname +"\‘ "val ecusqlID = statement.executeQuery(sqlID)while (ecusqlID.next()){    companyID = ecusqlID.getInt("ENTERPRISE_ID")}val timenew = System.currentTimeMillis()var dateFormat: SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd")var nowtime: Date = new Date(timenew)val currentTimenew = dateFormat.format(nowtime)val sqlscore = "insert  into tm_enterprise_credit(CREATE_TIME ,UPDATE_TIME , ENTERPRISE_ID, CREDIT_SCORE ) values( \‘" + currentTimenew + "\‘ , \‘" + currentTimenew + "\‘,\‘" + companyID + "\‘,\‘" + score + "\‘)"val sqlcountN = "select count(*) from  tm_enterprise_credit  where  ENTERPRISE_ID  = \‘" + companyID + "\‘"val ecusqlcountN = statement.executeQuery(sqlcountN)while (ecusqlcountN.next()){    countnum = ecusqlcountN.getInt(1)}if (countnum > 0 ) {    val sqlnew = "update tm_enterprise_credit set CREDIT_SCORE= \‘" + score + " \‘ , UPDATE_TIME= \‘" + currentTimenew + "\‘" + "where ENTERPRISE_ID= \‘" + companyID + "\‘"    val ecusqlnew = statement.executeUpdate(sqlnew)} else {    val ecusqlscore = statement.executeUpdate(sqlscore)}

方法二:查询

val jdbcDF = sqlContext.read.format("jdbc").options(Map("url" -> "jdbc:mysql://10.1.2.190:8066/mq_crm?user=kr.user&[email protected]",
"dbtable" -> "crm_order")).load()

val tableDF = sqlContext.jdbc("jdbc:mysql://mysql_hostname:mysql_port/testDF?user=your_username&password=your_password", "user")

//查询mySql数据库
val tableDF = sqlContext.jdbc("jdbc:mysql://10.1.2.190:8066/mq_sale_disc?user=kr.user&[email protected]", "tmp_enterprise")

注意:execute、executeUpdate、executeQuery三者区别

查询用executeQuery

插入、更新、删除用executeUpdate

				
时间: 2024-10-12 21:45:00

scala链接mysql的相关文章

JDBC链接Mysql数据库+执行sql

package com.wangbingan.utils; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; /**  * JDBC链接数据库  *   * @author Administrator  *   */ public class JdbcUtil { public static void main(Strin

EntityFramework 多数据库链接,MySql,SqlServer,Oracel等

环境:EntityFramework5.0,MySql5.6,MSSQL2012 EF是强大的ORM工具,真正意义上的多数据库链接指的是不同类型的数据库,以及同种类型的数据库多个库,EF很好的支持这一点,下面简单演示下: 创建一个MVC4.0,Framework4.5的基本项目,然后重点是WebConfig配置: <?xml version="1.0" encoding="utf-8"?> <!-- For more information on

php链接Mysql数据库

PHP链接Mysql数据库: 1:首先打开软件(WampServer再点击鼠标左键,再点phpMYAdmin最后打开的页面是 再填用户名,密码可以写不可写,最后再点执行就出现这个页面 再点test新建表名)最后就在mysql里面创建表格,在表格里面可以添加数据.查询数据.删除数据.修改数据 例: (1):添加数据INSERT INTO `message`(`id`, `user`, `title`, `content`, ` lda te`) VALUES ('','张三','学习php','飞

PHP链接Mysql数据库的小知识

1:在PHP写隔行变色有三种写法(但是最好的是用js+css) (1)CSS: tr:nth-child(even){             background: #cad9ea;         } even:代表偶数: ood:代表奇数:   (2)PHP: <?php                 if($n %2 == 0){            ?>            <tr style="background: #cad9ea"> <

Qt5.8 下链接 Mysql 错误以及解决方法

Qt 5.8 下链接 Mysql(Windows 平台下),有朋友会出现一个这个无法连接的错误 QSqlDatabase: QMYSQL driver not loaded QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QODBC QODBC3 QPSQL QPSQL7 解决办法如下 根据 Qt版本不同 ,32 和 64 的 ,需要的驱动是不同的,总的来说,无论 Mysql 是什么版本的,64 位 Qt 要用 64 位的 Mysq

C#链接MySQL

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using MySql.Data.MySqlClient; namesp

python链接mysql

1.安装MySQLdb MySQLdb 是用于Python链接Mysql数据库的接口,它实现了 Python 数据库 API 规范 V2.0,基于 MySQL C API 上建立的. 下载地址: http://sourceforge.net/projects/mysql-python/files/mysql-python/ 我下载了1.2.3版本 2.代码 1 #!/usr/bin/python 2 # -*- coding: UTF-8 -*- 3 import MySQLdb 4 # 打开数

2.Hive链接Mysql

安装MySQL参见 http://zhangyanfeng.blog.51cto.com/3884312/1733463 在hive中的conf文件中创建hive-site.xml并写入hive链接MySQL配置信息 <configuration> <property> <name>javax.jdo.option.ConnectionURL</name> <value>jdbc:mysql://192.168.0.207:3306/hive?c

visual studio 2012 链接Mysql 5.1

首先在nuGet 下载MySql.Data.Entity 安装 mysql for visual studio http://www.mysql.com/why-mysql/windows/visualstudio/ 安装 mysql connector 6.9.3 http://dev.mysql.com/downloads/connector/net/6.9.html 安装 Entity Framework 6 Tools for Visual Studio 2012 http://www.