Java初学者:图书管理小工具(MySQL版本)代码
2010-06-05 23:21
786 查看
1.Welcome
2.Select
3.ShowAllBooks
4.InquireBook
5.LendBook
6.ReturnBook
7.AddBook
8.ShowLentBooks
9.toMySQL
代码中对于中文对齐输出的问题还是没有解决
ShowAllbooks,ShowLentBooks等类中在输出时,根据bookname字段中字符串的长度进行排序,使用SELECT * FROM * ORDER BY LENGTH(*)
如果字段的字符类型是char,而不是varchar,则应使用:SELECT * FROM * ORDER BY CHAR_LENGTH(*)
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(*)
相关文章推荐
- Java初学者:图书管理小工具代码
- 图书信息管理java实现代码
- Java代码管理工具SVN系列
- 推送、 数据统计、 代码版本管理工具
- java与 mysql6.0版本连接代码
- 【Java资源大全】SonarQube:开源的代码质量管理工具
- Java开发工具打包下载包括jdk各种版本以及eclipse mysql等多种工具
- 数据结构(JAVA版本)练习之集合 简易图书管理系统
- Sonar 1.11版本发布,Java代码质量管理平台
- JAVA 面向对象编程开发--------初学者---------------------彩票系统----图书管理系统
- Java版本:图书管理系统
- java连接MySQL版本的电话本管理系统
- 图书管理系统java代码实现
- SVN版本管理工具使用中常见的代码提交冲突问题的解决方法
- OS X中Java版本管理工具jEnv
- 我是个在校生,现在想用java作为前台代码,连接到SQL server数据库上去,项目是;图书管理系统,现在没有好的java源代码,各位大侠能不能帮个忙?
- Mac 下的 Java 版本管理工具 jenv
- 基于JAVA的图书管理系统(jsp+mysql+tomcat)
- Mac 下的 Java 版本管理工具 jenv
- 项目管理大法归档 - 思维导图、原型工具、接口测试、设计模式、版本管理、单元测试、持续集成、代码审查、Bug 跟踪