But postgres123 is the incorrect password of user Postgres.
We need to define the correct password of the user into the connection string, the wrong password will issue the error i.e. Database user password – This is the password of the database user which was we have used in the connection string.It will issue an error that permission is denied. Without connecting permission, it will not connect to the database server. The user which we were using has permission to connect to the database server. This is the database user which we were using to connect the database server. Database username – This parameter is also important while connecting to the database server using the connection string.
This will contain the driver information of the database server.
We need to define different database drivers for different databases. Database driver – This is an important parameter while using a connection string to connect the database server.It will also contain the information of the database port and database name. This parameter contains the information of database hostname database hostname is nothing but the IP of the database server. We need to use different URL for the different types of databases. Database URL – This is nothing but the database URL which was used to connect the java application to the database server.Parameter description syntax of JDBC connection string. The JDBC connection string for DB2 database –ĭatabase_url = Jdbc:as400://host_name/database_name ĭatabase_driver = .AS400JDBCDRIVER.The JDBC connection string for SQL server database –ĭatabase_url = Jdbc:microsoft:sqlserver//host_name:port_no/database_name = name_of_databaseĭatabase_driver = .SQLServerDriver.JDBC connection string for PostgreSQL database –ĭatabase_url = Jdbc:postgresql://host_name/database_name.The JDBC connection string for MySQL database –ĭatabase_url = Jdbc:mysql://host_name/database_nameĭatabase_user_password = password_of_user.As you've seen, you can connect to MySQL or any other database ( Postgresql, SQL Server, Oracle, etc.) using the usual Java JDBC technology from your Scala applications.Hadoop, Data Science, Statistics & othersīelow is the syntax of the connection strings is as follows. If you were looking for a simple Scala JDBC connection example, I hope this short article was helpful. Val url = "jdbc:mysql://localhost/drupal7"
Val url = "jdbc:mysql://hostname/database_name"Īnd if I was instead connecting to a database named drupal7, the MySQL URL would look like this instead: If you're new to JDBC and the MySQL URL shown above looks weird because I'm accessing the "mysql" database in the MySQL database server, remember that the general MySQL connection URL looks like this: Println("host, user = " + host + ", " + user)Īs you can see, this Scala JDBC database connection example looks just like Java JDBC, which you can verify from my very old JDBC connection example and JDBC SQL SELECT example. Val resultSet = statement.executeQuery("SELECT host, user FROM user") Val statement = connection.createStatement() create the statement, and run the select query
there's probably a better way to do thisĬonnection = DriverManager.getConnection(url, username, password) connect to the database named "mysql" on the localhost * A Scala JDBC connection example by Alvin Alexander, In this example I'm connecting to a MySQL database server on my local computer, and then running a SQL SELECT query against the user table of the mysql database:
Without any further introduction, here's the source code for a complete Scala class (an object, actually) that connects to a MySQL database using nothing but plain old JDBC. I just ran a simple JDBC connection and SQL SELECT test, and everything seems to work just as it does in Java. If you want to use a SQL database with your Scala applications, it's good to know you can still use the traditional Java JDBC programming library to access databases. Scala JDBC FAQ: How can I use the Java JDBC API in my Scala application?