Domanda

I'm working on java JDK7 and Microsoft Access 2007.Basically I want to get the minimum value from the all the columns of row1. But the following code doesn't work.

import java.io.*;
import java.util.*;
import java.net.*;
import java.sql.*;

public class server 
{
public void check() 
{
    int min = 100, row = 0, index, i = 2;
    try {
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection cn = DriverManager.getConnection("jdbc:odbc:DSN2");
            Statement st = cn.createStatement();
            ResultSet rs = st.executeQuery("select *from Table1");
            rs.next();
            System.out.println(rs.getInt(1) + "\t" + rs.getInt(2) + "\t" + rs.getInt(3) + "\t" + rs.getInt(4) + "\t" + rs.getInt(5) + "\t" + rs.getInt(6));
            for (i = 2; i < 7; i++)
             {
                System.out.println("hello");
                if (rs.getInt(i) < min) {
                    index = i;
                    min = rs.getInt(i);
                                        }
            }
        } catch (Exception e) {
            e.getMessage();
                              }
        switch (i) {
            case 2:
                ioConnect();
                break;
            case 3:
                break;
            case 4:
                ioConnect();
                break;
            case 5:
                ioConnect();
                break;
            case 6:
                ioConnect();
                break;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public void ioConnect() {
    try {
        ServerSocket ss = new ServerSocket(2000);

        Socket so = ss.accept();
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("enter the message");

        String str = br.readLine();
        PrintStream ps = new PrintStream(so.getOutputStream());
        ps.println(str);

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

class serverm {

public static void main(String s[]) {
    servern obj = new servern();
    obj.check();

}
}

So here I get the output the first row each column values that's fine, but when the control enters the for loop the println statement prints the hello only once and the cursor blinks. This indicates the the program has not ended correctly.

È stato utile?

Soluzione

Your first problem is that you don't know what your problem is...

To solve this problem, and get to solve the real issue, instead of this

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

Use

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

And please paste the full stack trace this produces...

EDIT As OP stated in a comment:

there is no data found exception thrown

This means that there is no data to be read. Which is a known case for MS Access:

This typically occurs when you try to read the value of a column multiple times.

So you should only read every value once! To fix the imediate issue, comment the System.out.println() line here

...
rs.next();
// comment this: 
// System.out.println(rs.getInt(1) + "\t" + rs.getInt(2) + "\t" + rs.getInt(3) + "\t" + rs.getInt(4) + "\t" + rs.getInt(5) + "\t" + rs.getInt(6));
for (i = 2; i < 7; i++)
...

And be careful, as you call getInt() for the same index two times in the loop, which has to be corrected:

for (i = 2; i < 7; i++)
{
    System.out.println("hello");
    if (rs.getInt(i) < min) { // getInt()
        index = i;
        min = rs.getInt(i); // second getInt() call for same index --- throws exception
    }
}

Correct:

for (i = 2; i < 7; i++)
{
    System.out.println("hello");
    int value=rs.getInt(i); // getInt() call, put into local variable
    if (value < min) {
        index = i;
        min = value; //just use local variable - OK
    }
}

Altri suggerimenti

It is a small mistake that you have done. Irrespective of whether the result has record or not, you are calling rs.getInt(1)... . Use classical JDBC way of testing whether result set has records or not.

while(rs.next()){
   rs.getInt(1);
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top