Introduction
Java provides the API for developing database applications that works with any
relational database systems.
You may have heard a lot about database systems. Database systems are everywhere. Your
social security information is stored in a database by the government. If you shop online,
your purchase information is stored in a database by the company. If you attend a university,
your academic information is stored in a database by the university. Database systems not
only store data, they also provide means of accessing, updating, manipulating, and analyzing
data. Your social security information is updated periodically, and you can register for courses
online. Database systems play an important role in society and in commerce.
This chapter introduces database systems, the SQL language, and how database applications
can be developed using Java.
SQL is the standard database language for defining and accessing databases.
A database is a repository of data that form information. When you purchase a database
system—such as MySQL, Oracle, IBM’s DB2 and Informix, Microsoft SQL Server, or
Sybase—from a software vendor, you actually purchase the software comprising a database
management system (DBMS). Database management systems are designed for use by professional
programmers and are not suitable for ordinary customers. Application programs are
built on top of the DBMS for customers to access and update the database. Thus, application
programs can be viewed as the interfaces between the database system and its users.
Application programs may be stand-alone GUI applications or Web applications and may
access several different database systems in the network,
SQL (pronounced “S-Q-L” or “sequel”) is the universal language for accessing relational
database systems. Application programs may allow users to access a database without directly
using SQL, but these applications themselves must use SQL to access the database.
Before learning about JDBC, you most have simple knowledge of SQL queries.
Create,update,delete,insert ,drop,ect must be known.
JDBC
JDBC is the Java API for accessing relational database.
The Java API for developing Java database applications is called JDBC. JDBC is the trademarked name of a Java API that supports Java programs that access relational databases. JDBC is not an acronym, but it is often thought to stand for Java Database Connectivity. JDBC provides Java programmers with a uniform interface for accessing and manipulating relational databases. Using the JDBC API, applications written in the Java programming language can execute SQL statements, retrieve results, present data in a user-friendly interface, and propagate changes back to the database. The JDBC API can also be used to interact with multiple data sources in a distributed, heterogeneous environment. The relationships between Java programs, JDBC API, JDBC drivers. The JDBC API is a set of Java interfaces and classes used to write Java programs for accessing and manipulating relational databases. Since a JDBC driver
serves as the interface to facilitate communications between JDBC and a proprietary database, JDBC drivers are database specific and are normally provided by the database vendors. You need MySQL JDBC drivers to access the MySQL database, and Oracle JDBC drivers to access the Oracle database. For the Access database, use the JDBC-ODBC bridge driver included in the JDK. ODBC is a technology developed by Microsoft for accessing databases on the Windows platform. An ODBC driver is preinstalled on Windows. The JDBC-ODBC bridge driver allows a Java program to access any ODBC data source.
Java Source code for Database connection.
import java.sql.Connection;
import java.sql.DriverManager;
public class DatabaseConnection {
Connection con;
public DatabaseConnection(){
}
public Connection setConnection(){
try {
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/profile", "root", ""); //profile is name of database, root is username to connect datatabase and no password
} catch (Exception e) {
e.printStackTrace();
}
return con;
}
}
This comment has been removed by the author.
ReplyDelete