• 大小: 10.02MB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-10-16
  • 语言: 数据库
  • 标签: springMVC  

资源简介

springMVC入门实例,对员工增删改查操作,员工关联部门,代码简单易懂。(员工系统-Mysql)

资源截图

代码片段和文件信息

package com.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.springemployee.crud.entities.Employee;


public class JDBC {
 PreparedStatement ps;
     ResultSet rs;
    
    //写一个连接数据库的方法
    private static Connection getConnection(){
     String url=“jdbc:mysql://localhost:3306/emp“;
     String userName=“root“;
     String password=“123456“;
     Connection conn=null;
       
     try{
     Class.forName(“com.mysql.jdbc.Driver“);
     }catch(ClassNotFoundException e){
     System.out.println(“找不到驱动!“);
     e.printStackTrace();
     }
    
     try{
     conn=DriverManager.getConnection(urluserNamepassword);
     if(conn!=null){
     System.out.println(“connection successful“);
     }
     }catch(SQLException e){
     System.out.println(“connection fail“);
     e.printStackTrace();
     }
     return conn;
    }
    //写一个查询数据库语句的方法
    
    
    public static int insert(Employee employee) {
     Connection conn = getConnection();
        int i = 0;
        String sql = “insert into Employee (lastNameemailgenderdepartment) values(????)“;
        PreparedStatement pstmt;
        try {
            pstmt = (PreparedStatement) conn.prepareStatement(sql);
            pstmt.setString(1 employee.getLastName());
            pstmt.setString(2 employee.getEmail());
            pstmt.setInt(3 employee.getGender());
            pstmt.setInt(4 employee.getDepartment());
            pstmt.execute();
            pstmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }
    
    
    
    public static int update(Employee employee) {
     Connection conn = getConnection();
        int i = 0;
        String sql = “update Employee set lastName=‘“+employee.getLastName()+“‘ email=‘“+employee.getEmail()+“‘ gender=“+employee.getGender()+“ department=“+employee.getDepartment()+“ where id=“ +employee.getId();
        PreparedStatement pstmt;
        try {
            pstmt = (PreparedStatement) conn.prepareStatement(sql);
            i = pstmt.executeUpdate();
            System.out.println(“resutl: “ + i);
            pstmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }
    
    
    public static List getAll() {
     Connection conn = getConnection();
     List list=new ArrayList();
        String sql = “select * from Employee“;
        PreparedStatement pstmt;
        try {
            pstmt = (PreparedStatement)conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
           // int col = rs.getmetaData().getColumnCount();
            System.out.println(“=

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2017-03-23 16:01  springemployee\
     文件         898  2017-03-21 14:18  springemployee\.classpath
     文件        1122  2017-03-16 10:22  springemployee\.project
     目录           0  2017-03-16 11:58  springemployee\.settings\
     文件         567  2017-03-16 10:11  springemployee\.settings\.jsdtscope
     文件         122  2017-03-22 19:10  springemployee\.settings\org.eclipse.core.resources.prefs
     文件         364  2017-03-16 10:11  springemployee\.settings\org.eclipse.jdt.core.prefs
     文件         494  2017-03-16 10:11  springemployee\.settings\org.eclipse.wst.common.component
     文件         349  2017-03-16 10:11  springemployee\.settings\org.eclipse.wst.common.project.facet.core.xml
     文件          49  2017-03-16 10:11  springemployee\.settings\org.eclipse.wst.jsdt.ui.superType.container
     文件           6  2017-03-16 10:11  springemployee\.settings\org.eclipse.wst.jsdt.ui.superType.name
     文件         455  2017-03-16 10:22  springemployee\.springBeans
     目录           0  2017-03-16 10:11  springemployee\build\
     目录           0  2017-03-25 09:41  springemployee\build\classes\
     目录           0  2017-03-25 09:41  springemployee\build\classes\com\
     目录           0  2017-03-25 09:41  springemployee\build\classes\com\jdbc\
     文件        5260  2017-03-25 09:41  springemployee\build\classes\com\jdbc\JDBC.class
     文件        4898  2017-03-25 09:41  springemployee\build\classes\com\jdbc\JdbcUtil.class
     目录           0  2017-03-25 09:41  springemployee\build\classes\com\springemployee\
     目录           0  2017-03-25 09:41  springemployee\build\classes\com\springemployee\crud\
     目录           0  2017-03-25 09:41  springemployee\build\classes\com\springemployee\crud\dao\
     文件        1567  2017-03-25 09:41  springemployee\build\classes\com\springemployee\crud\dao\DepartmentDao.class
     文件        1417  2017-03-25 09:41  springemployee\build\classes\com\springemployee\crud\dao\EmployeeDao.class
     目录           0  2017-03-25 09:41  springemployee\build\classes\com\springemployee\crud\entities\
     文件        1405  2017-03-25 09:41  springemployee\build\classes\com\springemployee\crud\entities\Department.class
     文件        2226  2017-03-25 09:41  springemployee\build\classes\com\springemployee\crud\entities\Employee.class
     目录           0  2017-03-25 09:41  springemployee\build\classes\com\springemployee\crud\handlers\
     文件        3512  2017-03-25 09:41  springemployee\build\classes\com\springemployee\crud\handlers\EmployeeHandler.class
     文件        3524  2017-03-25 09:41  springemployee\build\classes\com\springemployee\crud\handlers\JdbcTest.class
     文件        1165  2017-03-20 10:03  springemployee\build\classes\springmvc.xml
     文件         857  2017-03-25 09:45  springemployee\emp.sql
............此处省略59个文件信息

评论

共有 条评论