try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://"+hostName.getText()+";" +
"databaseName="+dbName.getText()+";user="+userName.getText()+";password="+password.getText()+";";
Connection con = DriverManager.getConnection(connectionUrl);
if(con!=null){JOptionPane.showMessageDialog(this, "Connection Established");}
} catch (SQLException e) {
JOptionPane.showMessageDialog(this, e);
//System.out.println("SQL Exception: "+ e.toString());
} catch (ClassNotFoundException cE) {
//System.out.println("Class Not Found Exception: "+ cE.toString());
JOptionPane.showMessageDialog(this, cE.toString());
}
When there is an error it shows a long JOptionPane message box that is longer than the width of the computer screen. How can I break e.toString() into two or more parts.
I'm setting a character limit, then search for the last space character in that environment and write an "\n" there. (Or I force the "\n" if there is no space character). Like this:
And I call it like this in the (try)-catch bracket:
You have to use
\n
to break the string in different lines. Or you can:→ http://ninopriore.com/2009/07/12/the-java-joptionpane-class/ (dead link, see archived copy).
Similar to Andrew Thomson's answer, the following code let's you load an
HTML
file from the project root directory and display it in aJOptionPane
. Note that you need to add a Maven dependency for Apache Commons IO. Also the use ofHTMLCompressor
is a good idea if you want to read formatted HTML code from a file without breaking the rendering.This let's you manage the
HTML
code better than in Java Strings.Don't forget to create a file named
document.html
with the following content:Result: