UPDATE: I solved this problem exactly the same way friends suggested. But I kind of missed the fact that I have to follow mysql’s protocol in order to get the result. In mysql there is always an initial handshaking packets doesn’t have any data field so you don’t get any data with getInputStream. The only way is to create a mysql connection in your own server and send the result to your client. It works very well. Thank you everybody
I want to read mysql connection info with java. For example I made a server socket with java on port 4444. Then I made a jdbc connection on port 4444. But it doesn’t work. I don’t know why and even I don’t know if it is possible or not.
This is the sever code:
int portNr = 4444;
// Create a ServerSocket object to watch that port for clients
ServerSocket ss;
try {
ss = new ServerSocket(portNr);
System.out.println("starting...");
// Here we loop indefinitely, just waiting for clients to connect
while (true) {
// accept() does not return until a client requests a connection
// Now that a client has arrived, create an instance of our
// special
// thread subclass to respond to it.
Socket clientSocket = null;
try {
clientSocket = ss.accept();
System.err.println("heheh"+clientSocket);
// PrintWriter out = new
// PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
String inputLine = in.readLine();
System.err.println(inputLine)....;</i>
It prints “hehehe”; but it doesn’t print input line.
And this is my connection code:
public class messagetest
{
// The JDBC Connector Class.
private static final String dbClassName = "com.mysql.jdbc.Driver";
private static final String CONNECTION =
"jdbc:mysql://127.0.0.1:4444/shadi";
public static void main(String[] args) throws
Exception,SQLException
{
System.out.println(dbClassName);
Class.forName(dbClassName);
// Properties for user and password. Here the user and password are both 'paulr'
Properties p = new Properties();
p.put("user","--------");
p.put("password","-------");
System.err.println("after p");
Connection c = DriverManager.getConnection(CONNECTION,p);
System.err.println("client:");
It doesn’t print “client:”
Thanks for your help.
You cannot bind your own Server on the same port as the DB-Server instance.
If you really want to “monitor” it like that, use a little “man in the middle”: Set the DB to another port, say 4445. Then inside your monitor listen on 4444, that clients will connect to and make your own connection to 4445. Then forward input and output between the two.
EDIT:
OK, we cleared that up. Forget about the first part of my answer.
Your server runs on 4444 and your db on 3306.
So in your monitor, you’ll need two connections: One Monitor-Client and one Monitor-DB.
You’ll have to have two threads reading on each of them and writing what it has read to the other.
And I don’t know if you will be happy using readLine. For checking out what is being sent, I recommend reading bytes …