简体   繁体   中英

mssql jdbc connection for .exe not working

So I have a situation where I compile the code in IDE and it works, when I run it as .jar, it works as expected but when I create a .exe installer and install it on the same system the application stops before try of className in below code

class GenerateImage {

private ResultSet rs;
private List<Row> rowData;
private Row r2;
Connection con;
private static final Shape circle = new Ellipse2D.Double(-3, -3, 6, 6);

public GenerateImage() throws IOException, SQLException, ParseException {
    JOptionPane.showMessageDialog(null, "Inside");
    Date startDate, endDate, currentDate, currentTime;
    String date = "2018-07-29 00:00:00";
    float minValue = 0;
    float maxValue = 0;
    String date_field = "CurDT";
    String chartFilePath = "E:\\TrendChart.jpeg";
    String pdfFilePath = "E:\\" + date.replaceAll(":", "_").replaceAll("-", "_") + "_TREND_report.pdf";
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
    String url = "jdbc:sqlserver://DESKTOP-DINSO\\DINSO:1433;databaseName=master;integratedSecurity=true";
    JOptionPane.showMessageDialog(null, "step 1");

    try {
        JOptionPane.showMessageDialog(null, Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"));
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        JOptionPane.showMessageDialog(null, Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"));
    } catch (ClassNotFoundException ex) {
        JOptionPane.showMessageDialog(null, "class not found");
        Logger.getLogger(GenerateImage.class.getName()).log(Level.SEVERE, null, ex);
    }
    //JOptionPane.showMessageDialog(null, DriverManager.getConnection(url));
    con = DriverManager.getConnection(url);
    if (con == null) {
        JOptionPane.showMessageDialog(null, "connection is null");
    } else {
        JOptionPane.showMessageDialog(null, "connection is not null");
    }
    TimeSeries value = new TimeSeries("value");

    JOptionPane.showMessageDialog(null, "try started");
    String sql;
     //            sql = "SELECT * FROM \"Sheet1$\" WHERE " + date_field + 
     ">=" + startdate + " AND " + date_field + "<= " + enddate;
    sql = "SELECT * FROM \"Sheet1\"";
    Statement stmt = con.createStatement();
    int count = 0;
    ResultSet rs1 = stmt.executeQuery("SELECT COUNT(*) FROM \"Sheet1\"");
    while (rs1.next()) {
        count = rs1.getInt(1);
    }
    rs = stmt.executeQuery(sql);
    if (rs == null) {
        JOptionPane.showMessageDialog(null, "rs is null");
    }
    int sizeofRS = rs.getRow();

    rowData = new ArrayList<Row>();

    int i = 0;
    System.out.println("size of rs is " + count);

    for (int j = 0; j < count; j = j + 1) {
        if (j < count) {
            rs.next();
            r2 = new Row(rs.getString(1), rs.getString(5));
            rowData.add(r2);
            currentDate = sdf.parse(rs.getString(1));
            value.addOrUpdate(new Minute(currentDate), 
            Float.valueOf(rs.getString(5)));
            System.out.println(" row data size " + rowData.size());
        }
    }
    con.close();
    JOptionPane.showMessageDialog(null, "sql connection closed");
    System.out.println("Connection closed");
}

I have also included sqljdbc.dll directory in project Configurations.

I have added many popups just for debugging purposes.

This GenerateImage constructor is called in the main method.

My database type is odbc.

The code works fine in IDE and also in jar format. why does this not work when packed in .exe and installed on the same system.

I have used Inno native package manager by NetBeans.

Okay so this is a critical problem I realized. The problem is missing file "sqljdbc_auth.dll" in jre. So when you pack your application remember copy the file in your jre directory so that it will be included in the package.

There is another scenario where you have done everything right but then too the application won't work because the client machine already has a jre installed which won't have the missing file. So here you'll have to copy the file again or ask the client to remove any jre or jdk from the system as the application will use system jre first then prefer packaged jre

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM