I know that using JDBC we can create output for queries inside a database, but how can I get the output of the command SHOW PROCESSLIST as an output of a Java program.
In mysql we get it by: SHOW PROCESSLIST
But I was wondering if we can generate the output using Java?
You can use executeQuery for getting SHOW PROCESSLIST ResultSet.
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql", "root", password);
Statement stmt = null;
try {
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SHOW PROCESSLIST");
while (rs.next())
{
System.out.println(rs.getString("Host"));
System.out.println(rs.getString("Id"));
System.out.println(rs.getString("User"));
System.out.println(rs.getString("db"));
System.out.println(rs.getString("Command"));
System.out.println(rs.getString("state"));
System.out.println(rs.getString("info"));
}
Depending on your MySQL version, you can perform a select on
SELECT * FROM information_schema.PROCESSLIST;
and you can do a where between the user, database, and host IP.
For example:
SELECT * FROM information_schema.PROCESSLIST WHERE db ="mycase" AND HOST LIKE "192.168.11.174%"
Related
Here's my query:
select *
from reg
where indexno=?
or tel=?
And here's my code:
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con =DriverManager.getConnection("jdbc:mysql://url","unam","pass");
String query = "select * from reg where indexno= ? or tel=?";
PreparedStatement ps = con.prepareStatement(query);
ps.setString(1, in.getText());
ps.setString(2, tl.getText());
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(query);
Let's take a closer look at what your code is doing.
Connecting to the database:
Connection con =DriverManager.getConnection("jdbc:mysql://url","unam","pass");
Creating the SQL query:
String query = "select * from reg where indexno= ? or tel=?"`;
Creating a prepared statement:
PreparedStatement ps = con.prepareStatement(query);
Setting some bind parameter values:
ps.setString(1, in.getText());
ps.setString(2, tl.getText());
Creating a whole new non-prepared statement (wait, what? Why are we not using the prepared statement we spent some time creating?):
Statement st = con.createStatement();
Using the new non-prepared statement to execute the SQL query.
ResultSet rs = st.executeQuery(query);
As a result of the last two lines, your SQL query is sent straight to the MySQL database. MySQL doesn't understand what the ? marks are for, and hence complains with a syntax error about them.
When handling prepared statements, JDBC drivers will either replace the ? marks with the database's own syntax for bind parameters (unless the database supports ? marks directly, but not all databases do), or put the values directly in the SQL string after suitable escaping of any characters, before they send the SQL to the database. Statements don't support bind parameters, and will just send the SQL string they are given straight to the database.
Your code creates a PreparedStatement and sets two bind parameter values. It seems a shame not to actually use your prepared statement once you've created it. You can get the result set you want out of it by calling ps.executeQuery(). There is no need for the separate Statement you created by calling connection.createStatement().
The fix therefore is to remove the last two lines of the code in your question and add the following line in place of them:
ResultSet rs = ps.executeQuery();
When I execute a query using PreparedStatement.executeQuery() from a Java application, I get a resultSet object with wrong values for one of the fields. When I use run the same query in SQL developer, then I see the values correctly.
For e.g. In my query output,I am able to see the values as below:
ID_TABLE VALUE_ID
-------------------------
1234 111111
1234 222222
5678 333333
5678 444444
But, in java, when I am iterating the result set in while loop, I have printed logs with rs.getString("ID_TABLE") and rs.getString("VALUE_ID") on a single line as
System.out.println(" Table Id= "+rs.getString("ID_TABLE")+" -Value Id= "+rs.getString("VALUE_ID"));
Here, I am getting in logs as Table Id=5678 -Value Id=222222 which is incorrect. I am always getting the same output in sql developer and it is running in a single thread only. Nothing is running in parallel.
I am using Oracle 12c (version-12.1) and jdk 1.7.0_45.
Do you think it's a Java side problem or Oracle side data fetching problem?
Please let me know if you need additional details.
Please find below my method code snippet:
Connection conn = null;
ResultSet res = null;
String strQuery = "SELECT ID_TABLE,VALUE_ID FROM AppTableDetails";
PreparedStatement pStmt = null;
conn = getConnection();
pStmt = conn.prepareStatement(strQuery);
res = pStmt.executeQuery();
while (res.next())
{
System.out.println("Table Id= "+res.getString("ID_TABLE")+" -Value Id= "+res.getString("VALUE_ID"));
}
One more thing, this error is occurring randomly and not every time when I run the process.
I'm working on a dynamic web project and using the PreparedStatement to execute the SQL queries against the DB2 database.
String myQuery = "select id from user where name = ?";
PreparedStatement stmt = connection.prepareStatement(myQuery);
stmt.setString(1, test);
ResultSet resultSet = statement.executeQuery();
How can I receive the full SQL query that is about to be executed on the DB2 server in the console?
If you are familiar with Debugging options in Eclipse. You may try the following:
Set a Breakpoint at ResultSet resultSet = statement.executeQuery();
Right click your application, say Debug As select Java Application (or Whatever applicable in your case i.e. may be SpringBoot App etc.
Perform step that gets you to code mentioned in the Question.
If you check Variables tab in Debug Perspective of Eclipse, you will find variables like myQuery , stmt (according to your code)
Whatever you see as value of stmt would be the full SQL query you need.
Also, if you don't want to keep looking at this variable always you may try Java Logging and Print your Full SQL query in Logs.
I wish to get user info just like provided by
SELECT SYS_CONTEXT ('USERENV', 'SESSION_USER') FROM DUAL;
and
SELECT SYS_CONTEXT ('USERENV', 'OS_USER') FROM DUAL;
inside a JAVA UDF for Oracle 11g without making a JDBC connection and running these queries to query from DUAL.
I tried System.getProperty("user.name") to read the current OS_user through jvm but I think we are not allowed to fetch information outside the database environment.
More generically, problem statement is to fetch information about the user who has logged into database and using that java UDF (where we need to determine these information) ?
I have found solution to above problem by using the "jdbc:default:connection" which is an internal connection maintained by database itself which is always available. Notice I did not do conn.close(); in the end because this is a shared stream which once closed is closed for all database clients.
public static String doSQL() throws SQLException {
String result = new String();
String q1 = "SELECT SYS_CONTEXT('USERENV','SESSION_USER') FROM DUAL";
Connection conn =
DriverManager.getConnection("jdbc:default:connection");
PreparedStatement ps = conn.prepareStatement(
q1
);
ResultSet rs = ps.executeQuery();
while (rs.next())
result = rs.getString(1);
return "my udf says"+result;
}
I would like to make o program that would store data from excel files in databases. I have plenty of databases so in my program i have to choose in which one I will store the data.
I have made the code to be able to connect mysql with my program and to show the available databases. What I would like to do now is to say in which database i would store the data.
To be more specific I would like the user first of all to see tha available databases in his client and afterwards he would have the chance to say in which database the data would be stored.
Could anyone help me how I would do this?
The code to see all the available databases is the below:
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection) DriverManager.getConnection(
"jdbc:mysql://localhost:3306/", "root", "root");
DatabaseMetaData meta = (DatabaseMetaData) con.getMetaData();
ResultSet res = meta.getCatalogs();
System.out.println("List of the databases: ");
while (res.next()){
System.out.println (" " +res.getString(1));
}
Thank you in advance!
I hope this SO link should help you . You can get all the data from the connection object
First do a create a simple connection
Connection con = (Connection) DriverManager.getConnection( "jdbc:mysql://localhost:3306/", "root", "root");
Look for an example here
see how he uses ResultSet Class to access the result.
Now,
retrieve information from information_schema.SCHEMATA so you can query like
SELECT schema_name FROM information_schema.SCHEMATA S;
to get all the schemas
Next after getting choice from user(maybe from console) you can set database according to uservalue using Connection#setCatalog()
If you want table information use this query
SELECT * FROM information_schema.TABLES T;
It would list all the tables in all schemas