Lab 6-1: MySQL Server

Similar documents
Java Database Connectivity (JDBC) 25.1 What is JDBC?

CSC System Development with Java. Database Connection. Department of Statistics and Computer Science. Budditha Hettige

DataBase Lab JAVA-DATABASE CONNECTION. Eng. Haneen El-masry

Testing with MySQL embedded

Tiers (or layers) Separation of concerns

Accessing databases in Java using JDBC

ERwin and JDBC. Mar. 6, 2007 Myoung Ho Kim

PHP: Cookies, Sessions, Databases. CS174. Chris Pollett. Sep 24, 2008.

ECE 650 Systems Programming & Engineering. Spring 2018

Bitnami MariaDB for Huawei Enterprise Cloud

Embedded SQL. Introduction

Embedded SQL. Davood Rafiei

SQL from Applications

Overview. SQL from Applications. Accesing data from an application. Embedded SQL JDBC Stored Procedures. UVic C SC 370, Fall 2002

Bitnami MySQL for Huawei Enterprise Cloud

Java Database Connectivity

PostgreSQL Database and C++ Interface (and Midterm Topics) ECE 650 Systems Programming & Engineering Duke University, Spring 2018

JDBC. Oracle ODBC SP API SP API. SQL server C function calls. SQL server ODBC SP API. Oracle DSN Oracle ODBC Oracle

COP4540 TUTORIAL PROFESSOR: DR SHU-CHING CHEN TA: H S IN-YU HA

Data Analysis and Integration

PRINCIPLES OF OPERATING SYSTEMS

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 14

Linux Network Administration. MySQL COMP1071 Summer 2017

This lecture. Databases - JDBC I. Application Programs. Database Access End Users

SSE 3200 Mysql lab. Introduction. Getting started

JDBC Java Database Connectivity is a Java feature that lets you connect

CS 3305 Intro to Threads. Lecture 6

Getting Started with MySQL

Server-side Web Programming

Accessing a database from Java. Using JDBC

CPSC 421 Database Management Systems. Lecture 10: Embedded SQL

Topic 12: Database Programming using JDBC. Database & DBMS SQL JDBC

Unit 3 - Java Data Base Connectivity

JAVA AND DATABASES. Summer 2018

BUSINESS INTELLIGENCE LABORATORY. Data Access: Relational Data Bases. Business Informatics Degree

APT Session 4: C. Software Development Team Laurence Tratt. 1 / 14

Lab # 9. Java to Database Connection

How To Start Mysql Use Linux Command Line Client In Ubuntu

Introduction: Before start doing any code, there is some terms that one should be familiar with:

Installing MySQL on the Command Line

Array Initialization

Installing LAMP on Ubuntu and (Lucid Lynx, Maverick Meerkat)

CMPT 300. Operating Systems. Brief Intro to UNIX and C

CS 3113 Introduction to Operating Systems Midterm October 11, 2018

CS 3113 Introduction to Operating Systems Midterm October 11, 2018

Hydra Installation Manual

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

Pieter van den Hombergh. March 25, 2018

Databases 2012 Embedded SQL

Parallele Numerik. Blatt 1

CSE 351. Introduction & Course Tools

ECE 435 Network Engineering Lecture 2

Databases and SQL Lab EECS 448

DATABASE DESIGN I - 1DL300

Discuss setting up JDBC connectivity. Demonstrate a JDBC program Discuss and demonstrate methods associated with JDBC connectivity

Contents Upgrading BFInventory iii

DB I. 1 Dr. Ahmed ElShafee, Java course

NETWORK AND SYSTEM PROGRAMMING

ECE 435 Network Engineering Lecture 2

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

Working with Databases and Java

SQream Connector JDBC SQream Technologies Version 2.9.3

ChiliProject - Bug # 529: builder is not part of the bundle. Add it to Gemfile

Hierarchical Data Format query language (HDFql)

Creative assets management. MySQL Install Guide

Kaivos User Guide Getting a database account 2

Environment Settings on SIT

Introduction to Socket Programming

15 practical examples of using commands Mysqladmin to administer a MySQL server Wednesday, 17 March :23

Visit for more.

CS 33. Architecture and the OS. CS33 Intro to Computer Systems XIX 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

You write standard JDBC API application and plug in the appropriate JDBC driver for the database the you want to use. Java applet, app or servlets

Intel Do-It-Yourself Challenge Robotics Hello World

Chapter 11 Introduction to Programming in C

Mysql Tutorial Create Database User Grant All Specification

Check the Desktop development with C++ in the install options. You may want to take 15 minutes to try the Hello World C++ tutorial:

CS342 - Spring 2019 Project #3 Synchronization and Deadlocks

JDBC Architecture. JDBC API: This provides the application-to- JDBC Manager connection.

Creating Your First MySQL Database. Scott Seighman Sales Consultant Oracle

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

Sample slides and handout

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

JENET C LIBRARY API ver. 3.00

Enterprise Java Unit 1- Chapter 6 Prof. Sujata Rizal

MANTHLY TEST SEPTEMBER 2017 QUESTION BANK CLASS: XII SUBJECT: INFORMATICS PRACTICES (065)

are all acceptable. With the right compiler flags, Java/C++ style comments are also acceptable.

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

This tutorial is going to guide you throughout the setup of your. workspace. First, we re going to see how to install the MySQL RDBMS

Aware IM Version 8.1 Installation Guide

13.1 Relational Databases (continued) 13.1 Relational Databases. - Logical model

13.1 Relational Databases

Setup of PostgreSQL, pgadmin and importing data. CS3200 Database design (sp18 s2) Version 2/9/2018

1. PhP Project. Create a new PhP Project as shown below and click next

Using MySQL on the Winthrop Linux Systems

MyLEAD Release V1.3 Installation Guide

MySQL Installation Guide (OS X)

EMC Documentum Foundation Classes

Algolytics AdvancedMiner Installation

Introduction to Java

Problem Set 1: Unix Commands 1

Transcription:

Lab 6-1: MySQL Server 1. Objective The participants of the course will be able to: Install and configure a MySQL server. Define a c-program, which enables to access (write and read) to the database of the MySQL server. Define a java-program, which enables to access the database of the MySQL server through the Internet. 2. Install and configure the MySQL server Ref.: http://zetcode.com/db/mysqlvb 2.1 Installation of the MySQL server If you don't already have MySQL installed, we must install it. $ sudo apt-get install mysql-server This command installs the MySQL server and various other packages. While installing the package, we are prompted to enter a password for the MySQL root account. 2.2 Installation of the MySQL C-API The MySQL C-API is a C-based API that client applications written in C can use to communicate with MySQL Server. The MySQL C-API can be installed as follow: $ sudo apt-get install libmysql++-dev 2.3 Creating a new MySQL database Now we are going to create a new database with the command shell. $ mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 30 Server version: 5.5.52-0+deb8u1 (Raspbian) Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql> SHOW DATABASES; Ver. 1.0 TI Biel, 01/11/2016 Page 1/7

+--------------------+ Database +--------------------+ information_schema mysql +--------------------+ 2 rows in set (0.00 sec) We use the mysql monitor client application to connect to the server. We connect to the database using the root account. We show all available databases with the SHOW DATABASES statement. mysql> CREATE DATABASE testdb; Query OK, 1 row affected (0.02 sec) We create a new testdb database. We will use this database throughout the tutorial. We must grant all the privileges to root, also when he tries to access to the databases from an external computer. Attention: in this example you must type your own mysql root password as ˈroot_passwordˈ. mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root_password' with grant option; Query OK, 0 rows affected (0.00 sec) To quit MySQL, use this command: mysql> quit; Bye 2.4 Configuration of the MySQL server The MySQL server listens on a single network socket for TCP/IP connections. This socket is bound to a single address, but it is possible for an address to map onto multiple network interfaces. The default address is 127.0.0.1. To specify an address explicitly, use the bindaddress=addr option at server startup, where addr is an IPv4 address or a host name. Configuration of the bind-address option If the bind-address must be set to 0.0.0.0, so that the server accepts TCP/IP connections on all server host IPv4 interfaces. This can be realized by setting the value of the bind-address in the configuration file /etc/mysql/my.cnf: bind-address = 0.0.0.0 Make also sure you comment out the following line: # skip-external-locking Ver. 1.0 TI Biel, 01/11/2016 Page 2/7

3. Access to the MySQL database with a C-Program Ref.: http://zetcode.com/db/mysqlc 3.1 Create and fill a MySQL table The next code example will create a table and insert some data into it. #include <my_global.h> #include <mysql.h> /* in order to avoid unnecessary repetition, we create a custom finish with error function */ void finish_with_error(mysql *con) { fprintf(stderr, "%s\n", mysql_error(con)); int main(int argc, char *argv[]) { /* allocates a MYSQL object suitable for the function mysql_real_connect() */ MYSQL *con = mysql_init(null); if (con == NULL) { fprintf(stderr, "%s\n", mysql_error(con)); /* connect to the testdb database: user name is ˈrootˈ, password ˈrootˈ and database ˈtestdbˈ */ if (mysql_real_connect(con, "localhost", "root", "root", "testdb", 0, NULL, 0) == NULL) { if (mysql_query(con, "DROP TABLE IF EXISTS Cars")) { /* create a table named Cars, which has three columns */ if (mysql_query(con, "CREATE TABLE Cars(Id INT, Name TEXT, Price INT)")) { /* insert the first row into the Cars table */ if (mysql_query(con, "INSERT INTO Cars VALUES(1,'Audi',52642)")) { /* insert the second row into the Cars table */ if (mysql_query(con, "INSERT INTO Cars VALUES(2,'Mercedes',57127)")) { Ver. 1.0 TI Biel, 01/11/2016 Page 3/7

/* insert the third row into the Cars table */ if (mysql_query(con, "INSERT INTO Cars VALUES(3,'Skoda',9000)")) { exit(0); We can compile this c-programm as follow: gcc createdb.c -o createdb -std=c99 `mysql_config --cflags --libs` When you have executed this programm, you can test its contributions to the database testdb with the mysql monitor client application: $ mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 30 Server version: 5.5.52-0+deb8u1 (Raspbian) Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql> USE testdb; mysql> SHOW TABLES; +------------------+ Tables_in_testdb +------------------+ Cars +------------------+ 1 row in set (0.00 sec) mysql> SELECT * FROM Cars; +------+------------+--------+ Id Name Price +------+------------+--------+ 1 Audi 52642 2 Mercedes 57127 3 Skoda 9000 +------+------------+--------+ 3 rows in set (0.00 sec) mysql> quit; Bye 3.2 Read from a MySQL table In the next example, we will retrieve data from a table with a C-Program. We need to do the following steps: Ver. 1.0 TI Biel, 01/11/2016 Page 4/7

Create a connection Execute query Get the result set Fetch all available rows Free the result set #include <my_global.h> #include <mysql.h> void finish_with_error(mysql *con) { fprintf(stderr, "%s\n", mysql_error(con)); int main(int argc, char *argv[]) { MYSQL *con = mysql_init(null); if (con == NULL) { fprintf(stderr, "mysql_init() failed\n"); if (mysql_real_connect(con, "localhost", "root", "root", "testdb", 0, NULL, 0) == NULL) { // prints all columns from the Cars table if (mysql_query(con, "SELECT * FROM Cars")) { // retrieve all data from the Cars table MYSQL_RES *result = mysql_store_result(con); if (result == NULL) { // get the number of fields (columns) in the table int num_fields = mysql_num_fields(result); // fetch the rows and print them to the screen MYSQL_ROW row; while ((row = mysql_fetch_row(result))) { for(int i = 0; i < num_fields; i++) { printf("%s ", row[i]? row[i] : "NULL"); printf("\n"); Ver. 1.0 TI Biel, 01/11/2016 Page 5/7

mysql_free_result(result); exit(0); 4. Access to the MySQL database with a Java-Program In the next example, we will retrieve data from a table with a Java-Program. import java.sql.*; public class ReadDataBase { public static void main(string[] args) { try { // create our mysql database connection String mydriver = "com.mysql.jdbc.driver"; Class.forName(myDriver); // IP address of the server String ipaddress = "192.168.1.42"; String dbname = "testdb"; String dbusername = "root"; String dbpassword = "root"; String connectionstring = "jdbc:mysql://" + ipaddress + "/" + dbname + "?user=" + dbusername + "&password=" + dbpassword + "&useunicode=true&characterencoding=utf-8"; Connection con = null; con = DriverManager.getConnection(connectionString); // our SQL SELECT query String query = "SELECT * FROM Cars"; Statement st = con.createstatement(); ResultSet rs = st.executequery(query); System.out.println("iterate through the java resultset"); while (rs.next()) { int id = rs.getint("id"); String name = rs.getstring("name"); int price = rs.getint("price"); // print the results System.out.format("%d, %s, %d\n", id, name, price); st.close(); Ver. 1.0 TI Biel, 01/11/2016 Page 6/7

catch (Exception e) { System.err.println("Got an exception! "); System.err.println(e.getMessage()); Before you compile this Java program, you must download the following jar file into your working directory: mysql-connector-java-5.1.22-bin.jar Then you can compile and start your Java program within a Linux console as follow: javac -classpath mysql-connector-java-5.1.22-bin.jar ReadDataBase.java java -classpath mysql-connector-java-5.1.22-bin.jar:. ReadDataBase Ver. 1.0 TI Biel, 01/11/2016 Page 7/7