Search This Blog

Wednesday 14 February 2024

DataBases Stuff

 How to Connect a database in MySQL with a c++ application in Windows

Download Mysql from 

https://dev.mysql.com/downloads/installer/

And the connector library from 

https://dev.mysql.com/downloads/connector/cpp/

Unzip the file 

https://cdn.mysql.com//Downloads/Connector-C++/mysql-connector-c++-8.3.0-winx64.zip


Create a C++ empty project in Visual Studio.

Right-click over the name of the project, ->Add -> New Item -> C++ File. Rename the file as main.cpp

One important thing is to create a database. For that, you can open a command prompt, type the next command:

MySQL -u root -p 

Enter password: the one you used when install MySQL

Then use the command:

create database quickstartdb

Copy and paste the next code:

<stdlib.h> #include <iostream> #include "mysql_connection.h" #include <cppconn/driver.h> #include <cppconn/exception.h> #include <cppconn/prepared_statement.h> using namespace std; //for demonstration only. never save your password in the code! const string server = "localhost"; const string username = "root"; const string password = "yourownpassword"; int main() { sql::Driver *driver; sql::Connection *con; sql::Statement *stmt; sql::PreparedStatement *pstmt; try { driver = get_driver_instance(); con = driver->connect(server, username, password); } catch (sql::SQLException e) { cout << "Could not connect to server. Error message: " << e.what() << endl; system("pause"); exit(1); } //please create database "quickstartdb" ahead of time con->setSchema("quickstartdb"); stmt = con->createStatement(); stmt->execute("DROP TABLE IF EXISTS inventory"); cout << "Finished dropping table (if existed)" << endl; stmt->execute("CREATE TABLE inventory (id serial PRIMARY KEY, name VARCHAR(50), quantity INTEGER);"); cout << "Finished creating table" << endl; delete stmt; pstmt = con->prepareStatement("INSERT INTO inventory(name, quantity) VALUES(?,?)"); pstmt->setString(1, "banana"); pstmt->setInt(2, 150); pstmt->execute(); cout << "One row inserted." << endl; pstmt->setString(1, "orange"); pstmt->setInt(2, 154); pstmt->execute(); cout << "One row inserted." << endl; pstmt->setString(1, "apple"); pstmt->setInt(2, 100); pstmt->execute(); cout << "One row inserted." << endl; delete pstmt; delete con; system("pause"); return 0; }

I leave the video from where I took the code. 

Change debug to release in solution configuration. 

Go to Project-> NameofProject Properties. Change Configuration to Release. Follow the next configuration steps in:

https://www.youtube.com/watch?v=a_W4zt5sR1M&t=2s

And this link for the database creation, And another option for connection processes:

https://www.youtube.com/watch?v=B8fdzMTnOLc


MySQL - Java  Installation process 

I will share links that I used in the process:

This one is the forts one:

https://www.ionos.com/digitalguide/server/tools/xampp-tutorial-create-your-own-local-test-server/

Then the two for the antivirus and UAC disable:

https://www.process.st/how-to/disable-microsoft-defender-antivirus-service/

https://portal.microfocus.com/s/article/KM000020477?language=en_US

You will find some error, I encounter some and these videos helped me:

https://www.youtube.com/watch?v=faxAeJybrBw

https://www.youtube.com/watch?v=o56L2He3YbA

This video shows how to manage a MySQL database from intellij but does not show how to use java

This one gives the connection using Apache Net Beans, Java and MySQL

https://www.youtube.com/watch?v=zerYyyJ9CRs

These series of youtube videos teach JDBC concepts

https://www.youtube.com/playlist?list=PLhs1urmduZ2-yp3zID5rMEmXDETN8xvMo

Here is the code I generated following the last tutorial:

https://github.com/ivonneleonor/JavaSqlConnection



















No comments:

Post a Comment