mysql数据库的连接

时间:2021-06-30 13:02:33
public TJb checkjbByschool(long id)throws ClassNotFoundException,SQLException {
        Class.forName("com.mysql.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1/sql", "user1", "11111111");
        Statement statement = connection.createStatement();
        ResultSet resultSet;
        String s = "select * from tjb "
                + "where time=(select max(time) from tjb where school ="+school_id+" ) "
                + "and school="+school_id;
        resultSet = statement.executeQuery(s);
        if (resultSet.next())
        {
            TJb jb = (TJb)jbRepository.findOne(resultSet.getLong("id"));
            /**数据库关闭链接**/
            connection.close();
            return jb;
        }
        else {
            /**数据库关闭链接**/
            connection.close();
            return null;
        }
    }