您的位置:首页 > 数据库 > MySQL

Java初学者:图书管理小工具(MySQL版本)代码

2010-06-05 23:21 786 查看
1.Welcome

import java.sql.SQLException;
import java.util.Date;
public class Welcome {
/**
* @param args
* @throws SQLException
* @throws ClassNotFoundException
* @throws IllegalAccessException
* @throws InstantiationException
*/
public static void main(String[] args) throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
// TODO Auto-generated method stub
System.out.println("Welcome to visit software of book management!/n");
System.out.println("Now is "+new Date()+"/n");
Select s=new Select();
s.SelectService();
}


2.Select

import java.sql.SQLException;
import java.util.Scanner;
public class Select {

ReturnBook rb=new ReturnBook();
InquireBook ib=new InquireBook();
LendBook lb=new LendBook();
ShowAllBooks sab=new ShowAllBooks();
AddBook ab=new AddBook();
ShowLentBooks slb=new ShowLentBooks();
public void SelectService() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException
{
System.out.println("1.Show all the books/n2.Inquire book/n3.Lend book/n4.Return book/n5.Add book/n" +
"6.Show lent books");
System.out.print("Please select one from services listed above:");
Scanner s=new Scanner(System.in);
int selectNumber=s.nextInt();

if(selectNumber==1)
{
sab.ShowAll();
}
else if(selectNumber==2)
{
ib.Inquire();
}
else if(selectNumber==3)
{
lb.Lend();
}
else if(selectNumber==4)
{
rb.returnbook();
}
else if(selectNumber==5)
{
ab.add();
}
else if(selectNumber==6)
{
slb.ShowLent();
}
else
{
System.out.println("Input error,Please select one from services listed below again.");
Select ss=new Select();
ss.SelectService();
}
}
}


3.ShowAllBooks

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
public class ShowAllBooks {
public void ShowAll() throws SQLException, InstantiationException,
IllegalAccessException, ClassNotFoundException {
Connection conn = toMySQL.getConnection();
String s1="******************************************************************************";
String s2="------------------------------------------------------------------------------";
try {
Statement stat = conn.createStatement();
ResultSet result = stat.executeQuery("SELECT * FROM allbooks");

System.out.println(s1);
System.out.printf("%-35s%-25s%-30s"," Name","Author","Press");
System.out.println();
System.out.println(s1);

if (!result.isBeforeFirst())
System.out.println("There is no book!");
else {
while(result.next()) {

System.out.printf("%-30s%-25s%-30s"," "+result.getString(1),result.getString(2),result.getString(3));
System.out.println();
}
System.out.println(s2);
}
result.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
conn.close();
}
System.out.print("b)Back OR e)Exit:");
Scanner s = new Scanner(System.in);
String which = s.next();
if (which.equalsIgnoreCase("b")) {
Select st = new Select();
st.SelectService();
} else {
return;
}
}
}


4.InquireBook

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
public class InquireBook {
Scanner s = new Scanner(System.in);
public void Inquire() throws SQLException, InstantiationException,
IllegalAccessException, ClassNotFoundException {
Connection conn = toMySQL.getConnection();

String s1="************************************************************************";
String s2="------------------------------------------------------------------------";

try {
System.out.print("Input the book's name:");
String name = s.nextLine();
Statement stat = conn.createStatement();
ResultSet result = stat
.executeQuery("SELECT * FROM allbooks WHERE bookname LIKE '%"
+name + "%'");

System.out.println(s1);
System.out.printf("%-35s%-25s%-30s"," Name","Author","Press");
System.out.println();
System.out.println(s1);

if (!result.isBeforeFirst())
System.out.println("There is no book you want.");
else {
while (result.next()) {

System.out.printf("%-30s%-25s%-30s", " "+result.getString(1),result.getString(2),result.getString(3));
System.out.println();
}
System.out.println(s2);
}
result.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
conn.close();
}
System.out.print("a)Again OR b)Back OR e)Exit:");
String which = s.next();
if (which.equalsIgnoreCase("a")) {
InquireBook ib = new InquireBook();
ib.Inquire();
} else if (which.equalsIgnoreCase("b")) {
Select s = new Select();
s.SelectService();
} else {
return;
}
}
}


5.LendBook

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.Scanner;
public class LendBook {
Scanner s=new Scanner(System.in);
public void Lend() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException
{
Connection conn = toMySQL.getConnection();
try {
System.out.print("Please input name of the book you will lend:");
String name = s.nextLine();

System.out.print("Please input who:");
String who=s.nextLine();

Statement stat = conn.createStatement();
stat.executeUpdate("INSERT INTO lentbooks VALUES(" + "'"+name+"','" +who+"','"+ new Date()+"'"+ ")");
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
conn.close();
}

System.out.print("a)Again OR b)Back OR e)Exit:");
String which=s.nextLine();
if(which.equalsIgnoreCase("a"))
{
LendBook lb=new LendBook();
lb.Lend();
}
else if(which.equalsIgnoreCase("b"))
{
Select ss=new Select();
ss.SelectService();
}
}
}


6.ReturnBook

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
public class ReturnBook {
Scanner s = new Scanner(System.in);
public void returnbook() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException
{
System.out.print("Please input the book's name:");
String name=s.nextLine();

Connection conn=toMySQL.getConnection();
try
{
Statement stat=conn.createStatement();
stat.executeUpdate("DELETE FROM lentbooks WHERE bookname="+"'"+name+"'");
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
conn.close();
}

System.out.print("b)Back OR e)Exit:");
Scanner s=new Scanner(System.in);
String which=s.next();
if(which.equalsIgnoreCase("b"))
{
Select st=new Select();
st.SelectService();
}
else
{
return;
}
}
}


7.AddBook

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
public class AddBook {
Scanner s = new Scanner(System.in);
public void add() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
System.out.print("Please input the book's name:");
String name = s.nextLine();
System.out.print("Please input the author:");
String author = s.nextLine();
System.out.print("Please input the press:");
String press = s.nextLine();
Connection conn = toMySQL.getConnection();
try {
Statement stat = conn.createStatement();
stat.executeUpdate("INSERT INTO allbooks VALUES(" + "'"+name+"','" +
author+"','"+ press+"'"+ ")");
} catch (Exception e) {
e.printStackTrace();
} finally {
conn.close();
}
System.out.print("a).Again OR b)Back OR e)Exit:");
String which = s.next();
if (which.equalsIgnoreCase("a")) {
AddBook ab = new AddBook();
ab.add();
} else if (which.equalsIgnoreCase("b")) {
Select st = new Select();
st.SelectService();
} else {
return;
}
}
}


8.ShowLentBooks

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
public class ShowLentBooks {
public void ShowLent() throws SQLException, InstantiationException,
IllegalAccessException, ClassNotFoundException {
Connection conn = toMySQL.getConnection();
String s1 = "*********************************************************************************************";
String s2 = "---------------------------------------------------------------------------------------------";
try {
Statement stat = conn.createStatement();
ResultSet result = stat.executeQuery("SELECT * FROM lentbooks");

System.out.println(s1);
System.out.printf("%-38s%-25s%-30s", " Name","Who","Time");
System.out.println("/n"+s1);

if (!result.isBeforeFirst())
System.out.println("No book has been lent!");
else {
while (result.next()) {
String s21=result.getString(1),s22=result.getString(2),s23=result.getString(3);
System.out.printf("%-30s%-25s%-30s"," "+s21,s22,s23);
System.out.println();
}
System.out.println(s2);
}
result.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
conn.close();
}
System.out.print("b)Back OR e)Exit:");
Scanner ss = new Scanner(System.in);
String which = ss.next();
if (which.equalsIgnoreCase("b")) {
Select st = new Select();
st.SelectService();
} else {
return;
}
}
}


9.toMySQL

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class toMySQL {
public static Connection getConnection() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {

Class.forName("com.mysql.jdbc.Driver").newInstance();

String url="jdbc:mysql://localhost/BookManagement?useUnicode=true&characterEncoding=utf-8";
String username = "root";
String password = "06122553";
return DriverManager.getConnection(url, username, password);
}
}


代码中对于中文对齐输出的问题还是没有解决

ShowAllbooks,ShowLentBooks等类中在输出时,根据bookname字段中字符串的长度进行排序,使用SELECT * FROM * ORDER BY LENGTH(*)

如果字段的字符类型是char,而不是varchar,则应使用:SELECT * FROM * ORDER BY CHAR_LENGTH(*)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: