1 2 3 4

10.29

昨天看那个异常处理的代码了,所以对那个选课系统稍微搁置了一下,今天全拿出来了。

上代码

dao层代码

package systemUse;

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

public class inputToMYSQL {
    public void addData(userData data) {
        System.out.println(2);
        Connection connection=linkMYSQL.link_mysql();//链接数据库
        PreparedStatement ps=null;
        System.out.println(1);
        try {
            System.out.println(3);
            //String input="insert into pleaseok(name,teacher,place)"+"values (?,?,?)";
            System.out.println(4);
            ps=connection.prepareStatement("insert into please(userid,userpass,name,teacher,place)"+"values (?,?,?,?,?)");
            System.out.println(5);
            ps.setString(1, data.getuserid());
            ps.setString(2, data.getuserpass());
            ps.setString(3, data.getName());
            ps.setString(4, data.getTeacher());
            ps.setString(5, data.getPlace());
            /*int row=ps.executeUpdate();
            if(row>0) {
                System.out.println("成功");
            }
            */
            
            ps.executeUpdate();
            
            System.out.println("数据导入成功");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            linkMYSQL.stop_link_mysql(connection);
            linkMYSQL.stop_link_mysql(ps);
        }
    }
    public List<String> getData() {
        List<String> str=new ArrayList<String>();
        Connection connection=linkMYSQL.link_mysql();//链接数据库
            System.out.println(1);
        Statement ps=null;

        try {
            ps = connection.createStatement();
            String output = "select * from please;";
            System.out.println("test use5");
             ResultSet rs = ps.executeQuery(output);
               System.out.println("test use6");
             while(rs.next()){
                 String userid=rs.getString("userid");
                 String userpass=rs.getString("userpass");
                 String name = rs.getString("name");
                 String teacher = rs.getString("teacher");
                 String place = rs.getString("place");
     
                 // 输出数据
               //  System.out.print(name+" "+teacher+" "+place);
                 str.add(userid+userpass+name+teacher+place);
                // System.out.print("\n");
                 
             }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println(2);
        linkMYSQL.stop_link_mysql(connection);
        linkMYSQL.stop_link_mysql(ps);
        return str;
    }
}

连接数据库的代码

package systemUse;

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 linkMYSQL {
    // MySQL 8.0 以上版本 - JDBC 驱动名及数据库 URL
    static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";  
    //*************************************************服务器端口/数据库名称?useSS**********************
    static final String URL = "jdbc:mysql://localhost:3306/RUNOOBA?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
    // 数据库的用户名与密码                               
    static final String USER = "root";
    static final String PASS = "root1";
    public static Connection link_mysql()  {
        Connection conn=null;
            try {
                Class.forName(JDBC_DRIVER);
                System.out.println("连接数据库...");
                conn = DriverManager.getConnection(URL,USER,PASS);
                System.out.println("已连接数据库");
                
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        return conn;
            
    }
    public static void stop_link_mysql(Connection conn) {
        if(conn!=null)
            try {
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }
    public static void stop_link_mysql(PreparedStatement pre) {
        if(pre!=null)
            try {
                pre.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }
    public static void stop_link_mysql(Statement pre) {
        if(pre!=null)
            try {
                pre.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }
    public static void stop_link_mysql(ResultSet res) {
        if(res!=null)
            try {
                res.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }
}

user数据

package systemUse;

public class userData {
    private String name;
    private String teacher;
    private String place;
    private String userid;
    private String userpass;

    public userData(String na,String te,String pl,String id,String pass) {
        this.name = na;
        this.place = pl;
        this.teacher = te;
        this.userid=id;
        this.userpass=pass;
    }

    public String getName() {
        return name;
    }
    public String getPlace() {
        return place;
    }

    public String getTeacher() {
        return teacher;
    }
    public String getuserid() {
        return userid;
    }
    public String getuserpass() {
        return userpass;
    }
}

 

posted @ 2020-10-29 23:16  小陈的太阳  阅读(58)  评论(0编辑  收藏  举报