课堂派数据库作业

package com.company;

import java.sql.*;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        int flag=0;
        Scanner sc=new Scanner(System.in);
        String book=sc.next();
        Connection conn = null;
        Statement state = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        String jdbc = "jdbc:mysql://localhost:3306/bookstore";
        try {
            conn = DriverManager.getConnection(jdbc, "root", "123");
            state = conn.createStatement();
            String sql = "select * from books";
            rs = state.executeQuery(sql);
            while(rs.next()){
                int i=1;
                if(book.equals(rs.getString(i))){
                    flag++;
                    System.out.println(rs.getString(i)+" "+rs.getString(2)+" "+rs.getString(3)+" "+rs.getString(4)+" "+rs.getInt(5));
                    }
                else {
                    i++;
                }
            }
            if(flag==0){
                System.out.println("查无此书");
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        try {
            conn.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        String jdb = "jdbc:mysql://localhost:3306/bookstore";
        try {
            conn = DriverManager.getConnection(jdb, "root", "123");
            state = conn.createStatement();
            String sql = "select * from books";
            rs = state.executeQuery(sql);
            while(rs.next()){
                System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3)+" "+rs.getString(4)+" "+rs.getInt(5));
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        try {
            conn.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
上一篇:JDBC MySQL开启事务


下一篇:重定向解决浏览器刷新后数据多次存入的问题