Difference between revisions of "Connecting to a Database"

From CCIL
Jump to: navigation, search
m
m
 
Line 13: Line 13:
  
 
<pre>
 
<pre>
 +
...
 
Connection cnn = db.getConnection();
 
Connection cnn = db.getConnection();
 
PreparedStatement st = (PreparedStatement) cnn
 
PreparedStatement st = (PreparedStatement) cnn
Line 18: Line 19:
 
st.setString(1, name);
 
st.setString(1, name);
 
ResultSet rs = st.executeQuery();
 
ResultSet rs = st.executeQuery();
 +
...
 
</pre>
 
</pre>
 +
 +
=== Reading the connection properties from the context ===
 +
 +
First thing, you need a reference to the ''contexts'' service.
 +
 +
TBA

Latest revision as of 09:56, 13 June 2016

Connection to various databases goes trough DatabaseConnectionManager class. In order to use it, you should add the following dependency to your project:

<dependency>
	<groupId>net.ccil</groupId>
	<artifactId>ccil-common-sql</artifactId>
	<version>${ccil.version}</version>
</dependency>

Usage

...
Connection cnn = db.getConnection();
PreparedStatement st = (PreparedStatement) cnn
       .prepareStatement("SELECT * FROM collections WHERE context = ?");
st.setString(1, name);
ResultSet rs = st.executeQuery();
...

Reading the connection properties from the context

First thing, you need a reference to the contexts service.

TBA