十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
package com.bms;
创新互联建站于2013年创立,是专业互联网技术服务公司,拥有项目成都做网站、网站设计网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元潮阳做网站,已为上家服务,为潮阳各地企业和个人服务,联系电话:028-86922220
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
// book对象
public class Book {
private String bId; // 编号
private String bName; // 书名
// getset方法
public String getbId() {
return bId;
}
public void setbId(String bId) {
this.bId = bId;
}
public String getbName() {
return bName;
}
public void setbName(String bName) {
this.bName = bName;
}
//构造方法
public Book() {
}
public Book(String bId, String bName) {
this.bId = bId;
this.bName = bName;
}
/*
* 增加
* */
public static ListBook add(ListBook list) {
Scanner sn = new Scanner(System.in);
System.out.print("请输入编号:");
String bid = sn.next();
System.out.print("请输入名称:");
String bName = sn.next();
Book book = new Book(bid, bName);
for (Book b : list) {
if (b.bId.equals(book.bId)) {
System.out.println("编号重复,请重新输入!");
return list;
}
}
list.add(book);
System.out.println("添加成功!");
return list;
}
/*
* 查询
* */
public static void query(ListBook list) {
System.out.println("编号\t书名");
for (Book b : list) {
System.out.println(b.getbId() + "\t" + b.getbName());
}
}
/*
* 修改
* */
public static void update(ListBook list) {
query(list);
Scanner sc = new Scanner(System.in); // 键盘输入的对象
System.out.print("请输入编号:");
String s = sc.next();
Integer id = null;
for (int i = 0; i list.size(); i++) {
id = list.get(i).getbId().equals(s) ? i : null;
}
if (id == null) {
System.out.println("输入的编号不存在,请重新选择!");
return;
}
System.out.print("请输入新的书名:");
String newName = sc.next();
list.get(id).setbName(newName);
System.out.print("修改成功!");
}
/*
* 删除
* */
public static void del(ListBook list) {
query(list);
Scanner sc = new Scanner(System.in); // 键盘输入的对象
System.out.print("请输入编号:");
String s = sc.next();
for (int i = 0; i list.size(); i++) {
if (list.get(i).getbId().equals(s)) {
list.remove(i);
return;
}
}
System.out.println("输入的编号不存在,请重新选择!");
}
}
/*
* 测试*/
class Test {
public static void main(String[] args) {
ListBook bookList = new ArrayList(); // 存放所有图书的列表
bookList.add(new Book("1", "Java 基础")); // 图书的列表添加一本图书
System.out.print("欢迎进入图书管理系统,");
boolean b = true;
while (b) {
System.out.print("请选择:\n1.增加\n2.查询\n3.修改\n4.删除\n5.退出\n(输入序号):");
Scanner sn = new Scanner(System.in); // 键盘输入的对象
String select = sn.next();
switch (select) {
case "1":
System.out.println("您选择了增加");
Book.add(bookList);
break;
case "2":
System.out.println("您选择了查询:");
Book.query(bookList);
break;
case "3":
System.out.println("您选择了修改");
Book.update(bookList);
break;
case "4":
System.out.println("您选择了删除");
Book.del(bookList);
break;
case "5":
System.out.println("您选择了退出");
b = false;
System.out.println("退出程序!");
break;
default:
System.out.println("输入错误的序号,请重新输入");
break;
}
}
}
}
点击电脑的开始。
首先点击电脑的开始,找到QLServer配置管理器,将SQLServer网络配置中的SQLEXPRESS的协议如下图中的三个服务改为启用,然后点击TCPIP协议,修改动态端口为1433,重启服务器才能生效,首先建立一个java项目,命名为TestDB然后右键点击TestDB选择buildpath然后找到AddExternalJARs,找到下载的sqljdbc的包,打开即可,然后项目的referencedlibraries目录下能看到导入的包,新建一个包database包中再建立TestConect执行以下代码,代码执行结果和数据库中查询的结果一样。
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class BookWork {
static ListBook data;
private static Scanner input;
public static void main(String[] args) {
if (!initBook("d:/book.txt")) {
System.out.println("初始图书列表失败 ..");
return;
}
input = new Scanner(System.in);
while (true) {
try {
System.out.println("请输入操作:");
System.out.println("1.找书 2.删除图书 3.退出");
int number = Integer.parseInt(input.next());
if (number == 1) {
findBook();
} else if (number == 2) {
delBook();
} else if (number == 3) {
System.out.println("退出");
break;
} else {
System.out.println("这个不是我要的...重来...");
System.out.println();
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("这个不是我要的...重来...");
System.out.println();
}
}
}
private static void delBook() {
System.out.println("请输入要删除的书名或编号:");
String key = input.next();
if (key != null !key.equals("")) {
for (Book book : data) {
if (book.number.equals(key) || book.name.contains(key)) {
data.remove(book);
System.out.println(" 图书 " + book.toString() + " 已删除");
return;
}
}
}
System.out.println("没有您要删除的");
}
private static void findBook() {
System.out.println("请输入要查找的书名或编号:");
String key = input.next();
if (key != null !key.equals("")) {
for (Book book : data) {
if (book.number.equals(key) || book.name.contains(key)) {
System.out.println("找到了 图书 " + book.toString());
return;
}
}
}
System.out.println("没有您要找的");
}
private static boolean initBook(String string) {
try {
System.out.println("图书导入中...");
System.out.println("列表文件 -- " + string);
File file = new File(string);
if (!file.exists()) {
return false;
}
data = new ArrayListBook();
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
String line = "";
while ((line = bufferedReader.readLine()) != null) {
String[] strings = line.split(",");
Book b = new Book(strings[0], strings[1]);
data.add(b);
System.out.println("导入" + b.toString());
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
public static class Book {
String number;
String name;
public Book(String number, String name) {
super();
this.number = number;
this.name = name;
}
@Override
public String toString() {
return "Book [编码:" + number + ", 名称:" + name + "]";
}
}
}
001,金瓶梅
002,杂事秘辛
003,飞燕外传
004,控鹤监秘记
005,汉宫春色