Posted on 2006-06-14 15:03 
氧气中毒 阅读(287) 
评论(0)  编辑  收藏  所属分类: 
数据处理 
			 
			
		 
		
		
				 
				 /**/
				
						/*
				/**/
				
						/*
						
								
								 * 创建日期 2006-6-13
 * 创建日期 2006-6-13
 *
 *
 * java连接oracle数据库
 * java连接oracle数据库
 */
 
						*/
				
				
						
						 package
				 person.fane.test;
				
				package
				 person.fane.test;

 import
				 java.sql.
				*
				;
				import
				 java.sql.
				*
				;

 /** */
				
						/**
				/** */
				
						/**
						
								
								 * A JDBC test application for Oracle
 * A JDBC test application for Oracle
 * 
						@author
						 Fane
 * 
						@author
						 Fane
 * 
						@version
						 1.0.0
 * 
						@version
						 1.0.0
 * 
						@since
						 JDK1.4
 * 
						@since
						 JDK1.4
 */
 
						*/
				
				
						
						 
						 public
				 
				class
				 OracleTest
				
				public
				 
				class
				 OracleTest 
				
						 {
				
				
						{
 private
						 
						final
						 String oracleDriverName 
						=
						 
						"
						oracle.jdbc.driver.OracleDriver
						"
						;
    
						private
						 
						final
						 String oracleDriverName 
						=
						 
						"
						oracle.jdbc.driver.OracleDriver
						"
						;
 
    
 //
						以下使用的Test就是Oracle里的表空间
    
						//
						以下使用的Test就是Oracle里的表空间
						
								
								 private
						 
						final
						 String oracleUrlToConnect 
						=
						"
						jdbc:oracle:thin:@192.168.0.36:1521:Test
						"
						;
						
						    
						private
						 
						final
						 String oracleUrlToConnect 
						=
						"
						jdbc:oracle:thin:@192.168.0.36:1521:Test
						"
						; 
 private
						 Connection myConnection 
						=
						 
						null
						;
    
						private
						 Connection myConnection 
						=
						 
						null
						;

 /** */
						
								/**
    
						/** */
						
								/**
								
										
										 * To load the jdbc driver
     * To load the jdbc driver
 *
     * 
 */
     
								*/
						
						
								
								 public
						 OracleTest()
    
						public
						 OracleTest()

 
    
						
								 {
						
						
								{
 try
        
								try
								
										
										 
										 
        
								
										 {
								
								
										{
 Class.forName(oracleDriverName);
            Class.forName(oracleDriverName);
 }
								
								catch
								(ClassNotFoundException ex)
        }
								
								catch
								(ClassNotFoundException ex)

 
        
								
										 {
								
								
										{
 System.out.println(getErrorMessage(ex,
										"
										The Driver loaded error,please contact to your Software Designer!
										"
										).toString());
            System.out.println(getErrorMessage(ex,
										"
										The Driver loaded error,please contact to your Software Designer!
										"
										).toString());
 }
        }
								
								
										
										 }
    }
						
						
								
								 
    
 public
						 StringBuffer getErrorMessage(Exception ex,String alarmMessage)
    
						public
						 StringBuffer getErrorMessage(Exception ex,String alarmMessage)

 
    
						
								 {
						
						
								{
 StringBuffer errorStringBuffer 
								=
								 
								new
								 StringBuffer();
        StringBuffer errorStringBuffer 
								=
								 
								new
								 StringBuffer();
 errorStringBuffer.append(alarmMessage);
        errorStringBuffer.append(alarmMessage);
 errorStringBuffer.append(ex.getMessage());
        errorStringBuffer.append(ex.getMessage());
 return
								 errorStringBuffer;
        
								return
								 errorStringBuffer;
 }
    }
						
						
								
								 
    

 /** */
						
								/**
    
						/** */
						
								/**
								
										
										 * getConnection method
     * getConnection method 
 * 
								@return
								 Connection
     * 
								@return
								 Connection
 */
     
								*/
						
						
								
								 public
						 Connection getConnection()
    
						public
						 Connection getConnection()

 
    
						
								 {
						
						
								{
 try
        
								try
								
										
										 
										 
        
								
										 {
								
								
										{
 this
										.myConnection 
										=
										 DriverManager.getConnection(oracleUrlToConnect,
										"
										Fane
										"
										,
										"
										201
										"
										);
            
										this
										.myConnection 
										=
										 DriverManager.getConnection(oracleUrlToConnect,
										"
										Fane
										"
										,
										"
										201
										"
										);
 
            
 }
								
								catch
								(Exception ex)
        }
								
								catch
								(Exception ex)

 
        
								
										 {
								
								
										{
 System.out.println(getErrorMessage(ex,
										"
										Can not get connection,please contact to your Software Designer!
										"
										).toString());
            System.out.println(getErrorMessage(ex,
										"
										Can not get connection,please contact to your Software Designer!
										"
										).toString());
 }
        }
								
								
										
										 
        
 return
								 
								this
								.myConnection;
        
								return
								 
								this
								.myConnection;
 
        
 }
    }
						
						
								
								 
    

 /** */
						
								/**
    
						/** */
						
								/**
								
										
										 * 
								@param
								 args
     * 
								@param
								 args
 */
     
								*/
						
						
								
								 
								 public
						 
						static
						 
						void
						 main(String[] args)
    
						public
						 
						static
						 
						void
						 main(String[] args) 
						
								 {
						
						
								{
 OracleTest myOracleTest 
								=
								 
								new
								 OracleTest();
        OracleTest myOracleTest 
								=
								 
								new
								 OracleTest();
 try
        
								try
								
										
										 
										 
        
								
										 {
								
								
										{
 Connection myConnection 
										=
										 myOracleTest.getConnection();
            Connection myConnection 
										=
										 myOracleTest.getConnection();

 System.out.println(
										"
										Now begin to excute
            System.out.println(
										"
										Now begin to excute


 .
										"
										);
.
										"
										);
 
        
 PreparedStatement myPreparedStatement 
										=
										 myConnection.prepareStatement(
										"
										select area_id, area_name,ip_address,tel,area_type,pc_id from c_area_info
										"
										);
            PreparedStatement myPreparedStatement 
										=
										 myConnection.prepareStatement(
										"
										select area_id, area_name,ip_address,tel,area_type,pc_id from c_area_info
										"
										);
 //
										myPreparedStatement.setInt(1,2);
            
										//
										myPreparedStatement.setInt(1,2);
										
												
												 ResultSet myResultSet 
										=
										 myPreparedStatement.executeQuery();
										
										            ResultSet myResultSet 
										=
										 myPreparedStatement.executeQuery();
 StringBuffer myStringBuffer 
										=
										 
										new
										 StringBuffer();
            StringBuffer myStringBuffer 
										=
										 
										new
										 StringBuffer();
 
            
 while
										(myResultSet.next())
            
										while
										(myResultSet.next())

 
            
										
												 {
										
										
												{
 
                
 myStringBuffer.append(myResultSet.getInt(
												"
												area_id
												"
												)
												+
												"
												  
												"
												);
                myStringBuffer.append(myResultSet.getInt(
												"
												area_id
												"
												)
												+
												"
												  
												"
												);
 myStringBuffer.append(myResultSet.getString(
												"
												area_name
												"
												)
												+
												"
												  
												"
												);
                myStringBuffer.append(myResultSet.getString(
												"
												area_name
												"
												)
												+
												"
												  
												"
												);
 myStringBuffer.append(myResultSet.getString(
												"
												ip_address
												"
												)
												+
												"
												  
												"
												);
                myStringBuffer.append(myResultSet.getString(
												"
												ip_address
												"
												)
												+
												"
												  
												"
												);
 myStringBuffer.append(myResultSet.getString(
												"
												tel
												"
												)
												+
												"
												  
												"
												);
                myStringBuffer.append(myResultSet.getString(
												"
												tel
												"
												)
												+
												"
												  
												"
												);
 myStringBuffer.append(myResultSet.getInt(
												"
												area_type
												"
												)
												+
												"
												  
												"
												);
                myStringBuffer.append(myResultSet.getInt(
												"
												area_type
												"
												)
												+
												"
												  
												"
												);
 myStringBuffer.append(myResultSet.getInt(
												"
												pc_id
												"
												)
												+
												"
												\n
												"
												);
                myStringBuffer.append(myResultSet.getInt(
												"
												pc_id
												"
												)
												+
												"
												\n
												"
												);
 }
            }
										
										
												
												 System.out.println(myStringBuffer.toString());
            System.out.println(myStringBuffer.toString());
 //
										System.out.println(new String(myStringBuffer.toString().getBytes("ISO-8859-1"),"GBK"));
            
										//
										System.out.println(new String(myStringBuffer.toString().getBytes("ISO-8859-1"),"GBK"));
										
												
												 }
								
								catch
								(Exception ex)
										
										        }
								
								catch
								(Exception ex)

 
        
								
										 {
								
								
										{
 System.out.println(myOracleTest.getErrorMessage(ex,
										"
										Application error,please contact to your Software Designer!
										"
										).toString());
            System.out.println(myOracleTest.getErrorMessage(ex,
										"
										Application error,please contact to your Software Designer!
										"
										).toString());
 }
        }
								
								
										
										 
										
										 }
    }
						
						
								
								 
								
								 }
}