KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > code_example2


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
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 // Here is a code example to configure the JDBCAppender without a configuration-file
18

19 import org.apache.log4j.*;
20 import java.sql.*;
21 import java.lang.*;
22 import java.util.*;
23
24 public class Log4JTest
25 {
26     // Create a category instance for this class
27
static Category cat = Category.getInstance(Log4JTest.class.getName());
28
29     public static void main(String JavaDoc[] args)
30     {
31         // A JDBCIDHandler
32
MyIDHandler idhandler = new MyIDHandler();
33
34         // Ensure to have all necessary drivers installed !
35
try
36         {
37             Driver d = (Driver)(Class.forName("oracle.jdbc.driver.OracleDriver").newInstance());
38             DriverManager.registerDriver(d);
39         }
40         catch(Exception JavaDoc e){}
41
42         // Set the priority which messages have to be logged
43
cat.setPriority(Priority.DEBUG);
44
45         // Create a new instance of JDBCAppender
46
JDBCAppender ja = new JDBCAppender();
47
48         // Set options with method setOption()
49
ja.setOption(JDBCAppender.CONNECTOR_OPTION, "MyConnectionHandler");
50         ja.setOption(JDBCAppender.URL_OPTION, "jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(COMMUNITY=tcp.world)(PROTOCOL=TCP)(Host=LENZI)(Port=1521))(ADDRESS=(COMMUNITY=tcp.world)(PROTOCOL=TCP)(Host=LENZI)(Port=1526)))(CONNECT_DATA=(SID=LENZI)))");
51         ja.setOption(JDBCAppender.USERNAME_OPTION, "mex_pr_dev60");
52         ja.setOption(JDBCAppender.PASSWORD_OPTION, "mex_pr_dev60");
53
54         ja.setOption(JDBCAppender.TABLE_OPTION, "logtest");
55
56         // There are two ways to setup the column-descriptions :
57
// 1. Use the the method setOption(JDBCAppender.COLUMNS_OPTION, column-description)
58
//ja.setOption(JDBCAppender.COLUMNS_OPTION, "id_seq~EMPTY id~ID~MyIDHandler msg~MSG created_on~TIMESTAMP created_by~STATIC~:-) Thomas Fenner (t.fenner@klopotek.de)");
59

60         // 2. Use the better way of coding with method setLogType(String columnname, int LogType.xxx, Object xxx)
61
ja.setLogType("id_seq", LogType.EMPTY, "");
62         ja.setLogType("id", LogType.ID, idhandler);
63         ja.setLogType("msg", LogType.MSG, "");
64         ja.setLogType("created_on", LogType.TIMESTAMP, "");
65         ja.setLogType("created_by", LogType.STATIC, "FEN");
66
67         // If you just want to perform a static sql-statement, forget about the table- and columns-options,
68
// and use this one :
69
//ja.setOption(JDBCAppender.SQL_OPTION, "INSERT INTO LOGTEST (id, msg, created_on, created_by) VALUES (1, @MSG@, sysdate, 'me')");
70

71         // other options
72
//ja.setOption(JDBCAppender.BUFFER_OPTION, "1");
73
//ja.setOption(JDBCAppender.COMMIT_OPTION, "Y");
74

75         // Define a layout
76
//ja.setLayout(new PatternLayout("%m"));
77

78         // Add the appender to a category
79
cat.addAppender(ja);
80
81         // These messages with Priority >= setted priority will be logged to the database.
82
cat.debug("debug");
83         cat.info("info");
84         cat.error("error");
85         cat.fatal("fatal");
86     }
87 }
88
89 // Implement a sample JDBCConnectionHandler
90
class MyConnectionHandler implements JDBCConnectionHandler
91 {
92     Connection con = null;
93    //Default connection
94
String JavaDoc url = "jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(COMMUNITY=tcp.world)(PROTOCOL=TCP)(Host=LENZI)(Port=1521))(ADDRESS=(COMMUNITY=tcp.world)(PROTOCOL=TCP)(Host=LENZI)(Port=1526)))(CONNECT_DATA=(SID=LENZI)))";
95    String JavaDoc username = "mex_pr_dev60";
96    String JavaDoc password = "mex_pr_dev60";
97
98    public Connection getConnection()
99    {
100     return getConnection(url, username, password);
101    }
102
103    public Connection getConnection(String JavaDoc _url, String JavaDoc _username, String JavaDoc _password)
104    {
105     try
106       {
107         if(con != null && !con.isClosed()) con.close();
108             con = DriverManager.getConnection(_url, _username, _password);
109             con.setAutoCommit(false);
110       }
111       catch(Exception JavaDoc e){}
112
113     return con;
114    }
115 }
116
117
118 // Implement a sample JDBCIDHandler
119
class MyIDHandler implements JDBCIDHandler
120 {
121     private static long id = 0;
122
123     public synchronized Object JavaDoc getID()
124    {
125         return new Long JavaDoc(++id);
126    }
127 }
128
129
Popular Tags