数据库
首页 > 数据库> > 课堂派数据库作业

课堂派数据库作业

作者:互联网

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();
        }
    }
}

标签:String,getString,rs,throwables,数据库,作业,printStackTrace,课堂,conn
来源: https://blog.csdn.net/qq_52746199/article/details/117337689