high-volume applications information and also need to give these data persistence. To achieve this, many systems applications interact with database managers to accommodate far-reaching information and data. In this post I will discuss how we can make a Java application to connect to a database created in MySQL.
To achieve this we need to have installed the MySQL driver (which can be obtained for free from their official website ) and driver that allows us to make such connection (classes that manipulate database records data).
Once downloaded
have to install the database manager. Inside the zip file of MySQL driver will have to find the jar file to place within our portfolio of Java classes (
.../java/jdk1.6.0_xx/jre/lib/ext ). In my case the downloaded file is mysql-connector-java-5.1.10.zip and contains a jar file called mysql-connector-java-5.1.10.bin.jar With
manager installed and the MySQL driver in place we can create a test database and add some records. The following example is the structure of a database to store contacts in a book:
However, in order to manipulate those records have to make use of the classes in package java.sql
. Some of the classes within this package are: Connection A connection with a particular database.
DatabaseMetaData contains information about the database. DriverManager
Basic services for the management of a set of JDBC drivers .
ResultSet Table of data representing a result set of databases, which is usually generated by executing a statement that queries the database.
ResultSetMetaData
object that can be used to obtain information on the types and properties of the columns in a ResultSet object. Statement
object used for executing a SQL statement.
The following code shows the records from the contacts in our database Agenda:
Lines 9 and 10 contain the instruction code for connection to the database through the getConnection (). This method has the following forms: static Connection getConnection (String url)
try to connect to the database of the given url. Static
Connection getConnection (String url, Properties info)
try to connect to the database of the given url. Static
Connection getConnection (String url, Properties info)
Try to connect to the database espeficicando the url given a set of properties.
static Connection getConnection (String url, String user, String pass)
try to connect to the database of the given url, using a username and password.
try to connect to the database of the given url, using a username and password.
Note that line 10 is marked with blue. Be careful to replace the values \u200b\u200b "user" and "password" for the appropriate user data that can access the database in the handler.
0 comments:
Post a Comment