Difference between revisions of "MySQL"
(10 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
− | ==MySQL on mi-linux== | + | == MySQL on mi-linux == |
− | MySQL is a very popular, open source, freely available, relational database management system available from [http://www.mysql.com MySQL.com] | + | '''MySQL''' is a very popular, open source, freely available, relational database management system available from [http://www.mysql.com MySQL.com] |
It has been installed on mi-linux and forms the backbone of most web applications/resources available to students, including this wiki! | It has been installed on mi-linux and forms the backbone of most web applications/resources available to students, including this wiki! | ||
− | Students will most likely make advantage of the database server in conjunction with web applications (created in [[PHP]], [[Java]], etc.) to form the data storage/retrieval component of any | + | Students '''will''' most likely make advantage of the database server in conjunction with web applications (created in [[PHP]], [[Java]], etc.) to form the data storage/retrieval component of any applicatio |
− | == | + | == HOW TO REGISTER == |
− | + | If you wish to create your own database, follow these steps | |
+ | |||
+ | 1. Visit https://mi-linux.wlv.ac.uk/facilities | ||
+ | |||
+ | 2. Login with your university account and password | ||
+ | |||
+ | 3. Click "Register for a MySQL Account" | ||
+ | |||
+ | 4. Read and agree to the terms and conditions. | ||
+ | |||
+ | 4. The next screen will display your connection details, including a RANDOMLY GENERATED PASSWORD and details on how to connect to the admin interface. '''Please make a note of these details''' | ||
+ | |||
+ | 5. Once registered, you can administer your database using the [[PhpMyAdmin|phpMyAdmin]] web interface. | ||
− | == | + | == How to Connect to MySQL from a Java Application == |
− | If you | + | If you are looking to connect to your MySQL database from your own Java program there are several steps you need to take. If you have seen a Java-to-database connection demonstrated before you will know that firstly you require the Connection String. In the Data Model the Connection String should be provided as a global variable and initialise the Connection String within the Data Model's constructor such as this: |
+ | <pre>public class DataModel{ private String connectionString; public DataModel() { /* * Where 0777777 is your student number * and where drowssap is your mysql password */ connectionString = "jdbc:mysql://mi-linux.wlv.ac.uk/db0712329?user=0777777&password=drowssap"; } ...etc...</pre> | ||
+ | This however will still not work as you need to import a mysql connector into your project, to do this follow these instructions: | ||
− | + | The SQL Connector needs to be downloading from this link: [http://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-5.1.7.tar.gz/from/pick#mirrors] There's a GB mirror halfway down the list. | |
− | + | Once that zip file has been downloaded and extracted there can be found a jar file inside called something like "mysql-connector-java-5.1.7-bin.jar". This file needs to be imported into the build path of the Project in question. | |
+ | <pre>Rigtht Click Project > Build Path > Configure Built Path > Libraries tab > Add External Jar > Ok</pre> | ||
+ | With the Connector imported and with your connectionString in place all that is required now is to produce your Data Model's methods that use the connection connection. | ||
− | + | Here is a snippet of code displaying such use; this method assumes the role of querying a table which contains details of Modules containing information such as Module Code, Module Title and Module Leader. | |
+ | <pre>public ArrayList<Module> getModules() { ArrayList<Module> ml = new ArrayList<Module>(); try { // use connection string for myaql mi-linux connection. Connection conn = DriverManager.getConnection(connectionString); Statement stmt = conn.createStatement(); /* You will then use stmt to execute your query * and place the results of that query into a resultSet. */ ...etc..</pre> | ||
+ | It is important to place your connection in a try/catch block to catch any errors with the query or connection itself, it is much more efficient than setting up your own query checks. | ||
− | ' | + | More information about Connecting to a database via a java based application can be found in Matthew Burley's CP2120 Software Design Lecture Notes on Relational Database Management System Persistence Data Models - pardon the mouth full. |
− | + | == How to connect PHP to an Oracle database == | |
− | + | Please see example [http://scitua.wlv.ac.uk/~cm1958/webdatabases/php/oracleExample.html here]. |
Latest revision as of 09:58, 22 September 2016
MySQL on mi-linux
MySQL is a very popular, open source, freely available, relational database management system available from MySQL.com
It has been installed on mi-linux and forms the backbone of most web applications/resources available to students, including this wiki!
Students will most likely make advantage of the database server in conjunction with web applications (created in PHP, Java, etc.) to form the data storage/retrieval component of any applicatio
HOW TO REGISTER
If you wish to create your own database, follow these steps
1. Visit https://mi-linux.wlv.ac.uk/facilities
2. Login with your university account and password
3. Click "Register for a MySQL Account"
4. Read and agree to the terms and conditions.
4. The next screen will display your connection details, including a RANDOMLY GENERATED PASSWORD and details on how to connect to the admin interface. Please make a note of these details
5. Once registered, you can administer your database using the phpMyAdmin web interface.
How to Connect to MySQL from a Java Application
If you are looking to connect to your MySQL database from your own Java program there are several steps you need to take. If you have seen a Java-to-database connection demonstrated before you will know that firstly you require the Connection String. In the Data Model the Connection String should be provided as a global variable and initialise the Connection String within the Data Model's constructor such as this:
public class DataModel{ private String connectionString; public DataModel() { /* * Where 0777777 is your student number * and where drowssap is your mysql password */ connectionString = "jdbc:mysql://mi-linux.wlv.ac.uk/db0712329?user=0777777&password=drowssap"; } ...etc...
This however will still not work as you need to import a mysql connector into your project, to do this follow these instructions:
The SQL Connector needs to be downloading from this link: [1] There's a GB mirror halfway down the list.
Once that zip file has been downloaded and extracted there can be found a jar file inside called something like "mysql-connector-java-5.1.7-bin.jar". This file needs to be imported into the build path of the Project in question.
Rigtht Click Project > Build Path > Configure Built Path > Libraries tab > Add External Jar > Ok
With the Connector imported and with your connectionString in place all that is required now is to produce your Data Model's methods that use the connection connection.
Here is a snippet of code displaying such use; this method assumes the role of querying a table which contains details of Modules containing information such as Module Code, Module Title and Module Leader.
public ArrayList<Module> getModules() { ArrayList<Module> ml = new ArrayList<Module>(); try { // use connection string for myaql mi-linux connection. Connection conn = DriverManager.getConnection(connectionString); Statement stmt = conn.createStatement(); /* You will then use stmt to execute your query * and place the results of that query into a resultSet. */ ...etc..
It is important to place your connection in a try/catch block to catch any errors with the query or connection itself, it is much more efficient than setting up your own query checks.
More information about Connecting to a database via a java based application can be found in Matthew Burley's CP2120 Software Design Lecture Notes on Relational Database Management System Persistence Data Models - pardon the mouth full.
How to connect PHP to an Oracle database
Please see example here.