21xrx.com
2024-09-20 06:13:51 Friday
登录
文章检索 我的文章 写文章
Java开发常见的工作有哪些?
2023-06-17 16:46:01 深夜i     --     --
Java开发 Web应用程序 桌面应用程序 数据库编程 Servlet JDBC Swing

在Java开发中,开发人员需要编写Java代码来实现各种不同的功能。常见的Java开发工作包括:

1. 开发Web应用程序:

Java是Web应用程序的强大语言,因为它提供了许多可用于开发Web应用程序的功能,如Servlet,JSP等。Java开发人员使用这些工具来开发Web应用程序,这些程序可以运行在各种不同的服务器上。

下面是一个使用 Servlet 实现 Hello World 的例子:


import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class HelloWorld extends HttpServlet {

 public void doGet(HttpServletRequest request, HttpServletResponse response)

  throws ServletException, IOException {

  response.setContentType("text/html");

  PrintWriter out = response.getWriter();

  out.println("");

  out.println("

Hello World

");

  out.println("");

 }

}

2. 开发桌面应用程序:

Java也可以用于开发桌面应用程序。Java提供了涉及用户界面和运行时环境的工具,如Swing和AWT。使用这些工具,开发人员可以创建交互式应用程序。

下面是一个使用 Swing 实现简单计算器的例子:


import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

class Calculator extends JFrame implements ActionListener {

  JButton b1, b2, b3, b4;

  JTextField t1, t2, t3;

  Calculator() {

    b1 = new JButton("+");

    b2 = new JButton("-");

    b3 = new JButton("/");

    b4 = new JButton("*");

    t1 = new JTextField(10);

    t2 = new JTextField(10);

    t3 = new JTextField(10);

    add(t1);

    add(t2);

    add(t3);

    add(b1);

    add(b2);

    add(b3);

    add(b4);

    b1.addActionListener(this);

    b2.addActionListener(this);

    b3.addActionListener(this);

    b4.addActionListener(this);

    setLayout(new FlowLayout());

    setVisible(true);

    setSize(500, 500);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  }

  public void actionPerformed(ActionEvent e) {

    int x = Integer.parseInt(t1.getText());

    int y = Integer.parseInt(t2.getText());

    if (e.getSource() == b1) {

      t3.setText(String.valueOf(x + y));

    }

    if (e.getSource() == b2) {

      t3.setText(String.valueOf(x - y));

    }

    if (e.getSource() == b3) {

      t3.setText(String.valueOf(x / y));

    }

    if (e.getSource() == b4) {

      t3.setText(String.valueOf(x * y));

    }

  }

}

public class Main {

  public static void main(String args[]) {

    new Calculator();

  }

}

3. 数据库编程:

Java也可以用于编写数据库应用程序。Java提供了JDBC库来与数据库进行交互。开发人员可以通过JDBC连接到各种关系型数据库,如MySQL,PostgreSQL和Oracle。使用JDBC,开发人员可以执行各种操作,如查询,插入,更新和删除数据。

下面是连接MySQL数据库并执行查询的一个例子:


import java.sql.*;

public class Main {

  public static void main(String[] args) {

    String url = "jdbc:mysql://localhost:3306/mydatabase";

    String username = "root";

    String password = "password";

    try (Connection connection = DriverManager.getConnection(url, username, password)) {

      String sql = "SELECT * FROM users";

      Statement statement = connection.createStatement();

      ResultSet resultSet = statement.executeQuery(sql);

      while (resultSet.next()) {

        int id = resultSet.getInt("id");

        String name = resultSet.getString("name");

        String email = resultSet.getString("email");

        System.out.println(String.format("%d %s %s", id, name, email));

      }

    } catch (Exception e) {

      System.out.println(e.getMessage());

    }

  }

}

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复