2020年10月27日Java学习日记

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

CourseDao.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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;
    }
}

 

 

posted @ 2020-10-27 01:26  Zwyooo  阅读(43)  评论(0编辑  收藏  举报