Thursday, May 22, 2008

How to log sql statement stored in PreparedStatement



Take the following program.

import java.net.URL;
import java.sql.*;

class JDBCapp {
static MyConnection theConn;

public static void main (String args[]) {
new JDBCapp().doit();
}

public void doit() {
theConn = new MyConnection();
theConn.connect("EAS Demo DB V3", "dba", "sql");

PreparedStatement prepstmt;
try {
prepstmt = theConn.dbConn.prepareStatement
("SELECT emp_id FROM employee" );
prepstmt.execute();
prepstmt.close();
}
catch (Exception e) { e.printStackTrace(); }
theConn.disconnect();
}
}


class MyConnection {
Connection dbConn = null;
void connect(String db, String user, String passw) {
try {
Driver d =
(Driver)Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
String URL = "jdbc:odbc:" + db;
dbConn = DriverManager.getConnection(URL, user, passw);
java.io.PrintWriter w =
new java.io.PrintWriter
(new java.io.OutputStreamWriter(System.out));
DriverManager.setLogWriter(w);

}
catch (Exception e) {
e.printStackTrace();
}
}

void disconnect() {
try {
dbConn.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}


If we use BasicDataSource to getConnection instead of DriverManager,
use BasicDataSource .setLogWriter.

In addition, to log these sql strings to log4j, we will redirect these messages from printwriter to log4j. Use the following code to do this.

http://www.cenqua.com/clover/eg/jboss/report/org/jboss/logging/util/LoggerWriter.html

Wednesday, May 21, 2008

Show lock on table in oracle and unlock them

Show lock
=========
select O.object_name,
O.object_type,
S.sid,
S.serial#,
L.oracle_username,
L.os_user_name,
L.locked_mode
from dba_objects O,
v$locked_object L,
v$session S
where O.object_id = L.object_id
and S.sid = L.session_id
=============

Unlock
ALTER SYSTEM KILL SESSION 'sid, serialid';

Monday, May 5, 2008

Angry with entrecard

I am very angry with entrecard system. They considers my site was spam blog.
They said that because entrecard is their system and they want to do what they want.
It is not acceptable. I will make my own system which can be better than entrecard. If we have more competition, i think they will know where they are in internet.
They delete my site but all their ads is still in my site. What do you think about them? They are all bullshit.

Google