2011-10-19 25 views

回答

2

下面的代码是JAVA。相应地转换为JSP

import java.text.ParseException; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.sql.Statement; 
import java.util.logging.Level; 
import java.util.logging.Logger; 

public class Main 
{ 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) throws ParseException 
    { 
     try 
     {    
      String db_file_name_prefix = "c:\\mydbdir\\mydb"; 

      Connection con = null; 
      // Load the HSQL Database Engine JDBC driver 
      // hsqldb.jar should be in the class path or made part of the current jar 
      Class.forName("org.hsqldb.jdbcDriver"); 

      // connect to the database. This will load the db files and start the 
      // database if it is not alread running. 
      // db_file_name_prefix is used to open or create files that hold the state 
      // of the db. 
      // It can contain directory names relative to the 
      // current working directory 
      con = DriverManager.getConnection("jdbc:hsqldb:file:" + db_file_name_prefix, // filenames 
        "sa", // username 
        ""); // password 

      Statement statement = con.createStatement(); 
      //look at " for table name 
      ResultSet rs = statement.executeQuery("SELECT * FROM \"User\""); 

      //print the result set 
      while (rs.next()) 
      { 
       System.out.print("ID: " + rs.getString("ID")); 
       System.out.print(" first name: " + rs.getString("firstname")); 
       System.out.println(" last name: " + rs.getString("lastname")); 
      } 

      statement.close(); 
      con.close(); 

     } catch (SQLException ex) 
     { 
      Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); 
      ex.printStackTrace(); 
     } catch (ClassNotFoundException ex) 
     { 
      Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 
} 
+0

谢谢@Siva查兰....我做的每一件事情按建议的意见,并成功编译我的代码,但在运行它显示一个运行时错误.. java.sql.SQLSyntaxErrorException:用户缺少privalege或找不到对象 –