Page 388 - 완) I MDP 프로젝트 작품 보고서(전체과 1학년)1.6
P. 388

인천전자마이스터고                                                                            전자회로설계과



                  System.out.println("Connecting to database...");

                  conn = DriverManager.getConnection(DB_URL,USER,PASS);


                  //STEP 4: Execute a query
                  System.out.println("Creating statement...");
                  stmt = conn.createStatement();

                  String sql;
                  sql = "SELECT phone FROM member";
                  ResultSet rs = stmt.executeQuery(sql);


                  //STEP 5: Extract data from result set

                  while(flag){
                     //Retrieve by column name
                     phone = rs.getString("phone");


                     //Display values

                     System.out.println("Phone: " + phone);
                  }
                  //STEP 6: Clean-up environment
                  rs.close();
                  stmt.close();

                  conn.close();
                       }catch(SQLException se){
                          //Handle errors for JDBC
                          se.printStackTrace();
                       }catch(Exception e){

                          //Handle errors for Class.forName
                          e.printStackTrace();
                       }finally{
                          //finally block used to close resources
                          try{

                             if(stmt!=null)
                                stmt.close();
                          }catch(SQLException se2){
                          }// nothing we can do
                          try{

                             if(conn!=null)




                                                         -  381  -
   383   384   385   386   387   388   389   390   391   392   393