登陆模块升级版本之模板方法篇

//模板

package cn.bless_remind;

import java.sql.DriverManager;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.ResultSet;

public abstract  class TempleteDemo
{
 Connection connection;
 PreparedStatement statement;
 ResultSet result;
 
 public abstract  void doExecute(String name,String password);
  
 public Connection getConnection()
 {
//  ReadProperties read = new ReadProperties();
                try
    {
                 System.out.println("正在连接……");
     Class.forName("com.mysql.jdbc.Driver");
//    connection = (Connection) DriverManager.getConnection(
//      read.getName(),
//      read.getPassword()
//      );
//    System.out.println("url is: "+read.getUrl());
//     System.out.println("name is: "+read.getName());
//    System.out.println("password is: "+read.getPassword());
     connection = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/itcast","root","");
     System.out.println("连接成功!");
    } catch (ClassNotFoundException e)
    {
     e.printStackTrace();
    } catch (SQLException e)
    {
     e.printStackTrace();
    }
    return connection;
 }
 public void closeConnection()
 {
  if(connection!=null)
  {
   try
   {
    connection.close();
    System.out.println("已关闭连接");
   }
   catch(Exception e)
   {
    e.printStackTrace();
   }
  }
 }
 public void closeStatement()
 {
  if(statement!=null)
  {
   try
   {
    statement.close();
    System.out.println("已关声明!");
   }
   catch(Exception e)
   {
    e.printStackTrace();
   }
  }
 }
 public void closeResult()
 {
  if(result!=null)
  {
   try
   {
    result.close();
    System.out.println("已关闭结果集!");
   }
   catch(Exception e)
   {
    e.printStackTrace();
   }
  }
      }
 }

//读取配置文件

package cn.bless_remind;

import java.io.FileInputStream;
import java.util.Properties;

public class ReadProperties
{
 String url;
 String name;
 String password;
 Properties properties = new Properties();
 FileInputStream input;
 
 public ReadProperties()
 {
  try
  {
    input = new FileInputStream("c:/a.properties");
    properties.load(input);
  }
  catch(Exception e)
  {
   e.printStackTrace();
  }
 }
 public String getUrl()
 {
  url = properties.getProperty("url");
  return url;
 }
 public String getName()
 {
  name = properties.getProperty("name");
  return name;
 }
 public String getPassword()
 {
  password = properties.getProperty("password");
  return password;
 }
}

//properties文件

url=jdbc:mysql://localhost/itcast
name=root
password=

//登陆模块

package cn.bless_remind;

import java.sql.SQLException;

import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.ResultSet;

public class Login  extends TempleteDemo
{
 public void doExecute(String name,String password)
 {
   try
   {
    //connection = getConnection();
    statement = (PreparedStatement)connection.prepareStatement("select * from student where name=? and password=? ");
    statement.setString(1,name);
    statement.setString(2,password);
    ResultSet result = (ResultSet) statement.executeQuery();
    if(result.next())
    {
     System.out.println("正在登陆……");
     System.out.println("登陆成功!");
    }
    else
    {
     System.out.print("登陆失败!");
    }
   } catch (SQLException e)
   {
    e.printStackTrace();
   }
 }
}

总结:相对以前的登陆模块,现在的登陆模块实现了业务逻辑和数据逻辑的分离,并在相当大程度上降低了程序的耦合性,有利于程序的模块化以及移植

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值