- GUI
- Windows API tutorial
- Introduction to Windows API
- Windows API main functions
- System functions in Windows API
- Strings in Windows API
- Date & time in Windows API
- A window in Windows API
- First steps in UI
- Windows API menus
- Windows API dialogs
- Windows API controls I
- Windows API controls II
- Windows API controls III
- Advanced controls in Windows API
- Custom controls in Windows API
- The GDI in Windows API
- PyQt4 tutorial
- PyQt5 tutorial
- Qt4 tutorial
- Introduction to Qt4 toolkit
- Qt4 utility classes
- Strings in Qt4
- Date and time in Qt4
- Working with files and directories in Qt4
- First programs in Qt4
- Menus and toolbars in Qt4
- Layout management in Qt4
- Events and signals in Qt4
- Qt4 Widgets
- Qt4 Widgets II
- Painting in Qt4
- Custom widget in Qt4
- The Breakout game in Qt4
- Qt5 tutorial
- Introduction to Qt5 toolkit
- Strings in Qt5
- Date and time in Qt5
- Containers in Qt5
- Working with files and directories in Qt5
- First programs in Qt5
- Menus and toolbars in Qt5
- Layout management in Qt5
- Events and signals in Qt5
- Qt5 Widgets
- Qt5 Widgets II
- Painting in Qt5
- Custom widget in Qt5
- Snake in Qt5
- The Breakout game in Qt5
- PySide tutorial
- Tkinter tutorial
- Tcl/Tk tutorial
- Qt Quick tutorial
- Java Swing tutorial
- JavaFX tutorial
- Java SWT tutorial
- wxWidgets tutorial
- Introduction to wxWidgets
- wxWidgets helper classes
- First programs in wxWidgets
- Menus and toolbars in wxWidgets
- Layout management in wxWidgets
- Events in wxWidgets
- Dialogs in wxWidgets
- wxWidgets widgets
- wxWidgets widgets II
- Drag and Drop in wxWidgets
- Device Contexts in wxWidgets
- Custom widgets in wxWidgets
- The Tetris game in wxWidgets
- wxPython tutorial
- Introduction to wxPython
- First Steps
- Menus and toolbars
- Layout management in wxPython
- Events in wxPython
- wxPython dialogs
- Widgets
- Advanced widgets in wxPython
- Drag and drop in wxPython
- Internationalisation
- Application skeletons in wxPython
- The GDI
- Mapping modes
- Creating custom widgets
- Tips and Tricks
- wxPython Gripts
- The Tetris game in wxPython
- C# Winforms Mono tutorial
- Java Gnome tutorial
- Introduction to Java Gnome
- First steps in Java Gnome
- Layout management in Java Gnome
- Layout management II in Java Gnome
- Menus in Java Gnome
- Toolbars in Java Gnome
- Events in Java Gnome
- Widgets in Java Gnome
- Widgets II in Java Gnome
- Advanced widgets in Java Gnome
- Dialogs in Java Gnome
- Pango in Java Gnome
- Drawing with Cairo in Java Gnome
- Drawing with Cairo II
- Nibbles in Java Gnome
- QtJambi tutorial
- GTK+ tutorial
- Ruby GTK tutorial
- GTK# tutorial
- Visual Basic GTK# tutorial
- PyGTK tutorial
- Introduction to PyGTK
- First steps in PyGTK
- Layout management in PyGTK
- Menus in PyGTK
- Toolbars in PyGTK
- Signals & events in PyGTK
- Widgets in PyGTK
- Widgets II in PyGTK
- Advanced widgets in PyGTK
- Dialogs in PyGTK
- Pango
- Pango II
- Drawing with Cairo in PyGTK
- Drawing with Cairo II
- Snake game in PyGTK
- Custom widget in PyGTK
- PHP GTK tutorial
- C# Qyoto tutorial
- Ruby Qt tutorial
- Visual Basic Qyoto tutorial
- Mono IronPython Winforms tutorial
- Introduction
- First steps in IronPython Mono Winforms
- Layout management
- Menus and toolbars
- Basic Controls in Mono Winforms
- Basic Controls II in Mono Winforms
- Advanced Controls in Mono Winforms
- Dialogs
- Drag & drop in Mono Winforms
- Painting
- Painting II in IronPython Mono Winforms
- Snake in IronPython Mono Winforms
- The Tetris game in IronPython Mono Winforms
- FreeBASIC GTK tutorial
- Jython Swing tutorial
- JRuby Swing tutorial
- Visual Basic Winforms tutorial
- JavaScript GTK tutorial
- Ruby HTTPClient tutorial
- Ruby Faraday tutorial
- Ruby Net::HTTP tutorial
- Java 2D games tutorial
- Java 2D tutorial
- Cairo graphics tutorial
- PyCairo tutorial
- HTML5 canvas tutorial
- Python tutorial
- Python language
- Interactive Python
- Python lexical structure
- Python data types
- Strings in Python
- Python lists
- Python dictionaries
- Python operators
- Keywords in Python
- Functions in Python
- Files in Python
- Object-oriented programming in Python
- Modules
- Packages in Python
- Exceptions in Python
- Iterators and Generators
- Introspection in Python
- Ruby tutorial
- PHP tutorial
- Visual Basic tutorial
- Visual Basic
- Visual Basic lexical structure
- Basics
- Visual Basic data types
- Strings in Visual Basic
- Operators
- Flow control
- Visual Basic arrays
- Procedures & functions in Visual Basic
- Organizing code in Visual Basic
- Object-oriented programming
- Object-oriented programming II in Visual Basic
- Collections in Visual Basic
- Input & output
- Tcl tutorial
- C# tutorial
- Java tutorial
- AWK tutorial
- Jetty tutorial
- Tomcat Derby tutorial
- Jtwig tutorial
- Android tutorial
- Introduction to Android development
- First Android application
- Android Button widgets
- Android Intents
- Layout management in Android
- Android Spinner widget
- SeekBar widget
- Android ProgressBar widget
- Android ListView widget
- Android Pickers
- Android menus
- Dialogs
- Drawing in Android
- Java EE 5 tutorials
- Introduction
- Installing Java
- Installing NetBeans 6
- Java Application Servers
- Resin CGIServlet
- JavaServer Pages, (JSPs)
- Implicit objects in JSPs
- Shopping cart
- JSP & MySQL Database
- Java Servlets
- Sending email in a Servlet
- Creating a captcha in a Servlet
- DataSource & DriverManager
- Java Beans
- Custom JSP tags
- Object relational mapping with iBATIS
- Jsoup tutorial
- MySQL tutorial
- MySQL quick tutorial
- MySQL storage engines
- MySQL data types
- Creating, altering and dropping tables in MySQL
- MySQL expressions
- Inserting, updating, and deleting data in MySQL
- The SELECT statement in MySQL
- MySQL subqueries
- MySQL constraints
- Exporting and importing data in MySQL
- Joining tables in MySQL
- MySQL functions
- Views in MySQL
- Transactions in MySQL
- MySQL stored routines
- MySQL Python tutorial
- MySQL Perl tutorial
- MySQL C API programming tutorial
- MySQL Visual Basic tutorial
- MySQL PHP tutorial
- MySQL Java tutorial
- MySQL Ruby tutorial
- MySQL C# tutorial
- SQLite tutorial
- SQLite C tutorial
- SQLite PHP tutorial
- SQLite Python tutorial
- SQLite Perl tutorial
- SQLite Ruby tutorial
- SQLite C# tutorial
- SQLite Visual Basic tutorial
- PostgreSQL C tutorial
- PostgreSQL Python tutorial
- PostgreSQL Ruby tutorial
- PostgreSQL PHP tutorial
- PostgreSQL Java tutorial
- Apache Derby tutorial
- SQLAlchemy tutorial
- MongoDB PHP tutorial
- MongoDB Java tutorial
- MongoDB JavaScript tutorial
- MongoDB Ruby tutorial
- Spring JdbcTemplate tutorial
- JDBI tutorial
Data source in Java
In this tutorial, we learn how to set up a data source in Java with MySQL.
In this tutorial, we use the MySQL Connector/J driver. It is the official JDBC driver for MySQL.
There are two basic ways of creating a connection to a database in Java: a) with a driver manager, b) using a data source. The data source has several advantages over a driver manager:
- it supports distributed transactions
- it provides a connection pooling technique
- it can be managed by a server, i.e. outside an application
The driver manager hampers the application performance as the connections are created and closed in Java classes. A driver manager can be used in simple testing applications; for complex application a data source is always recommended. Refer to MySQL Java tutorial to see how to use a driver manager in a Java application.
An object that implements the data source interface will typically be registered with a naming service based on the Java Naming and Directory Interface (JNDI) API.
JDBC
JDBC is an API for the Java programming language that defines how a client may access a database. It provides methods for querying and updating data in a database. JDBC is oriented towards relational databases. From a technical point of view, the API is as a set of classes in the java.sql
package. To use JDBC with a particular database, we need a JDBC driver for that database.
MySQL
MySQL is a leading open source database management system. It is a multi-user, multi-threaded database management system. MySQL is especially popular on the web. MySQL comes in two versions: MySQL server system and MySQL embedded system.
mysql> CREATE DATABASE testdb; Query OK, 1 row affected (0.02 sec)
We create a new testdb
database. We only need a database object in this tutorial; we will not work with tables. We will use a SELECT VERSION()
statement to get the version of MySQL database.
Command line application
In this example, we connect to the database with a command line Java application.

This is how the project structure looks like in NetBeans.
The MysqlDataSource
is a class for creating datasources.
db.properties
# mysql properties mysql.driver=com.mysql.jdbc.Driver mysql.url=jdbc:mysql://localhost:3306/testdb mysql.username=testuser mysql.password=test623
These are the properties for the MySQL database. The db.properties
file is located in the src/resources
subdirectory in this project.
ComLineDSEx.java
package com.zetcode; import com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource; import com.mysql.jdbc.jdbc2.optional.MysqlDataSource; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.PreparedStatement; import java.util.Properties; public class ComLineDSEx { public static MysqlDataSource getMySQLDataSource() throws FileNotFoundException, IOException { Properties props = new Properties(); FileInputStream fis = null; MysqlDataSource ds = null; fis = new FileInputStream("src/resources/db.properties"); props.load(fis); ds = new MysqlConnectionPoolDataSource(); ds.setURL(props.getProperty("mysql.url")); ds.setUser(props.getProperty("mysql.username")); ds.setPassword(props.getProperty("mysql.password")); return ds; } public static void main(String[] args) throws IOException, SQLException { Connection con = null; PreparedStatement pst = null; ResultSet rs = null; MysqlDataSource ds = getMySQLDataSource(); try { con = ds.getConnection(); pst = con.prepareStatement("SELECT VERSION()"); rs = pst.executeQuery(); if (rs.next()) { String version = rs.getString(1); System.out.println(version); } } finally { if (rs != null) { rs.close(); } if (pst != null) { pst.close(); } if (con != null) { con.close(); } } } }
In this example, we connect to the database using a datasource and get the version of MySQL.
fis = new FileInputStream("src/main/Resources/db.properties"); props.load(fis);
The database properties are read from the db.properties
file with the FileInputStream
class.
ds = new MysqlConnectionPoolDataSource(); ds.setURL(props.getProperty("mysql.url")); ds.setUser(props.getProperty("mysql.username")); ds.setPassword(props.getProperty("mysql.password"));
A MysqlConnectionPoolDataSource
is created and the datasource properties are set.
con = ds.getConnection();
A connection object is created from the datasource with the getConnection()
method.
pst = con.prepareStatement("SELECT VERSION()");
An SQL statement is created. The SELECT VERSION()
command returns the version of MySQL.
rs = pst.executeQuery();
The query is executed. It returns a result set.
if (rs.next()) { String version = rs.getString(1); System.out.println(version); }
We get the first value from the result set and print it to the console.
} finally { if (rs != null) { rs.close(); } if (pst != null) { pst.close(); } if (con != null) { con.close(); } }
In the end, the resources are released.
A web application in Tomcat
We create a web application which will retrieve the version of MySQL. The application is deployed on Tomcat.

In our project, we use JSTL and MySQL driver JARs. The JavaServer Pages Standard Tag Library (JSTL) is a collection of useful JSP tags which provide core functionality common to many JSP files.
context.xml
<?xml version="1.0" encoding="UTF-8"?> <Context path="/TomcatDSEx"> <Resource name="jdbc/testdb" auth="Container" type="javax.sql.DataSource" username="testuser" password="test623" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/testdb" maxActive="10" maxIdle="4"/> </Context>
For Tomcat web server, we create a new resource in the context.xml
file. The file is located in the META-INF
directory.
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <resource-ref> <description>DB Connection</description> <res-ref-name>jdbc/testdb</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> </web-app>
Then, in the web.xml
file, we create a reference to the resource. In our application, we will refer to the data source with the jdbc/testdb
logical name.
index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <c:redirect url="/Version"/> </body> </html>
The index.jsp
file redirects to the Version
servlet.
showVersion.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>MySQL version</title> </head> <body> MySQL version: <c:out value="${version}"/> </body> </html>
The showVersion.jsp
is a UI element to display the data retrieved from the database.
MySQL version: <c:out value="${version}"/>
The JSTL's <c:out>
tag is used to output the value of the response.
Version.java
package com.zetcode.version; import com.zetcode.version.service.DBVersionService; import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet(name = "Version", urlPatterns = {"/Version"}) public class Version extends HttpServlet { protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); String page = "/showVersion.jsp"; String version = DBVersionService.getMySQLVersion(); request.setAttribute("version", version); RequestDispatcher disp = getServletContext().getRequestDispatcher(page); disp.forward(request, response); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } @Override public String getServletInfo() { return "Returns version of MySQL"; } }
The Version
servlet calls a service method to get the version of MySQL. The returned value is set as an attribute to the request object.
String page = "/showVersion.jsp";
At the end, the servlet points to the showVersion.jsp
file.
String version = DBVersionService.getMySQLVersion();
A service method is called to get the version of MySQL.
request.setAttribute("version", version);
The version value is set to the request object with the setAttribute()
method.
RequestDispatcher disp = getServletContext().getRequestDispatcher(page); disp.forward(request, response);
We dispatch to the showVersion.jsp
file.
DBVersionService.java
package com.zetcode.version.service; import com.zetcode.version.Version; import com.zetcode.version.util.ServiceLocator; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.logging.Level; import java.util.logging.Logger; import javax.sql.DataSource; public class DBVersionService { public static String getMySQLVersion() { String version = "no version"; DataSource ds = ServiceLocator.getDataSource("java:comp/env/jdbc/testdb"); Connection con = null; try { con = ds.getConnection(); Statement stm = con.createStatement(); ResultSet rs = stm.executeQuery("SELECT VERSION()"); if (rs.next()) { version = rs.getString(1); } } catch (SQLException ex) { Logger.getLogger(Version.class.getName()).log(Level.SEVERE, null, ex); } finally { if (con != null) { try { con.close(); } catch (SQLException ex) { Logger.getLogger(DBVersionService.class.getName()).log(Level.SEVERE, null, ex); } } } return version; } }
DBVersionService
is a service class which contains a method to get the version of MySQL.
DataSource ds = ServiceLocator.getDataSource("java:comp/env/jdbc/testdb");
The datasource is created with a ServiceLocator
class.
con = ds.getConnection(); Statement stm = con.createStatement(); ResultSet rs = stm.executeQuery("SELECT VERSION()"); if (rs.next()) { version = rs.getString(1); }
Here we have the JDBC code to connect to the database and execute an SQL statement.
ServiceLocator.java
package com.zetcode.version.util; import java.util.logging.Level; import java.util.logging.Logger; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; public class ServiceLocator { public static DataSource getDataSource(String jndiName) { Context ctx = null; DataSource ds = null; try { ctx = new InitialContext(); ds = (DataSource) ctx.lookup(jndiName); } catch (NamingException ex) { Logger.getLogger(ServiceLocator.class.getName()).log(Level.SEVERE, null, ex); } return ds; } }
The ServiceLocator
looks up the data source by its given JNDI name and returns it to the caller.
$ curl localhost:8084/TomcatDSEx/Version <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>MySQL version</title> </head> <body> MySQL version: 5.5.49-0ubuntu0.14.04.1 </body> </html>
The application responds with a HTML page containing the version of MySQL.
This was the Data source in Java tutorial. You might be also interested in JDBI tutorial , MyBatis tutorial , SQL query tag tutorial , or MySQL tutorial .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论