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

//模板

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();
   }
 }
}

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

 

你可能感兴趣的:(sql,C++,c,mysql,jdbc)