源代碼
import java.sql.Connection;
import java.sql.DriverManager; 
import java.sql.ResultSet;
import java.sql.Statement;public class ShowInnodbStatus {

public static void main(String[] args){ 
  Connection con = null; 
  try{ 
  //1.加载驱动 
  String driverName = "com.mysql.jdbc.Driver"; 
      Class.forName(driverName); 
  //2.连接数据库 
  String url = 
    "jdbc:mysql://127.0.0.1:3306/mysql"; 
  String userName = "root"; 
  String pwd = "root"; 
   con = (Connection) DriverManager.getConnection(url,userName,pwd); 
      Statement st=(Statement) con.createStatement(); 
      ResultSet rs=(ResultSet) st.executeQuery("show innodb status"); 
      while(rs.next()){ 
      System.out.println(rs.getString(1)); 
      } 
  } catch(Exception e){ 
  e.printStackTrace(); 
  }finally{ 
  try{ 
        con.close(); 
  }catch(Exception ex){ 
    ex.printStackTrace(); 
  } 
  } 

}現在執行代碼 
我用的是最新的mysql-connector-java-5.1.5-bin.jar
保存沒有錯誤 。 運行提示錯誤: 
java virtual Machine Laouncher 
could not find the main calss.  program will exit. 代碼就是上面的哦---估計還是我這邊配置原因 , java不是很熟悉 
java.lang.NoClassDefFoundError: org/apache/juli/logging/LogFactory 
at org.apache.catalina.startup.Bootstrap. <clinit>(Bootstrap.java:54) 
Caused by: java.lang.ClassNotFoundException: org.apache.juli.logging.LogFactory 
at java.net.URLClassLoader$1.run(Unknown Source) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.net.URLClassLoader.findClass(Unknown Source) 
at java.lang.ClassLoader.loadClass(Unknown Source) 
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) 
at java.lang.ClassLoader.loadClass(Unknown Source) 
at java.lang.ClassLoader.loadClassInternal(Unknown Source) 
... 1 more 
Exception in thread "main"

解决方案 »

  1.   

    我试过了,程序没有任何问题,运行的方式可能不正确。
    你在eclipse中用右键点击java文件“选择运行”->"java 应用程序"的方式运行试试,没问题的
      

  2.   

    文件名不是 ShowInnodbStatus.java
      

  3.   

    把public class ShowInnodbStatus前面的public 去掉再试试.
      

  4.   

    是的 感謝樓上 ,上面的問題已經解決 ,代碼沒有問題。 
    但為了解決問題, 我不得不繼續提出問題:
    如果java web程序应用发生了bug,比如try  catch 抛出错误, 
    我是否能够根据这个例外在执行楼上的代码 执行完后 退出 (逻辑上是否可行?) 
     
    (有人說执行sql语句要确保有connection,如果bug产生后没有connection是没法得到show innode status 的信息的----是否是這樣??)
      

  5.   

    用try catch来控制流程是不推荐的.
    我不懂sql.
      

  6.   

    我是否能够根据这个例外在执行楼上的代码 执行完后 退出你可以把这段代码写在catch里面,前提是这个异常会被正确的捕获进来,也可以加个finally,把代码写在finally里面。
      

  7.   

    "你可以把这段代码写在catch里面,前提是这个异常会被正确的捕获进来,也可以加个finally,把代码写在finally里面。"按照正常道理,程序產生了bug,如果我能得到這個異常, 我還能寫入數據庫的某個表的數據嗎?
    java異常錯誤如下:
    stange   error:com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException:   Deadlock   found   when   trying   to   get   lock;   try   restarting   transaction 
    com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException:   Deadlock   found   when   trying   to   get   lock;   try   restarting   transaction 
            at   sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native   Method) 
            at   sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) 
            at   sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) 
            at   java.lang.reflect.Constructor.newInstance(Constructor.java:513) 
            at   com.mysql.jdbc.Util.handleNewInstance(Util.java:406) 
            at   com.mysql.jdbc.Util.getInstance(Util.java:381) 
            at   com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1046) 
            at   com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957) 
            at   com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3376) 
            at   com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3308) 
            at   com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1837) 
            at   com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1961) 
            at   com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2543
      

  8.   

    我的code
      try{ 
      //1.加载驱动 
      。。
          while(rs.next()){ 
          System.out.println(rs.getString(1)); 
          } 
      } catch(Exception e){ 
      e.printStackTrace(); 
      }finally{ ----------你的意思我把把代码写在finally里面。"    try{ 
            con.close(); 
      }catch(Exception ex){ 
        ex.printStackTrace(); 
      } 
      } 

    }這里請問finally 前面和后面try  catch 有什么區別?
      

  9.   

    手工执行的? 命令应该是 java -cp .;mysql-connector-java-5.1.5-bin.jar ShowInnodbStatus
      

  10.   


    finally里面的代码确保会执行,也就是说:即使程序发生异常了,还是会执行finally中的con.close()方法的。