Giter Site home page Giter Site logo

java-web's Issues

实验六:JDBC编程

1. 三层架构

image

2. 加载驱动

Class.forName("");

注意:驱动程序应该到对应的数据库厂商网站下载,如:PostgreSQL https://jdbc.postgresql.org/download.html
Tips:准备面试还应该了解JDBC驱动的4种类型。

3. 连接数据库

String url = "jdbc:postgresql://<database_host>:<port>/<database_name>";
Connection con = DriverManager.getConnection(url, "userID", "password"); 

好的做法是保存在常量中,例如:

private final String url = "jdbc:postgresql://localhost/dvdrental";
private final String user = "postgres";
private final String password = "<add your password>";

详细的连接代码:

public Connection connect() {
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(url, user, password);
            System.out.println("Connected to the PostgreSQL server successfully.");
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
 
        return conn;
    }

更好的做法是将配置保存在配置文件中,如:jdbc.properties,可方便数据库连接管理,如切换开发与生产数据库。Hibernate等框架也采用这种方法。(问:Hibernate是怎么配置的?)

4. 准备SQL语句

select * from TABLE_NAME;
insert into ... ;
update ...;
delete ...;

http://www.w3school.com.cn/sql/

5. 查询数据

public int getActorCount() {
        String SQL = "SELECT count(*) FROM actor";
        int count = 0;
 
        try (Connection conn = connect();
                Statement stmt = conn.createStatement();
                ResultSet rs = stmt.executeQuery(SQL)) {
            rs.next();
            count = rs.getInt(1);
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
 
        return count;
    }

6. 插入数据

public long insertActor(Actor actor) {
        String SQL = "INSERT INTO actor(first_name,last_name) "
                + "VALUES(?,?)";
 
        long id = 0;
 
        try (Connection conn = connect();
                PreparedStatement pstmt = conn.prepareStatement(SQL,
                Statement.RETURN_GENERATED_KEYS)) {
 
            pstmt.setString(1, actor.getFirstName());
            pstmt.setString(2, actor.getLastName());
 
            int affectedRows = pstmt.executeUpdate();
            // check the affected rows 
            if (affectedRows > 0) {
                // get the ID back
                try (ResultSet rs = pstmt.getGeneratedKeys()) {
                    if (rs.next()) {
                        id = rs.getLong(1);
                    }
                } catch (SQLException ex) {
                    System.out.println(ex.getMessage());
                }
            }
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
        return id;
    } 

7. 修改数据

TODO

8. 删除数据

TODO

参考文献

  1. http://www.postgresqltutorial.com/postgresql-jdbc/
  2. http://www.w3school.com.cn/sql/
  3. http://www.sql-tutorial.net/

音乐论坛管理系统

1.论坛管理员可设立板块,置顶文章,删除文章。
2.楼主发布文章,发表评论。

image

实验一:Web基础

一、实验目的

练习并掌握简单HTML网页制作方法。

二、实验设备与环境

操作系统:Windows;开发平台:GitHub;开发工具:Sublime Text。

三、实验内容及步骤

  1. 安装和配置Git代码开发环境
    要求:成功安装Git工具;Fork实验库,将自己帐号下的实验库克隆到本地;结果截图到实验报告。
    提示:git clone https://github.com/{你的账号}/java-web.git
  2. 编写一个表单网页
    要求:
    (1)基于 http://www.bootcss.com/ 上的前端CSS库;
    (2)根据自选题目编写一个带表单的网页;
    (3)通过JavaScript或jQuery实现表单提交;
    (4)表单处理结果保存在一个JSON文件中;
    (5)提交成功之后JS弹窗显示JSON文件的内容(提示信息);
    (6)所有文件保存在个人目录下:labs/学号/,见:https://github.com/hzuapps/java-web。
    提示:
    (1)建议选择 http://v3.bootcss.com/ 库;
    (2)创建网页文件:labs/{你的学号}/index.html;
    (3)使用 <link>标签引用 Bootstrap 的CSS文件,或者使用CDN,参考:http://v3.bootcss.com/getting-started/
    (4)编写表单,参考:http://v3.bootcss.com/css/#forms;
    (5)创建JSON文件:labs/{你的学号}/success.json;
    (6)创建JavaScript文件:labs/{你的学号}/index.js;
    (7)使用jQuery提交代码,并显示JSON中的信息。
  3. 将代码提交到自己Fork的实验库
  4. 将实验结果填写到实验报告中
    要求:实验报告上传到 http://zeng.shaoning.net/javaweb/uploads/

代码及命令提示

  • 提交代码到GitHub
$ git pull
$ git add labs/{你的学号}/*
$ git commit -m "#2 提交实验代码"
$ git push
  • 编写表单代码
<form role="form">
  <div class="form-group">
    <label for="exampleInputEmail1">Email address</label>
    <input type="email" class="form-control" id="exampleInputEmail1" placeholder="Enter email">
  </div>
  <div class="form-group">
    <label for="exampleInputPassword1">Password</label>
    <input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
  </div>
  <div class="form-group">
    <label for="exampleInputFile">File input</label>
    <input type="file" id="exampleInputFile">
    <p class="help-block">Example block-level help text here.</p>
  </div>
  <div class="checkbox">
    <label>
      <input type="checkbox"> Check me out
    </label>
  </div>
  <button type="submit" class="btn btn-default">Submit</button>
</form>
$( "#target" ).submit(function( event ) {
  alert( "Handler for .submit() called." );
  event.preventDefault();
});
$.ajax({
  url: "success.json
}).done(function(data) {
  if ( console && console.log ) {
      console.dir(data);
      alert(data.msg);
    }
});
  • JSON文件内容
{
  "msg": "保存成功!"
}

实验二:Tomcat配置

实验目标

  1. 练习Tomcat的配置、管理及应用部署;
  2. 掌握在Eclipse中连接Tomcat调试代码的方法。

实验内容及步骤

  1. 下载Tomcat 8,在本地安装并运行;
  2. 修改Tomcat配置文件,增加一个具有部署应用权限的管理员帐号;
  3. 在Eclipse中创建动态Web项目,然后在Tomcat服务器中运行;
  4. 将实验一的网页改写成JSP,添加到Web项目中;
  5. 在JSP文件中编写Scriptlet,并添加断点进行调试;
  6. 新的JSP页面提交到自己的学号目录下。

实验要求

  1. 所有实验步骤截图并插入到实验报告中;
  2. JSP页面内容应该与自己所选题目相符合;
  3. 所开发功能不能是“登录”和“注册”。

Question

Good morning!Hi,Mr.zeng,could you tell us your WeChat number?

实验三:Servlet编程

实验要求

  1. 创建Java Web工程(jweb)或导入 jweb 工程到 Eclipse 中;
  2. 在 edu.hzu.javaweb.labs 包下创建自己的学号包(前缀为 se);
  3. 在自己的学号包下创建一个Servlet,可以命名为 Se{学号}Servlet;
  4. 将 Servlet 处理的请求 URL 指向自己的学号,如:/12345678;
  5. 根据自己的题目在 Servlet 中编写 Java 后台代码;
  6. 所有网页文件放到工程的 web/{学号}/ 目录下。

提示

  1. 可以在自己本地电脑的其他文件夹创建工程,保持名称一致即可;
  2. 代码编写完成之后,将编写的文件复制到 GitHub 库所在文件夹提交代码即可。
  3. 注意一定不要修改不属于自己的文件,避免引起代码冲突。

实验七:Java应用部署

实验要求

  1. 将自己的应用程序打包为 WAR,部署到Tomcat上。
  2. 截图保存到实验报告提交。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.