In Java, how do I set a return type if an exception occurs?

后端 未结 6 1076
[愿得一人]
[愿得一人] 2021-02-14 10:37

hey all, I\'m new to Java and was wondering if I define a method to return a database object

like

import java.sql.*;

public class DbConn {

    public          


        
6条回答
  •  孤街浪徒
    2021-02-14 11:22

    Try this one

    public ActionForward Login(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
        MigForm migForm = (MigForm) form;// TODO Auto-generated method stub
    
        Connection con = null;
        Statement st = null;
        ResultSet rs = null;
    
        String uname=migForm.getUname();
        String pwd=migForm.getPwd();
    
        try{
            Class.forName("oracle.jdbc.driver.OracleDriver");
            con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","uname","pwd");
            if(con.isClosed())
            {
                return mapping.findForward("success");
            }
    
            //st=con.createStatement();
    
            }catch(Exception err){
    
            System.out.println(err.getMessage());
            }
    
    
                return mapping.findForward("failure");
    
    
    
    }
    

提交回复
热议问题