javaee-jdbc,javaee

來源:互聯網
上載者:User

javaee-jdbc,javaee

1、先要匯入jdbc-mysql包

2、程式架構:

3、資料庫設定檔

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/jdbc_db
username=root
password=1234

4、訪問底層資料:

package cn.com.dale.utils;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class Utils {

    public Connection openConnection(){
        Properties pro = new Properties();
        try {
            pro.load(this.getClass().getClassLoader().getResourceAsStream("DBConfig.properties"));
            String driver = pro.getProperty("driver");
            String url = pro.getProperty("url");
            String username = pro.getProperty("username");
            String password = pro.getProperty("password");
            
            Class.forName(driver);
            return DriverManager.getConnection(url,username,password);        
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    
    public void closeConnection(Connection conn){
        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}


5、javabean:

package cn.com.dale.Bean;

public class User {
    private String name;
    private String password;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    
}

6、介面:

package cn.com.dale.Dao;

import cn.com.dale.Bean.User;

public interface UserDao {

    //denglu
    public User login(User user);
    //zhuce
    public void register(User user);
    //jiancha
    public boolean check(String name);
    
}

7、介面實作類別:

package cn.com.dale.DaoImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import cn.com.dale.Bean.User;
import cn.com.dale.Dao.UserDao;
import cn.com.dale.utils.Utils;

public class UserDaoImpl implements UserDao{

    public void register(User user) {
        
        if(check(user.getName())==false){
            System.out.println("該使用者名稱已存在!");
            return;
        }
        String sql = "insert into usertab(username,password) values(?,?)";
        Utils utils = new Utils();
        Connection conn = utils.openConnection();
        try {
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setString(1,user.getName());
            stmt.setString(2, user.getPassword());
            
            stmt.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            utils.closeConnection(conn);
        }
    }

    public User login(User user) {
        String sql="select * from usertab where username=? and password=?";
        Utils utils = new Utils();
        Connection conn = utils.openConnection();
        try {
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setString(1, user.getName());
            stmt.setString(2, user.getPassword());
            ResultSet rs = stmt.executeQuery();
            if(rs.next()){
                return user;
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            utils.closeConnection(conn);
        }
        return null;
    }


    public boolean check(String name) {
        String sql = "select * from usertab where username=?";
        
        Utils utils = new Utils();
        Connection conn = utils.openConnection();
        try {
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setString(1, name);
            ResultSet rs = stmt.executeQuery();
            if(rs.next()){
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            utils.closeConnection(conn);
        }
        return true;
    }   
}


8、測試:

package cn.com.dale.test;

import org.junit.Test;

import cn.com.dale.Bean.User;
import cn.com.dale.Dao.UserDao;
import cn.com.dale.DaoImpl.UserDaoImpl;

public class test01 {

    @Test
    public void test(){
        //Utils utils = new Utils();
        //System.out.println(utils.openConnection());
        
        User user = new User();
        user.setName("小非");
        user.setPassword("123");
        
        UserDao userDao = new UserDaoImpl();
        userDao.register(user);
        
        User user1 = userDao.login(user);
        System.out.println(user1.getName()+":"+user1.getPassword());
        
    }
}



相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.