Home »
Code Examples »
Groovy Code Examples
Groovy - How to connect with MySQL database? Code Example
The code for How to connect with MySQL database?
import java.sql.*;
import groovy.sql.Sql
class Example {
static void main(String[] args) {
// Creating a connection to the database named "STUDENT"
def sql = Sql.newInstance('jdbc:mysql://localhost:3306/STUDENT',
'admin', 'admin123', 'com.mysql.jdbc.Driver')
// Executing the queries
// SELECT VERSION - Returns the version of the database
// eachROW() method - Get the result from the database
sql.eachRow('SELECT VERSION()') {
row ->
println row[0]
}
sql.close()
}
}
Code by IncludeHelp,
on August 9, 2022 15:39