ApacheTomcat, J2EE, SQL2000 JDBC installed...now what?

slycat

Diamond Member
Jul 18, 2001
5,656
0
0
ok..on my win2k machine i installed
Apache tomcat, java2 sdk environment, and that MS sql2000 2000 driver for jdbc.

so now, how do i connect to a sql2000 database? what to configure?...any ideas?...links?...
i'm a hopeless noob :(
 

ugh

Platinum Member
Feb 6, 2000
2,563
0
0
Since you've obtained the MSSQL driver for JDBC, there should be some documentation telling you which class to instantiate (usually the DriverManager) and the format of the URL to use to connect to the DB.
 

manly

Lifer
Jan 25, 2000
13,286
4,062
136
Tomcat has a developer's guide up on their web site to help you get started.
 

AmigaMan

Diamond Member
Oct 12, 1999
3,644
1
0
Here is the link to the Tomcat 4.1 JNDI datasource howto section. This will help you in setting up a servlet to access a database. If you have anymore questions, post them. I just went through this recently (setting up a mySQL database) and the knowledge is still fresh in my mind.
 

slycat

Diamond Member
Jul 18, 2001
5,656
0
0
lets say this machine is B and my sql2000 is machine A...so what do i do? All machines are Win2000 server.

MachineA - sql2000 Database
MachineB - apache,tomcat,j2ee, mssql jdbc

so like...wat do i do to B to make it get data from A? shitz..i'm stupid :(
 

AmigaMan

Diamond Member
Oct 12, 1999
3,644
1
0
Really you just need Tomcat installed on your machine. The other stuff is useful if you're doing enterprise development, but since you're asking these kinds of questions, I'll assume you're not ;)
Also, I'm assuming you know at least something about a database and that SQL Server is properly set up on your other machine. If it isn't, then you really need to question why you are doing this.

Anyways, what you'll want to do is create a servlet that responds to an HTTP request from a webpage you've set up. You'll also need to configure your web.xml file and put a resource-ref section in it. Like this:
<resource-ref>
<description>Resource</description>
<res-ref-name>
jdbc/db
</res-ref-name>
<res-type>
javax.sql.DataSource
</res-type>
<res-auth>
Container
</res-auth>
</resource-ref>

In the server.xml for in your tomcat/conf directory, you'll need to configure a Resource:
<Context path="/TestServlet" docBase="C:\projects\test\testservlet" workDir="C:\projects\test\testservlet\work\org\apache\jsp">
<Resource name="jdbc/db" auth="Container" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/db">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>100</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>30</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>10000</value>
</parameter>
<parameter>
<name>username</name>
<value>the_username</value>
</parameter>
<parameter>
<name>password</name>
<value>the_password</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>org.gjt.mm.mysql.Driver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost:3306/test?autoReconnect=true</value>
</parameter>
</ResourceParams>
</Context>

Then in your servlet's init function, you'll want to get access to that database stuff you've been setting up:
public void init(ServletConfig config) throws ServletException
{
context = config.getServletContext();
try{
initCtx = new InitialContext();
envCtx = (Context) initCtx.lookup("java:comp/env");
ds = (DataSource) envCtx.lookup("jdbc/db");
}
catch(Throwable t)
{
t.printStackTrace(System.out);
}
super.init(config);
}

Finally in the method in which you want to call your database, you'll need something like this:

Connection conn;
conn = ds.getConnection();
Statement st = conn.createStatement();
String sql = "select whatever from yourtable";
ResultSet rs = st.executeQuery(sql);
if ( rs.next() )
{
//do whatever you want with the data
}
conn.close();