zoukankan      html  css  js  c++  java
  • 2020.10.23收获

    要求实现一个添加界面,添加任意课程,指定教室和老师。并实现在MYSQL的表中添加成功

    CourseDao.java

    package dao;
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import DBUtil.DBUtil;
    import domain.Course;
    public class CourseDao {
    	 
        /**
         * 添加
         * @param course
         * @return
         */
        public boolean add(Course course) {
            String sql = "insert into kecheng(name, teacher, classroom) values('" + course.getName() + "','" + course.getTeacher() + "','" + course.getClassroom() + "')";
            Connection conn = DBUtil.getConn();
            Statement state = null;
            boolean f = false;
            int a = 0;
             
            try {
                state = conn.createStatement();
                state.executeUpdate(sql);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                DBUtil.close(state, conn);
            }
             
            if (a > 0) {
                f = true;
            }
            return f;
        }
    }
    

    DBUtil.java

    package DBUtil;
    
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    
    public class DBUtil {
    
    	public static String db_url = "jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8&useSSL=false";
    	public static String db_user = "root";
    	public static String db_pass = "liutianwen0613";
    
    	public static Connection getConn () {
    		Connection conn = null;
    
    		try {
    			Class.forName("com.mysql.jdbc.Driver");
    			conn = DriverManager.getConnection(db_url, db_user, db_pass);
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    
    		return conn;
    	}//end getConn
    
    	public static void close (Statement state, Connection conn) {
    		if (state != null) {
    			try {
    				state.close();
    			} catch (SQLException e) {
    				e.printStackTrace();
    			}
    		}
    
    		if (conn != null) {
    			try {
    				conn.close();
    			} catch (SQLException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    
    	public static void close (ResultSet rs, Statement state, Connection conn) {
    		if (rs != null) {
    			try {
    				rs.close();
    			} catch (SQLException e) {
    				e.printStackTrace();
    			}
    		}
    
    		if (state != null) {
    			try {
    				state.close();
    			} catch (SQLException e) {
    				e.printStackTrace();
    			}
    		}
    
    		if (conn != null) {
    			try {
    				conn.close();
    			} catch (SQLException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    
    	public static void main(String[] args) throws SQLException {
    		Connection conn = getConn();
    		PreparedStatement pstmt = null;
    		ResultSet rs = null;
    		String sql ="select * from kecheng";
    		pstmt = conn.prepareStatement(sql);
    		rs = pstmt.executeQuery();
    		if(rs.next()){
    			System.out.println("连接成功");
    		}else{
    			System.out.println("连接失败");
    		}
    	}
    }
    

    LTW.jsp  

    package DBUtil;
    
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    
    public class DBUtil {
    
    	public static String db_url = "jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8&useSSL=false";
    	public static String db_user = "root";
    	public static String db_pass = "liutianwen0613";
    
    	public static Connection getConn () {
    		Connection conn = null;
    
    		try {
    			Class.forName("com.mysql.jdbc.Driver");
    			conn = DriverManager.getConnection(db_url, db_user, db_pass);
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    
    		return conn;
    	}//end getConn
    
    	public static void close (Statement state, Connection conn) {
    		if (state != null) {
    			try {
    				state.close();
    			} catch (SQLException e) {
    				e.printStackTrace();
    			}
    		}
    
    		if (conn != null) {
    			try {
    				conn.close();
    			} catch (SQLException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    
    	public static void close (ResultSet rs, Statement state, Connection conn) {
    		if (rs != null) {
    			try {
    				rs.close();
    			} catch (SQLException e) {
    				e.printStackTrace();
    			}
    		}
    
    		if (state != null) {
    			try {
    				state.close();
    			} catch (SQLException e) {
    				e.printStackTrace();
    			}
    		}
    
    		if (conn != null) {
    			try {
    				conn.close();
    			} catch (SQLException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    
    	public static void main(String[] args) throws SQLException {
    		Connection conn = getConn();
    		PreparedStatement pstmt = null;
    		ResultSet rs = null;
    		String sql ="select * from kecheng";
    		pstmt = conn.prepareStatement(sql);
    		rs = pstmt.executeQuery();
    		if(rs.next()){
    			System.out.println("连接成功");
    		}else{
    			System.out.println("连接失败");
    		}
    	}
    }
    

    Course.java 

    package domain;
    
    public class Course {
    
        private String name;
        private String teacher;
        private String classroom;
         
    
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getTeacher() {
            return teacher;
        }
        public void setTeacher(String teacher) {
            this.teacher = teacher;
        }
        public String getClassroom() {
            return classroom;
        }
        public void setClassroom(String classroom) {
            this.classroom = classroom;
        }
         
        public Course() {}
         
        public Course(String name, String teacher, String classroom) {
            
            this.name = name;
            this.teacher = teacher;
            this.classroom = classroom;
        }
    }
    

     

  • 相关阅读:
    【cocos】添加监听函数,先使用handle封装函数更优
    【cocos】PC端没问题,移动端人物模型消失
    【lua】lua string.match 和 string.split 从字符串中寻找特定字符串并保存
    APP测试通用用例--我的(用户角色全选、资料、修改密码、版本更新、消息提醒、关于APP、清除缓存等)
    APP通用测试用例(一)-登录和页面展示
    Django学习(一)-sign下的Hello Django!
    Django学习(一)
    如何快速去掉word中的文字被标红或者是显示成下划线
    在与SQLServer建立连接时出现与网络相关的或特定于实例的错误解决
    使用mdf和ldf附加还原SQL Server数据文件
  • 原文地址:https://www.cnblogs.com/ltw222/p/13885020.html
Copyright © 2011-2022 走看看