21xrx.com
2024-12-23 02:24:36 Monday
登录
文章检索 我的文章 写文章
Java实训报告总结 - 带你了解Java实训内容和代码案例
2023-06-18 21:22:54 深夜i     --     --
Java实训 Eclipse MySQL 面向对象 JDBC

Java实训报告总结 - 带你了解Java实训内容和代码案例

在本次Java实训中,我深入学习了Java的基础知识和面向对象编程,学会了使用Eclipse开发工具进行Java编程,还学习了使用MySQL数据库进行数据存储和管理。在实训的过程中,我完成了以下几个代码案例:

1. Java基础知识练习:编写一个程序,输出所有小于100的质数。


public class PrimeNumber {

 public static void main(String[] args) {

  for (int i = 2; i < 100; i++) {

   boolean isPrime = true;

   for (int j = 2; j <= Math.sqrt(i); j++) {

    if (i % j == 0)

     isPrime = false;

     break;

    

   }

   if (isPrime) {

    System.out.println(i);

   }

  }

 }

}

2. 面向对象编程实践:设计一个图书管理系统,可以添加、删除、查看和借阅图书。


public class LibrarySystem {

 private Map books;

 public void addBook(Book book) {

  books.put(book.getIsbn(), book);

 }

 public void removeBook(String isbn) {

  books.remove(isbn);

 }

 public Book getBook(String isbn) {

  return books.get(isbn);

 }

 public void borrow(String isbn) {

  Book book = books.get(isbn);

  if (book != null) {

   book.setBorrowed(true);

  }

 }

 public void returnBook(String isbn) {

  Book book = books.get(isbn);

  if (book != null) {

   book.setBorrowed(false);

  }

 }

}

public class Book {

 private String isbn;

 private String title;

 private boolean borrowed;

 public Book(String isbn, String title)

  this.isbn = isbn;

  this.title = title;

  borrowed = false;

 

 public String getIsbn()

  return isbn;

 

 public String getTitle()

  return title;

 

 public boolean isBorrowed()

  return borrowed;

 

 public void setBorrowed(boolean borrowed)

  this.borrowed = borrowed;

 

}

3. 数据库管理练习:使用Java JDBC连接MySQL数据库,实现对学生信息的增删改查功能。


public class StudentDAO {

 private Connection conn;

 public StudentDAO(Connection conn)

  this.conn = conn;

 

 public void addStudent(Student student) {

  try {

   PreparedStatement ps = conn.prepareStatement("INSERT INTO student VALUES (?, ?, ?)");

   ps.setString(1, student.getId());

   ps.setString(2, student.getName());

   ps.setInt(3, student.getAge());

   ps.executeUpdate();

  } catch (SQLException e) {

   e.printStackTrace();

  }

 }

 public void removeStudent(String id) {

  try {

   PreparedStatement ps = conn.prepareStatement("DELETE FROM student WHERE id=?");

   ps.setString(1, id);

   ps.executeUpdate();

  } catch (SQLException e) {

   e.printStackTrace();

  }

 }

 public void updateStudent(Student student) {

  try {

   PreparedStatement ps = conn.prepareStatement("UPDATE student SET name=?, age=? WHERE id=?");

   ps.setString(1, student.getName());

   ps.setInt(2, student.getAge());

   ps.setString(3, student.getId());

   ps.executeUpdate();

  } catch (SQLException e) {

   e.printStackTrace();

  }

 }

 public List getStudents() {

  List students = new ArrayList<>();

  try {

   Statement stmt = conn.createStatement();

   ResultSet rs = stmt.executeQuery("SELECT * FROM student");

   while (rs.next()) {

    String id = rs.getString("id");

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

    int age = rs.getInt("age");

    students.add(new Student(id, name, age));

   }

   rs.close();

   stmt.close();

  } catch (SQLException e) {

   e.printStackTrace();

  }

  return students;

 }

}

public class Student {

 private String id;

 private String name;

 private int age;

 public Student(String id, String name, int age) {

  this.id = id;

  this.name = name;

  this.age = age;

 }

 public String getId() {

  return id;

 }

 public String getName() {

  return name;

 }

 public int getAge() {

  return age;

 }

}

通过本次Java实训,我不仅掌握了Java编程的基本语法和编程思想,还学会了如何使用各种工具进行开发和调试。接下来,我会继续深入学习Java的高级应用和框架,为自己的软件开发之路打下坚实的基础。

  
  

评论区

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