Java Mysql query problem
Hy .
I am executing a query from a Jframe and the result are displayed in a JTable. The problem is the following: in the db table I have an IP address stored as an Integer , and in order to see the ip "correctly " x.x.x.x , I have to write in the select like this SELECT INET_NTOA(IP_SRC)......... ;
in Mysql the result are displayed ,also in Java when in the query is without INET_NTOA() there are data displayed , but when in JAVA the query has INET_NTOA () no data is being displayed!!
what can I do ?pls Help
[538 byte] By [
Vladislava] at [2007-11-27 1:06:09]

Is the function inet_ntoa() available only in mysql and not in java? is there a similar function?
So you are running the same SELECT statement through... lets say... any kind of third party SQL tool like SQL Station for instance...and you get a result, however when running the same SELECT statement through a JDBC call you retrieve an empty resultset?
yes, when running in a Sql tool from Select inet_ntoa(ip_src) I get for example 12.1.2.3 and when I run through JDBC no data is displayed !!
When you use the inet_ntoa() function in MySQL are you using the getString() method of ResultSet in your Java code?And are you aliasing that to some column name (using the "as" keyword) if you're not using column numbers?
tsitha at 2007-7-11 23:41:17 >

Perhaps you can post some code where the problem is occuring?Also, try placing a logging statement to show the exact value of ip_src before you execute the query.Is it what you expect?
hree is the first class , where the table is created
import javax.swing.table.AbstractTableModel;
import java.sql.*;
import java.util.ArrayList;
public class IPHDR extends AbstractTableModel {
private int colnum=4;
private int rownum;
private String[] colNames={
"sid","cid","ip_src","ip_dst"
};
private ArrayList<String[]> ResultSets;
public IPHDR(ResultSet rs) {
ResultSets=new ArrayList<String[]>();
try{
while(rs.next()){
String[] row={
rs.getString("sid"),rs.getString("cid"), rs.getString("ip_src"),rs.getString("ip_dst")
};
ResultSets.add(row);
}
}
catch(Exception e){
System.out.println("Exception in IPHDR");
}
}
public Object getValueAt(int rowindex, int columnindex) {
String[] row=ResultSets.get(rowindex);
return row[columnindex];
}
public int getRowCount() {
return ResultSets.size();
}
public int getColumnCount() {
return colnum;
}
public String getColumnName(int param) {
return colNames[param];
}
}
and here is the code from the main class
public ResultSet getResultFromIPHDR() {
ResultSet rsi=null;
try{
rsi=stmt.executeQuery("Select sid,cid,ip_src,ip_dst from iphdr");
} catch(SQLException e){}
return rsi;
}
I know the the exact value of the ip before executing the query , I want that the value 1370817797 to be displaey as an IP , X.X.X.X
> hree is the first class , where the table is created
[snip]
first off, please use code tags (hit that "code" button above the message text area) second off, you're just catching Exceptions and ignoring them. I would suggest at least calling printStackTrace in your catch blocks so you stand a chance of seeing what might be going wrong.
Good Luck
Lee
tsitha at 2007-7-11 23:41:17 >

> > hree is the first class , where the table is
> created
> [snip]
>
> first off, please use code tags (hit that "code"
> button above the message text area) second off,
> you're just catching Exceptions and ignoring them. I
> would suggest at least calling printStackTrace in
> your catch blocks so you stand a chance of seeing
> what might be going wrong.
>
> Good Luck
>
> Lee
thanks for the tip, I will try what you have just said
Tsith, like you've said , If I had any errors shouldn't that be displayed when compiling or running ?<calling printStackTrace in your catch blocks> can you be more specific thanks
I was looking at the following code that you posted:try{
rsi=stmt.executeQuery("Select sid,cid,ip_src,ip_dst from iphdr");
} catch(SQLException e){}
If anything gets thrown out of here you will never see what goes wrong, as you catch the exception and ignore it. Change that to something like try{
rsi=stmt.executeQuery("Select sid,cid,ip_src,ip_dst from iphdr");
} catch(SQLException e){
e.printStackTrace();
}
You do something like this in your code but rather than print the stack trace you just say, in effect, "something went wrong". That's a little better than ignoring it but you lose the debugging information contained in the Exception's stack trace.
tsitha at 2007-7-11 23:41:17 >

all I get is"Exception in IPHDR"after that I've e.printStackTrace() in the class that constructs the table (which also I've published the code )can you help me further ?
You know earlier where I said something about basically printing out "something went wrong" and how that's not very helpful? There you go.
If you're seeing "Exceptin in IPHDR" then that means you told it to print that. The only place I see where you tell the computer to print that is in this code:try{
while(rs.next()){
String[] row={
rs.getString("sid"),rs.getString("cid"), rs.getString("ip_src"),
rs.getString("ip_dst") };
ResultSets.add(row);
}
}
catch(Exception e){
System.out.println("Exception in IPHDR");
}
I suggested that you put a call to e.printStackTrace() after you print out that something has gone wrong, so you might get a clue as to what, exactly, has gone wrong.
tsitha at 2007-7-11 23:41:17 >

I've made the changes you suggested
public IPHDR(ResultSet rs) {
ResultSets=new ArrayList<String[]>();
try{
while(rs.next()){
String[] row={
rs.getString("sid"),rs.getString("cid"), rs.getString("ip_src"),rs.getString("ip_dst")
};
ResultSets.add(row);
}
}
catch(Exception e){
e.printStackTrace();
}
}
and the same error is displayed , only "exception IN IPHDR ", nothing more;
why it doesn't displays the exact error ?
If you changed it the way you indicated to not print out that String, and it's still printing out that String, then you've not compiled the code since changing the source.
tsitha at 2007-7-21 19:59:23 >

sry ,my mistake; here is the output(error)
java.sql.SQLException: Column 'ip_src' not found.
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:910)
at com.mysql.jdbc.ResultSet.findColumn(ResultSet.java:970)
at com.mysql.jdbc.ResultSet.getString(ResultSet.java:5613)
at vladpackage.IPHDR.<init>(IPHDR.java:30)
at vladpackage.Snort.initComponents(Snort.java:128)
at vladpackage.Snort.<init>(Snort.java:40)
at vladpackage.Snort$8.run(Snort.java:379)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
and I am confused ,the column exists ,the name is correct ,and why is not being seen ?
when is inet_ntoa(ip_src) is not seen ,and when is ip_src is recognized
WEll, the DB doesn't think that column name exists. I can't see where you call the IPHDR method, but the ResultSet you pass it must not have the ip_src column.
tsitha at 2007-7-21 19:59:23 >

If your SQL is "select inet_ntoa(ip_src), etc..." then the first column in the
result set is not called "ip_src". You can give it a name with "as":
"select inet_ntoa(ip_src) as my_col, etc..."
or you can use a ResultSet method that take a column index:
rs.getString(1) //instead of rs.getString("ip_src")
Finally it works ;the correct one is Select sid,cid,inet_ntoa(ip_src)as ip_src,ip_dst from iphdr;Thanks a lot guys ; you all are the best