Trending December 2023 # Gude To How Does Mysql Alias Works With Examples # Suggested January 2024 # Top 16 Popular

You are reading the article Gude To How Does Mysql Alias Works With Examples updated in December 2023 on the website Daihoichemgio.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested January 2024 Gude To How Does Mysql Alias Works With Examples

Introduction to MySQL Alias

MySQL Alias is used when we are willing to provide an alternate name for the column or table. Temporarily assigning a name for a column or table that is user-friendly and understandable. Alias is mainly used for the column and table. Column Alias gives an alternate name for the column headings, accessible for the end-user to understand. Table aliasing involves assigning alternate names to tables, which makes it more convenient to read and use them, particularly in the context of joins.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Syntax:

Given below is the syntax of the alias and how it is used in the column and table:

Here in the above syntax, we provide the column name which we want an alternate to be given. “AS” is optional. And at last, we provide the alias name.

Here in the above syntax, we provide the table name we want to give the alternate. “AS” is optional. And at last we provide the alias name.

How does MySQL Alias works?

MySQL Alias works in the following way:

1. Column Alias

Code:

CREATE TABLE EMPLOYEE_INFORMATION ( EMPLOYEE_ID VARCHAR(10), EMPLOYEE_NAME VARCHAR(20), DEPT_ID VARCHAR(10), EMPLOYEE_SAL INT );

Now let us insert data into the above table:

Code:

INSERT INTO EMPLOYEE_INFORMATION VALUES ('E1','SAM','D1', 90000); INSERT INTO EMPLOYEE_INFORMATION VALUES ('E2','WILL','D1', 80000); INSERT INTO EMPLOYEE_INFORMATION VALUES ('E3','SOMY','D1', 50000); INSERT INTO EMPLOYEE_INFORMATION VALUES ('E4','FRED','D1', 10000); INSERT INTO EMPLOYEE_INFORMATION VALUES ('E5','HARRY','D2', 70000); INSERT INTO EMPLOYEE_INFORMATION VALUES ('E6','PONY','D2', 70000); INSERT INTO EMPLOYEE_INFORMATION VALUES ('E7','DAVID','D2', 40000); INSERT INTO EMPLOYEE_INFORMATION VALUES ('E8','PETER','D3', 30000); INSERT INTO EMPLOYEE_INFORMATION VALUES ('E9','RAM','D3', 60000); INSERT INTO EMPLOYEE_INFORMATION VALUES ('E10','SAI','D3', 50000); select * from EMPLOYEE_INFORMATION;

Output:

Now let us give an alias to each column above without an underscore in the column name.

The below example is for the column aliasing:

Code:

SELECT EMPLOYEE_ID AS "EMPLOYEE ID", EMPLOYEE_NAME AS "EMPLOYEE NAME", DEPT_ID AS "DEPARTMENT_ID", EMPLOYEE_SAL AS "EMPLOYEE SALARY" FROM EMPLOYEE_INFORMATION;

Output:

2. Table Alias

A table alias is mainly used when we are performing joins on the table.

Below are the sample table with sample data to show how the alias is performed.

Code:

CREATE TABLE SAMPLE_A ( COL_A INT, COL_B INT );

Insert data into the table:

Code:

INSERT INTO SAMPLE_A VALUES (1, 5); INSERT INTO SAMPLE_A VALUES (2, 4); INSERT INTO SAMPLE_A VALUES (3, 3); INSERT INTO SAMPLE_A VALUES (4, 2); INSERT INTO SAMPLE_A VALUES (5, 1); select * from SAMPLE_A;

Output:

Now let us create another table.

Code:

CREATE TABLE SAMPLE_B ( COL_A INT, COL_B INT );

Insert data into the table:

Code:

INSERT INTO SAMPLE_B VALUES (1, 5); INSERT INTO SAMPLE_B VALUES (2, 4); INSERT INTO SAMPLE_B VALUES (3, 3); INSERT INTO SAMPLE_B VALUES (4, 2); INSERT INTO SAMPLE_B VALUES (5, 1); Select * from SAMPLE_B;

Output:

Now let us perform joining on above and use an alias:

a. Without Table alias

Code:

SELECT * FROM SAMPLE_A JOIN SAMPLE_B ON  SAMPLE_A.COL_A = SAMPLE_B.COL_A

Output:

b. With Table alias

Code:

SELECT * FROM SAMPLE_A A JOIN SAMPLE_B B ON A.COL_A = B.COL_A

Output:

Examples of MySQL Alias

Given below are the examples:

Example #1 – Column alias

Now let us find the maximum and minimum salary that each department has for the above table which we created earlier.

Code:

SELECT DEPT_ID AS "DEPARTMENT ID", MAX(EMPLOYEE_SAL)AS "MAXIMUM EMPLOYEE SALARY", MIN(EMPLOYEE_SAL)AS "MINIMUN EMPLOYEE SALARY" FROM EMPLOYEE_INFORMATION GROUP BY DEPT_ID;

Output:

Example #2 – Table alias

If we consider a table giving an alternate name for the table is said to be table aliasing.

Code:

SELECT * FROM SAMPLE_A JOIN SAMPLE_B ON SAMPLE_A.COL_A = SAMPLE_B.COL_A

Output:

Here every time mentioning the whole table name in the joining condition would be difficult.

b. With Table alias

Code:

SELECT * FROM SAMPLE_A A JOIN SAMPLE_B B ON A.COL_A = B.COL_A

Output:

Here every time mentioning the whole table name in the joining condition would be difficult. Hence, we have mentioned the alias for the table. SAMPLE_A has alias name as “A”. And SAMPLE_B table has “B” alias.

We could see that ‘AS’ is not written while mentioning the alias.

We can mention alias as below as well:

Code:

SELECT * FROM SAMPLE_A AS A JOIN SAMPLE_B AS B ON A.COL_A = B.COL_A

Output:

Conclusion

Things that need to put in mind regarding the MySQL Alias are if you want space to be provided in the alias_name, then we must enclose the alias_name in quotes. Spaces are accepted when we are defining in the column name. However, it is not good practice to use space in the table name aliasing. The alias_name is only valid within the scope of the SQL statement.

Recommended Articles

We hope that this EDUCBA information on “MySQL Alias” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

You're reading Gude To How Does Mysql Alias Works With Examples

Learn How Does Bin() Function Works In Mysql?

Introduction to MySQL BIN()

MySQL BIN() function represents one of the string functions of converting a given number into the binary symbol as a string value result. This function is responsible for the binary representation of digits in a series value. The MySQL BIN() function returns an identical binary string illustration of the provided binary value of a BIGINT numeral. While execution, if the argument’s value in the function is, provides as NULL, the resultant value of MySQL BIN() function will also be NULL.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

The CONV() function in MySQL has a syntax structure format of CONV(Num,10,2) and is considered the equivalent of the BIN() function. Here, Num denotes the BIGINT number, 10 represents base 10 decimal, and binary with base 2. When executed in the CONV() function format, the number changes from a base 10 decimal number to a base two binary digit string. It means this returns a similar output as BIN() function gives.

Syntax

The succeeding structure defines the syntax of the BIN() function in MySQL:

SELECT BIN(Dec_Num);

The parameter used in the above syntax of the BIN() MySQL function is the decimal digit or digits. It converts into its corresponding binary sequence value when we apply the function on a query in the database table.

For using this function while implementing in an SQL query, we need to follow the following syntax:

SELECT BIN(Num_Expr) FROM Table_Source;

Here, the Num_Expr denotes the BIGINT number that accepts a valid specified expression, column value, or any other numeral converted to a binary string when the result is fetched from the database query. Table_Source is the name of the database table.

How does BIN() function work in MySQL?

As mentioned above, the BIN() function produces a binary representation of the number declared in the function argument.

Suppose we want to convert the number 12 into a binary string output. Then, using the syntax, we have an introductory query statement as below:

SELECT BIN(12);

Output:

The output displays the binary representation of the decimal number 12, which is obtained by converting it to base 2.

It should be distinguished that the string value basedBIN() function gives NULL when the conversion of the number into binary string value has a length that exceeds the value of the max_allowed_packetsystem variable.

In this process, the initial position of the string but for those MySQL functions that work on the string is numbered 1.

For those functions which implement length arguments, the non-integer parameters are rounded to the nearest integer.

For example,

SELECT BIN(5126);

Output:

Now, we will take about CONV() function for the conversion then; the result of the function will be as follows:

SELECT CONV(5,10,2);

Output:

The process of base-2 conversion converts the decimal number 5 to its equivalent binary string representation, as shown in the screenshot above.

Examples of MySQL BIN()

We are now going to demonstrate the MySQL BIN() function and its uses as per the following examples:

Example #1

MySQL BIN() function simple examples:

SELECT BIN(34.56);

Output:

We are executing the BIN() function with a NULL value to pass in the argument.

SELECT BIN(NULL);

Also, let us give any character value with the Varchar data type in MySQL, then the conversion result will be as below:

SELECT BIN('ABC');

Output:

From the result, we know that if we pass any string value for the BIN() function, the output is zero(0). Therefore, we must provide a numeral for retrieving the Binary value.

Example #2

MySQL BIN() function example with a certain range of values:

Let us consider the next example that produces several binary values from different numbers ranging from 11 to 20 decimal digits. The statement for BIN() function is:

SELECT BIN(11) AS '11', BIN(12) AS '12', BIN(13) AS '13', BIN(14) AS '14', BIN(15) AS '15', BIN(16) AS '16', BIN(17) AS '17', BIN(18) AS '18', BIN(19) AS '19', BIN(20) AS '20';

Output:

Example #3

MySQL BIN() function example with Column values in the records of a table:

To implement this example, we have used the table data of a database table as a sample provided in the following example:

We have an Employee table; let’s create this one:

CREATE TABLE Employees ( EmpID int NOT NULL, EmpName varchar(255) NOT NULL, EmpProfile varchar(255), EmpSalary int, EmpPF int , PRIMARY KEY (EmpID) ); INSERT INTO Employees (EmpID, EmpName, EmpProfile, EmpSalary, EmpPF)VALUES ('210', 'Radha', 'Engineer', '50000', '3600'), ('211', 'Mohan', 'Manager', '40000', '2000'), ('212', 'Dev', 'Executive', '32000', '1800'), ('213', 'Madhuri', 'Blogger', '20000', Null), ('214', 'Rita', 'Pilot', '48000', '5000');

The table is displayed as follows:

SELECT * FROM Employees;

Output:

First of all, let us view the information through the below MySQL statement:

SELECT EmpName, EmpSalary, EmpPF FROM Employees;

Output:

Now, we apply MySQL BIN() function to the selected columns in the table above. We will write the below statement to provide the binary values of the column data values available in integer data type columns, i.e., EmpSalary and EmpPF.

Here, we use the BIN() function along with the SELECT query to find the binary string value equivalent to the current corresponding column values in the given table:

SELECT EmpName, EmpProfile, EmpSalary,BIN(EmpSalary) AS Binary_Salary,EmpPF, BIN(EmpPF) AS Binary_PF FROM Employees;

Output:

We have shown the column data binary representation of the corresponding table column values that we have applied as input to the MySQL BIN() function arguments.

This MySQL BIN() function helps evaluate the binary form of any number as we convert the decimal number into a binary one when we start calculating manually using the mathematical formula.

Conclusion

The MySQL BIN() function converts a decimal numeral to its equivalent binary string value when we pass the argument using the function in a MySQL statement execution.

BIN() function holds a binary result of Dec_Num as in the syntax described above, indicating a long, long number, i.e., BIGINT.

BIN() function is for number conversion to base 2 value, but the CONV() function can enable us to generate results on different bases, not only restricted to a binary one.

Recommended Articles

We hope that this EDUCBA information on “MySQL BIN()” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

How Rank() Works In Mysql With Sample Queries

Introduction to MySQL REGEXP

The following article provides an outline for MySQL REGEXP. A regular expression is used with SELECT queries to search for patterns, generally strings, in the database. We can consider the REGEXP as a search tool to understand easily. This operation is similar to the “LIKE …%” operator, which also does pattern matching. REGEXP can be combined with almost all operators from the keyboard. The regular expression operator (REGEXP) can be considered a separate set of languages.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Syntax:

The syntax for the REGEXP function is as follows:

expression REGEXP pattern

Here, the ‘expression’ stands for the column name or the expression to be looked at from the database. The term ‘pattern’ represents the string to be searched in the DB.

While using a real query, the REGEXP will be as follows:

SELECT * from table_name where expression REGEXP pattern;

The pattern will be mentioned with single quotes.

How RANK() works in MySQL?

Code:

select * from emp;

Output:

Example #1

Here we need to get the details of employees with the character ‘a’ in their names.

Code:

select * from emp where E_Name REGEXP 'a';

Output:

To analyze the query, we have described it to select all details from table ’emp’ with the character’ a’ in their E_Name field. And as we look at the output, we can understand that out of 5 employees, 3 have the character ‘a in their names: Alan, Carl, and Dave.

So we wrote the query for selecting details with a string in it. Here we just mentioned selecting data with a character. The specified character could be anywhere in the word.

Example #2

Here we see how to select the details of an employee whose name starts with a specific character.

Code:

select * from emp where E_Name REGEXP '^a';

Output:

Here, we used the operator ‘^’ along with the pattern to be searched and thus got the details of employees whose name starts with the character ‘a’.

So far, the characters or strings considered as the pattern were not case-sensitive.

Example #3

Here we make it more specific by making it case-sensitive.

Code:

select * from emp where E_Name REGEXP BINARY '^a';

Output:

Since we specified the lowercase character ‘a’ in the query, no results could be retrieved.

Example #4

Here let’s make it upper case and try to retrieve the output.

Code:

select * from emp where E_Name REGEXP BINARY '^E';

Here we got the details of employees with the character ‘E’ in upper case at the beginning.

Example #5

Here we can search a part of a word as well.

Code:

select * from emp where Location REGEXP BINARY '^New';

Output:

The query says to select the entries for which the location field starts with the word ‘New’. And from the sample table, we get two rows satisfying the condition, which are New York and New Jersey. Also, since the BINARY function is mentioned in the query, it will check for case sensitivity too.

Example #6

Here we see how to search for an ending character/ word/ part of the word.

Code:

select * from emp where Location REGEXP 'a$';

Output:

The query specifies to select entries from the employee table for which the location field ends with the character ‘a’. And as the output picked two entries from the table, which have character ‘a’ at the final character of the location, California and Alabama. So far, we have discussed searching for a single character, word, or a portion of a word.

Example #7

Here we can see how to search for a set of characters and a range of characters.

For example, let’s assume we need to identify the rows from the employee table with any of the characters’ a,’ b’, and ‘c’ in the E_Name column. We can put this exact requirement in two ways, first by mentioning all three characters as an array and second by mentioning the range of characters from ‘a’ to ‘c’.

Code:

select * from emp where E_Name REGEXP '[abc]';

Example #8

Code:

select * from emp where E_Name REGEXP '[a-c]';

Output:

Query 8 will pick those rows with characters within the range specified within the square bracket ‘[….]’. So those employee names, which include any characters ranging from ‘a’ to ‘c’, will be picked and displayed.

Example #9

Code:

Output:

Another operator generally used along with REGEXP is the number of characters in the word. Suppose we have to search for employee names with the same four characters; then we need to specify four instances of ‘.’ between the beginning and closing strings, which are ‘^’ and ‘$’.

Example #10

Here let’s write the query to identify names with four characters.

Code:

select * from emp where E_Name REGEXP '^....$';

Output:

Conclusion – MySQL REGEXP

In this article, we have seen the REGEXP operator, which searches for characters or patterns in a table. Now we have gained familiarity with various string operators commonly used with the REGEXP operator, along with their syntax. We can use almost all string operators with the REGEXP operator in MySQL.

Recommended Articles

We hope that this EDUCBA information on “MySQL REGEXP” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

How Parents() Function Works With Examples

Introduction to jQuery parents

Web development, programming languages, Software testing & others

Syntax:

$(selector ).parents(filter_expr)

Or

$(selector).parents()

In the above-given syntax, the jQuery parents() function is applied on a particular element. It is a selector tag where all ancestors are returned when this parent () function is applied to this selector tag. This function does not take any parameters, but still, it is considered optional.

selector: In this, the selector is nothing, but it is an HTML element or tag of which we want the element’s ancestors or grandparent element to be returned.

filter_expr: This is an optional parameter in this function for specifying the selector tag expression to look upwards in the DOM tree for searching with all this selector’s ancestors.

How parents() Function Works with Examples

In jQuery, the parents() function is a built-in function for displaying all the ancestors of the selected HTML tag for which this function needs to be applied. The working of this function is very simple Firstly, it will check for the specified selector tag, followed by the period (dot) operator and the parents() method to this selector tag. This function will traverse the entire DOM tree, where this tree is the representation of elements of the jQuery object.

Therefore parents() function traverses this DOM tree in the upward direction to search all the element’s ancestors such as grandparent element, great grandparent element, etc. are all displayed, which means this function parent () returns all the ancestor elements of the particular selected or matched HTML tag that is specified before the function declaration, and this particular specified selector optionally filters it. This function returns the element set in reverse order for the given multiple DOM having the original set, and the duplicate elements are removed and displayed.

Example #1

Code:

.parents_func_body* { display: block; border: 2px solid red; color: red; padding: 5px; margin: 15px; } <script $(document).ready(function() { $(“p”).parents().css({ “color”: “blue”, “border”: “2px solid red” }); });

Output:

In the above example, we have first made the document ready for the web page to be displayed according to the given function in the above code. In this, we are using the .ready() function for making the document ready. Then we specify the parents() function in which “p” the paragraph tag as a selector for this function, which means this function returns all the ancestor elements of the element “P” in the above code. Finally, we are applying the .css() function to properly display each element with described properties. This logic is defined or declared within the script tag, which is within the head tag.

Example #2

Now let us see another example for demonstrating the parents() function in which we are passing the optional parameters as some other HTML elements or selectors.

Code:

.main *{ border: 2px solid red; padding: 10px; margin: 10px; } function parents_func(){ $(document).ready(function(){ $(“p”).parents(“li, h2”).css({ “border”: “3px dashed blue”}); }); }

Conclusion – jQuery parents

In this article, we conclude that the parents() function in jQuery is very simple and is used to search or traverse the DOM tree, which consists of various elements to find out the parent element to help any developer to easily correct or upgrade any details. Similar to this parent () function, which returns all its ancestors elements of any selected element, even the parent() function also returns the direct parent element but not all the ancestors elements.

Recommended Articles

This is a guide to jQuery parents. Here we discuss the introduction and how parents() function works with examples, respectively. You may also have a look at the following articles to learn more –

How Does Mysql Union Work

Introduction to MySQL Union

MySQL Union combines the output of two or more “SELECT” statements. The output of the “UNION” will be without duplicate rows. The number of fields in the table should be the same, and the data type of the columns should be the same. If you don’t want the duplicate rows to be ignored, we can do it with the “UNION ALL”. In the result set of the UNION table, the column name will be the same as the column name of the first select statement. In this session, let us learn more about the usage of the UNION and let see the examples of it: –

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Syntax

Now let us see the syntax:

SELECT column_1, column_2,...column_n FROM First_tables [WHERE conditions] UNION [DISTINCT] SELECT column_1, column_2,...column_n FROM Second_tables [WHERE conditions]; How Does MySQL Union Work? Table1 create table uniona ( cola INT ); Table2 create table unionb ( colb INT );

Insert data into the tables as below: – Below is for the “uniona” table.

insert into uniona values (99); insert into uniona values (95); insert into uniona values (94); insert into uniona values (93); insert into uniona values (92); insert into uniona values (91); insert into uniona values (99);

Select the items for the table “uniona”: –

select * from uniona;

Let us see the screenshot for the same: –

Insert data into the tables as below: – Below is for the “unionb” table.

insert into unionb values (99); insert into unionb values (34); insert into unionb values (35); insert into unionb values (33); insert into unionb values (32); insert into unionb values (31); insert into unionb values (30); insert into unionb values (29); insert into unionb values (28);

Select the items for the table “unionb”: –

select * from unionb;

Let us see the screenshot for the same: –

Now let us perform union operation on the above tables: –

select * from uniona union select * from unionb;

Select the items for the table “uniona” and “unionb” and perform the union: –

Let us see the screenshot for the same: –

Example:

Now let us perform UNION on three tables as below: –

--Table1: - create table TEST_A ( cola INT ); --Table2: - create table TEST_B ( colb INT ); --Table3: - create table TEST_C ( colc INT ); insert into test_a values (1); insert into test_a values (2); insert into test_a values (3); insert into test_a values (4);

Let us see the columns of the table: –

select * from test_a; insert into test_b values (1); insert into test_b values (5); insert into test_b values (6); insert into test_b values (7); insert into test_b values (8);

Let us see the columns of the table: –

insert into test_c values (1); insert into test_c values (10); insert into test_c values (11); insert into test_c values (12); insert into test_c values (13);

Let us see the columns of the table: –

Output:

Screenshot for the above: –

select * from test_a union select * from test_b union select * from test_c

Output:

Here in the above output, we need to check two things: –

The column name of the result set is the column name of the first “select” statement. Here it is “cola”.

The second thing is that we can see that there is a duplicate row in all the tables of value “1”. But in the result set, UNION ignores the duplicate because we have only one row of data, “1”.

Screenshot for the same: –

Example of MySQL Union

Actual Table:-

create table St_marks ( subject_name varchar(20), marks int ); insert into st_marks values ('English', 98); insert into st_marks values ('Mathematics', 93); insert into st_marks values ('Physics', 78); insert into st_marks values ('Chemistry', 67); insert into st_marks values ('Art', 43); insert into st_marks values ('Music Class', 67); select * from st_marks;

Backup table:-

create table St_marks_bkup ( subject_name varchar(20), marks int ); insert into st_marks_bkup values ('English', 98); insert into st_marks_bkup values ('Mathematics', 93); insert into st_marks_bkup values ('Physics', 78); insert into st_marks_bkup values ('Chemistry', 67); insert into st_marks_bkup values ('Art', 43); insert into st_marks_bkup values ('Music Class', 67); select * from st_marks_bkup;

Output:

A screenshot for the above is given below: –

SELECT SUBJECT_NAME, MARKS FROM ST_MARKS UNION SELECT SUBJECT_NAME, MARKS FROM ST_MARKS_BKUP;

Output:

The screenshot is for the same: –

Conclusion

Things that need to remember are as below: –

MySQL Union combines the output of two or more “SELECT” statements. The output of the “UNION” will be without duplicate rows.

The number of fields in the table should be the same, and the data type of the columns should be the same. If you don’t want the duplicate rows to be ignored, we can do it with the “UNION ALL”.

In the result set of the UNION table, the column name will be the same as the column name of the first select statement.

Recommended Articles

We hope that this EDUCBA information on “MySQL Union” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

How Url Works In Jdbc With Examples?

Definition of JDBC URL

JDBC provides the URL to identify the database, so we can easily recognize the required driver and we can connect it. Basically JDBC URL we can use as database connection URL as per user requirement. When the driver loaded successfully we need to specify the required database connection URL to connect the database that the user wants. We know that the JDBC URL always starts with the JDBC keyword for the database connection; basically, the URL depends on the JDBC driver. We also need to provide the different parameters with the JDBC URL that is port number, hostname, database name, user name, and password, etc.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

specified protocol name//[specified host name][/specified database name][username and password]

Explanation

By using the above syntax, we try to implement the database connection, here we use different parameters such as protocol name, the hostname that we want, specified database name that we need to connect with the username and password, the database name depends on the user.

How URL works in JDBC?

Now let’s see how the URL works in JDBC as follows.

Register the JDBC Driver: After importing the class we need to load the JVM to fulfill that is it loaded the required driver as well as memory for JDBC request.

Database URL Formation: In this step, we need to provide the correct parameter to connect the specified database that we already discussed in the above point.

Create the Connection Object: After the formation of the URL, we need to create the object of connection that means we can call the DriverManager with grtConnection() methods to establish the connection with a specified database name.

Now let’s see in detail how we can import the JDBC Driver as follows.

Basically, the import statement is used to compile the java program and is also used to find the classes that are helpful to implement the source code as per user requirements. By using these standard packages, we can perform different operations such as insertion, delete and update as per user requirements.

import java.sql.*;

Now let’s see how we can register the JDBC Driver as follows.

We just need to import the driver before using it. Enlisting the driver is the cycle by which the Oracle driver’s class document is stacked into the memory, so it tends to be used as an execution of the JDBC interfaces.

You need to do this enrollment just a single time in your program. You can enlist a driver in one of two different ways.

The most widely recognized way to deal with registering a driver is to utilize Java’s Class.forName() technique, to progressively stack the driver’s class document into memory, which naturally enlists it. This technique is ideal since it permits you to make the driver enrollment configurable and compact.

2. By using DriverManager.registerDriver():

You should utilize the registerDriver() technique in case you are utilizing a non-JDK agreeable JVM, for example, the one given by Microsoft.

After you’ve stacked the driver, you can set up an association utilizing the DriverManager.getConnection() technique. JDBC provides the different JDBC drivers for the different database systems and we can utilize them as per the user requirement.

1. MySQL JDBC URL format:

This is the first JDBC URL format that can be used in MySQL to establish the connection with the required database name. The format of this URL is as follows.

(Connection con_obj = DriverManager.getConnection(specifed_jdbcUrl, user defined username, user defined password))

Explanation

In the above format, we use DriverManager.getConnection method to establish the connection with the database; here we need to pass the specified JDBC URL as well as we need to pass the username and password. The username and password fields are depending on the user. In JDBC URL we need to pass all parameters that we require to make the connection such as database name, protocol, etc.

2. Microsoft SQL Server URL format:

This is another famous URL format for the database system. Suppose we need to connect to the Microsoft SQL Server from a Java application at that time we can use the below-mentioned format as follows.

jdbc:sqlserver://[specified serverName[ specified instanceName][:required portNumber]][;property(that user defined properties)]

In the above syntax, we need to mention the server name that is the address of the server, or we can say that domain name or IP address. Also, we need to mention the instance name for server connection if we leave then it uses the default. In the same way, we can use port numbers and properties.

3. PostgreSQL JDBC URL format:

PostgreSQL is a famous open-source database system. So we can use the below-mentioned JDBC format as follows.

Jdbc:postgresql://hostname:port number/specified database name and properties. Examples

Now let’s see different examples of JDBC URLs for better understanding as follows.

import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.Statement; public class connection_t { public static void main(String args[]){ String m_url = " jdbc:mysql://localhost "; Connection con_obj = DriverManager.getConnection(m_url, "root", "root"); System.out.println("Connection successfully established with database. . ."); } }

Explanation

In the above example, we import the dependencies that are required to establish the connection with the database such as SQL. connection, SQL.DriverManger etc. After that, we import the class as shown. Here we also mentioned a connection string with connection parameters such as DriverManager.getConnection() method as shown. The final output or end result of the above example we illustrated by using the following screenshot as follows.

In the same way, we can connect to the Microsoft server and PostgreSQL as per our requirements.

Conclusion

We hope from this article you learn the JDBC URL. From the above article, we have learned the basic syntax of JDBC URLs as well we also see the different connection string URLs with different examples of JDBC URLs. From this article, we learned how and when we use the JDBC URL.

Recommended Articles

This is a guide to JDBC URL. Here we discuss the definition, syntax, How URL work in JDBC ? and examples respectively. You may also have a look at the following articles to learn more –

Update the detailed information about Gude To How Does Mysql Alias Works With Examples on the Daihoichemgio.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!