KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > bluecubs > xinco > core > server > XincoDBManager


1 /**
2  *Copyright 2004 blueCubs.com
3  *
4  *Licensed under the Apache License, Version 2.0 (the "License");
5  *you may not use this file except in compliance with the License.
6  *You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *Unless required by applicable law or agreed to in writing, software
11  *distributed under the License is distributed on an "AS IS" BASIS,
12  *WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *See the License for the specific language governing permissions and
14  *limitations under the License.
15  *
16  *************************************************************
17  * This project supports the blueCubs vision of giving back
18  * to the community in exchange for free software!
19  * More information on: http://www.bluecubs.org
20  *************************************************************
21  *
22  * Name: XincoDBManager
23  *
24  * Description: server-side database manager
25  *
26  * Original Author: Alexander Manes
27  * Date: 2004
28  *
29  * Modifications:
30  *
31  * Who? When? What?
32  * - - -
33  *
34  *************************************************************
35  */

36
37 package com.bluecubs.xinco.core.server;
38
39 import java.sql.*;
40 import javax.sql.DataSource JavaDoc;
41 import javax.naming.InitialContext JavaDoc;
42 import com.bluecubs.xinco.conf.XincoConfigSingletonServer;
43
44 public class XincoDBManager {
45     
46     public Connection con;
47     public XincoConfigSingletonServer config;
48     public static int count = 0;
49     
50     public XincoDBManager() throws Exception JavaDoc {
51         
52         //load compiled configuartion
53
config = XincoConfigSingletonServer.getInstance();
54         DataSource JavaDoc datasource = (DataSource JavaDoc)(new InitialContext JavaDoc()).lookup(config.JNDIDB);
55         con = datasource.getConnection();
56         con.setAutoCommit(false);
57         count++;
58     }
59
60     public int getNewID(String JavaDoc attrTN) throws Exception JavaDoc {
61
62             int newID = 0;
63             Statement stmt;
64             
65             stmt = con.createStatement();
66             ResultSet rs = stmt.executeQuery("SELECT * FROM xinco_id WHERE tablename='" + attrTN + "'");
67             while (rs.next()) {
68                 newID = rs.getInt("last_id") + 1;
69             }
70             stmt.close();
71
72             stmt = con.createStatement();
73             stmt.executeUpdate("UPDATE xinco_id SET last_id=last_id+1 WHERE tablename='" + attrTN + "'");
74             stmt.close();
75             
76             return newID;
77
78     }
79     
80     protected void finalize() throws Throwable JavaDoc {
81         try {
82             count--;
83             con.close();
84         } finally {
85             if (!con.isClosed()) {
86                 count++;
87             }
88             super.finalize();
89         }
90     }
91     
92 }
93
Popular Tags