Is there a method to get the Total JDBC connections? - java

I have a jabber bot that connects to a database to gather data and report it. In case a connection goes down it is re-established and DB connection is re-initiated. Is there a method that I can call to list all the JDBC open and closed connections?

If your database is Oracle, you can query the database sessions using the v$session view. For example with this query:
column sid format 9999
column serial# format 9999999
column username format a8
column machine format a15
column osuser format a6
column program format a12
SELECT sid, serial#, username, machine, osuser, status, program, TO_CHAR(logon_time, 'MM-DD HH24:MI') logon_time
FROM v$session
WHERE username IS NOT NULL AND machine IS NOT NULL
ORDER BY logon_time;
The rows starting with column are not significant if you run the query from Java. They are formatting the result table if you test the query using Sql*Plus before incorporating it into Java.

Related

Counts return by sql query on sql developer is different then returned in the application

I am writing queries for a dashboard purpose. I am taking count from a table A based on the created_date column. My query is below
select 'New requests today' as Details,count(id) CNT
from A
where trunc(created_date)=trunc(systimestamp at TIME ZONE 'US/Central')
When executed on sql developer I get count as 2 which is correct but as soon as the application runs the query the count is 3.
Below are the created_date of the 3 rows (all in CST)
11-OCT-19 10.27.14.634000000 AM
11-OCT-19 10.04.11.247000000 AM
10-OCT-19 08.00.29.443000000 PM
So technically the count is 2 but I am assuming that somehow from application it's reading the third row created_date as 11-OCT-19 01.00.29.443000000 AM GMT
Does anybody have an idea why it's happening like that and what could be the resolution?

Sqlserver: what are the differences between execute sql with jdbc driver and execute with sql client

I have a table named "T_ROLE", it has just one column named "NAME" which type is nvarchar(255), the sqlserver Collation is "SQL_Latin1_General_CP1_CI_AS"(en_US), now i want to insert japanese character, so i know that i need do sql like this:
INSERT INTO T_ROLE(NAME) VALUES(N'japaneseString')
this can be successful.
if i do sql:
INSERT INTO T_ROLE(NAME) VALUES('japaneseString')
which without N prefix, it will saved as '?', i can under these behavior.
But when i use sqlserver jdbc driver to do insert operation like this:
String sql = "INSERT INTO T_ROLE (NAME) VALUES(?)";
stmt.setString(1, "");
stmt.execute(sql);
notice: i don't use stmt.setNString() method, but it can be saved successful, why?
See this blog: https://blogs.msdn.microsoft.com/sqlcat/2010/04/05/character-data-type-conversion-when-using-sql-server-jdbc-drivers/
It turns out that the JDBC driver sends character data including varchar() as nvarchar() by default. The reason is to minimize client side conversion from Java’s native string type, which is Unicode.
So how do you force the JDBC driver not to behave this way? There is a connection string property, named sendStringParametersAsUnicode. By default it’s true.
One would ask what if I want to pass both varchar and nvarchar parameters at the same time? Well, even with the connection property set false, you can explicitly specify nvarchar type like this:
pStmt.setObject(2,Id,Types.NVARCHAR); //Java app code
Simple Google search for sql server jdbc nvarchar found this answer.

Get All tables allowed to user with jdbc

I'm connecting to a database using jdbc, getting list of all schemas and tables from database (I assume that some databases may return at this point only tables which current user can query, but some of databases return full list of tables) and when user try to query some tables he get "insufficient privileges" error.
Is there a way to get only tables, user can query using only jdbc capabilities? Without writing special query to database.
Now I'm looking at
DatabaseMetaData dbMeta = connection.getMetaData()
dbMeta.getTablePrivileges(null, null, null);
But from result of this query it's not so clear which exactly tables can user query.
Currently I'm working with SAP HANA database, but in general it may be any database, so I'm looking for some common approach.
Please look at
http://docs.oracle.com/javase/7/docs/api/java/sql/DatabaseMetaData.html#getTablePrivileges%28java.lang.String,%20java.lang.String,%20java.lang.String%29
You have to get the each row from the ResultSet and query of column name TABLE_NAME which contains the table name and PRIVILEGE which contains the access of each table.

mysql jdbc apply exceeding username max length (16 char)

I have a system that mostly written in java. This system has a simple MySQL user permission administrator. But now we getting started to work with more users and the length for the username is now over 16 chars. I modified all username length property in all mysql system tables. In Webmin and PhPMyAdmin modifying the password and table <=> user relations works correctly. But in this java program all operation witch associated with long usernames will be stopped with throwned SQLException with message: String ' ...long username...' is too long for user name (should be no longer than 16).
So looks like the problem is in jdbc. How can i force to apply long usernames in SQL operations?
(com.mysql.jdbc (5.1.24))
Query String:
st.execute("CREATE USER '"+user+"'#'localhost' IDENTIFIED BY 'nothing';");
st.execute("SET PASSWORD FOR '"+user+"'#'localhost' = PASSWORD('"+passwd+"');");
Stack Trace:
java.sql.SQLException: String '...long username...' is too long for user name (should be no longer than 16)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.ja$
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2503)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2664)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2809)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2758)
at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:894)
at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:732)
You can't change the MySQL username length without recompiling the MySQL source. See this note in the 5.1 documentation. This is also the case with MySQL 5.7.
Warning
The limit on MySQL user name length is hard-coded in the MySQL servers
and clients, and trying to circumvent it by modifying the definitions
of the tables in the mysql database does not work.
I found a better solution:
Use
"INSERT mysql.user (Host,user,Password) VALUES 'localhost','"+user+"',PASSWORD('"+passwd+"');");
instread of
SET PASSWORD FOR '"+user+"'#'localhost' = PASSWORD('"+passwd+"');
The MySQL will not know, that you modify the password or create user, just run the statement.

java.sql.PreparedStatement.setString Function getting Varchars confused with doubles

I have a servlet/tomcat server written in java.
I have a mysql class that I have written, and I have been using the functions in it to insert prepared statements into a mysql database using jdbc.
The function I call uses java.sql.PreparedStatement.setString in order to set the paramaters of the prepared statement. This has been working perfectly for thousands of different inputs for months on end without issue.
Recently however, when trying to use the function to insert an ip address into a VARCHAR type mysql column I am getting an exception thrown as follows:
com.mysql.jdbc.MysqlDataTruncation: Data truncation: Truncated incorrect DOUBLE value: '10.1.1.101'
This is bizarre, There is no notion of a DOUBLE anywhere in my code, and a "Show Columns" on the mysql table ensures that the data type is in fact a VARCHAR. I have had my colleagues look at this as well to double check that I wasn't missing something simple. However we are all stumped.
My only theory is that the JDBC driver or the SetText function is taking a liberty and assuming a DOUBLE data type because the first part of the ip address is in the form of XX.XX
Any help would be great, please don't tell me to do obvious stuff like check my column data types etc. I have spent a lot of time double and tripple checking everything.
The problem is not with JDBC Driver. The problem lies with MySQL. Here is what i get on MySQL commadline:
mysql> INSERT INTO route_table (SYSTEM, IP, PORT) VALUES ("192.168.1.24:8080","192.168.1.24","8080") ON DUPLICATE KEY UPDATE IP=values(IP) AND PORT=values(PORT);
Query OK, 1 row affected (0.04 sec)
mysql> INSERT INTO route_table (SYSTEM, IP, PORT) VALUES ("192.168.1.24:8080","192.168.1.24","8080") ON DUPLICATE KEY UPDATE IP=values(IP) AND PORT=values(PORT);
ERROR 1292 (22007): Truncated incorrect DOUBLE value: '192.168.1.24'
mysql>
The problem is , the syntax of INSERT INTO .... ON DUPLICATE KEY UPDATE used by you is incorrect. You have used a keyword AND instead of using a ,(comma). So the query should be:
mysql> INSERT INTO route_table (SYSTEM, IP, PORT) VALUES ("192.168.1.24:8080","192.168.1.24","8080") ON DUPLICATE KEY UPDATE IP=values(IP), PORT=values(PORT);
Query OK, 2 rows affected (0.00 sec)

Categories