考取JAVA证件的资料

源代码在线查看: jsp+beans实现读取数据库的例子.txt

软件大小: 4137 K
上传用户: lanyiting1
关键词: JAVA
下载地址: 免注册下载 普通下载 VIP

相关代码

				作者:asp3000
				email: coolknight@263.net
				日期:2000-8-7 17:50:30
				jsp+beans实现读取数据库的例子
				Select.jsp
				
				
				
				Select everything from a database
				
				
				
				
				
				
				
				
				
				
				Format results
				
				
				
				
				
				
				
				   
				       
				           
				       
				   
				
				
				
				
				
				
				DataBaseSelect.java
				import java.sql.*;
				import java.util.Vector;
				
				/**
				 * DataBaseSelect.java
				 * Written by Morgan Catlin
				 *   August 19, 1999
				 * 
				 * Variables:
				 *   Vector result = where I put the results of a select query
				 * 
				 * Methods:
				 *   Vector getResult() = returns result
				 *   void setResult() = sets result
				 *   String connect() = connects to a database
				 *   String select() = selects information from a database
				 */
				
				public class DataBaseSelect {
				   
				   private Vector result;
				     
				   public DataBaseSelect() {
				      result = new Vector();
				   } // constructor DataBaseSelect
				   
				   public String connect() {
				      try {
				     Class.forName("a jdbc driver").newInstance();
				     return "Driver Loaded!";
				      } catch (Exception E) {
				     return "Unable to load driver.";
				      }
				   }
				   
				   public String select() {
				      try {
				     Connection C = DriverManager.getConnection("database connection string");
				     
				     Statement Stmt = C.createStatement();
				     
				     ResultSet myResult = Stmt.executeQuery("SELECT acolumn from atable");
				     
				     while (myResult.next()) {
				        result.addElement(myResult.getString(1));
				     }
				     
				         // Clean up
				         myResult.close();
				         Stmt.close();
				         C.close();
				     return "Connection Success!";
				      } catch (SQLException E) {
				     return "SQLException: " + E.getMessage();
				      }
				   }
				      
				   /**
				    * Accessor for result
				    **/
				   public Vector getResult() {
				      return result;
				   }
				   
				   /**
				    * Mutator for result
				    **/
				   public void setResult(Vector avector) {
				     result = avector;
				   }  
				   
				} // class DataBaseSelect
							

相关资源