Last active
March 10, 2019 12:21
-
-
Save ezhov-da/67384fcde64a16dd79e0fd0fbfd820ea to your computer and use it in GitHub Desktop.
java tomcat mssql настройка подключения
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
==> https://tomcat.apache.org/tomcat-7.0-doc/jndi-resources-howto.html | |
Итак, для того, чтоб настроить подключения Tomcat для MSSQL, необходимо: | |
1. Поместить настройки подключения в файл : %CATALINA_HOME%\conf\context.xml | |
<Resource name="jdbc/mssql-otzprod1" | |
auth="Container" | |
type="javax.sql.DataSource" | |
maxActive="100" | |
maxIdle="30" | |
maxWait="10000" | |
username="" | |
password="" | |
driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver" | |
url="jdbc:sqlserver://prod1;integratedSecurity=true;"/> | |
2. Кладем драйвер MSSQL в папку: %CATALINA_HOME%\lib | |
3. Прописываем настройку в web.xml приложения: | |
<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> | |
4. Обращаемся к подключению: | |
public class Test | |
{ | |
private static final Logger LOG = Logger.getLogger(Test.class.getName()); | |
public void test() | |
{ | |
try | |
{ | |
InitialContext cxt = new InitialContext(); | |
if (cxt == null) | |
{ | |
throw new Exception("Uh oh -- no context!"); | |
} | |
DataSource ds = (DataSource) cxt.lookup("java:comp/env/jdbc/mssql-otzprod1"); | |
try (Connection connection = ds.getConnection();) | |
{ | |
try (PreparedStatement statement = connection.prepareStatement(" select id from OTZ.dbo.T_E_logErrors");) | |
{ | |
try (ResultSet resultSet = statement.executeQuery();) | |
{ | |
while (resultSet.next()) | |
{ | |
System.out.println(resultSet.getInt(1)); | |
} | |
} | |
} | |
} | |
if (ds == null) | |
{ | |
throw new Exception("Data source not found!"); | |
} | |
} catch (Exception ex) | |
{ | |
ex.printStackTrace(); | |
} | |
} | |
} | |
ТАДАМ! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment