I'm doing a website in JSP. I'm using Tomcat 6 and have gotten the site talking to the backend file. However, when I go to add in a database connection, I have problems. This is my first time trying to connect to a SQL Server 2008 database. In the past I have used Oracle.
So I have the Context.xml file in the META-INF folder. This is an example of the file I had before with the Oracle information:
Expand|Select|Wrap|Line Numbers
- <?xml version="1.0" encoding="UTF-8"?>
- <Context
- override="true"
- reloadable="true">
- <Resource
- auth="Container"
- name="jdbc/myDatabase"
- type="javax.sql.DataSource"
- driverClassName="oracle.jdbc.OracleDriver"
- username="lita"
- password="123"
- maxIdle="1"
- maxWait="5000"
- maxActive="3"
- removeAbandoned="true"
- url="jdbc:oracle:thin:@localhost:1521:XE"
- removeAbandonedTimeout="0"
- factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"/>
- </Context>
For SQL Server 2008:
I know the driverClassName will be "com.microsoft.sqlserver.jdbc.SQLServerDriver"
and that the url will be something like "jdbc:sqlserver://localhost;DatabaseName=Pie"
But I'm not sure exactly what this file will look like. I need help getting the right information set.
So:
- Database name: Pie
- Typical connection string I would use in C#: connectionString="Provider=SQLOLEDB;Data Source=XP_LAPTOP\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=Pie"
- I just use Windows Authentication to access SQL Server 2008 so would I set username and password to be my Windows login or just leave as blank?
Also: name="jdbc/myDatabase" - can this be any name so long as it matches up in the code?
Expand|Select|Wrap|Line Numbers
- private Connection con;
- Context init = new InitialContext();
- Context ctx = (Context) init.lookup("java:comp/env");
- DataSource ds = (DataSource) ctx.lookup("jdbc/myDatabase");
- con = ds.getConnection();
Any help would be greatly appreciated! Thanks :)