Java课程设计:校园导游系统
生活随笔
收集整理的這篇文章主要介紹了
Java课程设计:校园导游系统
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
校園導游系統
- 內容目錄:
- 設計要求
- 功能模塊分析
- 數據庫設計
- 數據庫連接
- 數據庫功能的實現
- gui的實現
內容目錄:
1. 設計要求 2. 功能分析設計要求
圖形化界面(GUI),編寫一個文本文件信息統計軟件 (1)顯示打開文件的基本信息(文件名、路徑、文件大小、最后修改日期) (2)顯示文件內容統計信息(行、字/單詞數) (3)顯示文件內容 (4)按照文件大小或最后修改時間排序 (5)按照文件名或者文件內容包含關鍵字模糊查詢功能模塊分析
1. 數據庫設計2. gui設計3. 數據庫連接4. 數據庫相關功能實現數據庫設計
1. 有校園建筑表校園建筑表 schooladd
CREATE TABLE `schooladd` (`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'primary key',`sname` varchar(20) DEFAULT NULL,`x` int(100) DEFAULT NULL,`y` int(100) DEFAULT NULL,PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 2. 有校園建筑信息表校園建筑信息表
CREATE TABLE `message` (`id` int(10) NOT NULL,`name` varchar(255) DEFAULT NULL,`message` varchar(255) DEFAULT NULL,PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci數據庫連接
利用jdbc,util和資源文件來連接數據庫JdbcUtil.java如下
package com.datasource.utils;import com.alibaba.druid.pool.DruidDataSourceFactory;import javax.sql.DataSource; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties;public class JdbcUtils {/*** 1. 定義成員變量DataSourse*/private static DataSource ds;static {try {//1. 加載配置文件Properties pro = new Properties();pro.load(JdbcUtils.class.getClassLoader().getResourceAsStream("druid.properties"));//2.獲取DataSourceds = DruidDataSourceFactory.createDataSource(pro);} catch (Exception e) {e.printStackTrace();}}/*** 獲取資源* @return ds.getConnection()*/public static Connection getConnection() throws SQLException{return ds.getConnection();}/*** 釋放資源*/public static void close(ResultSet rs, Statement stmt, Connection conn){if (rs != null){try {rs.close();} catch (SQLException e) {e.printStackTrace();}}if (stmt != null){try {stmt.close();} catch (SQLException e) {e.printStackTrace();}}if (conn != null){try {conn.close();} catch (SQLException e) {e.printStackTrace();}}}/*** 獲取連接池方法* @return ds*/public static DataSource getDataSource(){return ds;} }配置文件druid.properties如下:
使用的時mysql8.0數據庫,
數據庫功能的實現
通過JdbcTemplate和Jdbc來實現數據庫相關功能JdbcTemplate.java
package com.jdbcTemplate;import com.datasource.utils.JdbcUtils; import com.domain.SchoolAdd;import java.util.List; import java.util.Map; import java.util.Scanner;import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate;public class JdbcTemplateDemo {private static JdbcTemplate template = new JdbcTemplate(JdbcUtils.getDataSource());/*** 查詢schooladd表中的所有的數據*/public static void selectDate1() {//2.定義sqlString sql = "select * from schooladd ";//3.執行sqlList<SchoolAdd> list = template.query(sql,new BeanPropertyRowMapper<SchoolAdd>(SchoolAdd.class));for (SchoolAdd emp:list){System.out.println(emp);}}/*** 按id差尋所有數據*/public static void selectDateId() {//定義sql語句String sql = "select * from schooladd where sname=?";Scanner scanner = new Scanner(System.in);System.out.println("Enter the sname :");String sName = scanner.nextLine();Map<String, Object> map = template.queryForMap(sql,sName);System.out.println(map);scanner.close();}/*** 查詢建筑物的坐標*/public static double selectAdd(String sName1,String sName2) {String sql1 = "select x from schooladd where sname=?";String sql2 = "select y from schooladd where sname=?";int x1 = template.queryForObject(sql1,Integer.class,sName1);int y1 = template.queryForObject(sql2,Integer.class,sName1);int x2 = template.queryForObject(sql1,Integer.class,sName2);int y2 = template.queryForObject(sql2,Integer.class,sName2);return Math.sqrt((Math.pow((x1 - x2), 2)+Math.pow((y1-y2), 2)));}/*** 查詢簡介* @return 建筑簡介*/public static String selectMessage(String name) {String sql = "select message from message where name=?";return template.queryForObject(sql, String.class,name);} }SchoolAdd.java
package com.domain;public class SchoolAdd {private Integer id;private String sName;private Integer x;private Integer y;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getsName() {return sName;}public void setsName(String sName) {this.sName = sName;}public Integer getX() {return x;}public void setX(Integer x) {this.x = x;}public Integer getY() {return y;}public void setY(Integer y) {this.y = y;}@Overridepublic String toString() {return "SchoolAdd [id=" + id + ", sName=" + sName + ", x=" + x + ", y=" + y + "]";}}gui的實現
Start.java
package com.gui;import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent;import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem;public class Start extends JMenuBar{ /***開始菜單 */private static final long serialVersionUID = 1L;public Start() {// TODO Auto-generated constructor stubadd(createFileMenu()); //添加“文件”菜單add(createEditMenu()); //添加“編輯”菜單setVisible(true); }public static void main(String[] args) {JFrame frame=new JFrame("菜單欄");JLabel jlimage = new JLabel();ImageIcon icon = new ImageIcon("./img/school.jpg");icon.setImage(icon.getImage().getScaledInstance(540, 713, Image.SCALE_DEFAULT));jlimage.setIcon(icon);frame.add(jlimage);frame.setSize(540,713);frame.setJMenuBar(new Start());frame.setVisible(true);}//定義“簡介”菜單private JMenu createFileMenu(){JMenu menu=new JMenu("簡介(F)");menu.setMnemonic(KeyEvent.VK_F); //設置快速訪問符JMenuItem item=new JMenuItem("學校信息");item.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubSchoolMessage.runShowMessage("學校信息");}});menu.add(item);item=new JMenuItem("格致樓");item.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubSchoolMessage.runShowMessage("格致樓");}});menu.add(item);item=new JMenuItem("機電學院");item.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubSchoolMessage.runShowMessage("機電學院");}});menu.add(item);item=new JMenuItem("田徑場");item.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubSchoolMessage.runShowMessage("田徑場");}});menu.add(item);menu.addSeparator();item=new JMenuItem("退出");item.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubSystem.exit(0);}});menu.add(item);return menu;}//定義“編輯”菜單private JMenu createEditMenu(){JMenu menu=new JMenu("查詢(E)");menu.setMnemonic(KeyEvent.VK_E);JMenuItem item=new JMenuItem("查詢建筑信息(U)",KeyEvent.VK_U);item.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubSelectMessage.runShowMessage();}});menu.add(item);menu.addSeparator();item=new JMenuItem("查詢建筑之間的距離(U)",KeyEvent.VK_T);item.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubSelectLength.runShowMessage();}});menu.add(item);return menu;} }SchoolMessage.java
package com.gui;import java.awt.Font;import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.SwingConstants;import com.jdbcTemplate.JdbcTemplateDemo;public class SchoolMessage {private static void showMessage(String name) {JFrame frame = new JFrame("學校信息");String str = JdbcTemplateDemo.selectMessage(name);JLabel label = new JLabel(str);label.setFont(new Font("宋體",1,14));label.setHorizontalAlignment(SwingConstants.CENTER);label.setVerticalAlignment(SwingConstants.TOP);frame.add(label);frame.setSize(540,713);frame.setVisible(true);frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); }public static void runShowMessage(String name) {javax.swing.SwingUtilities.invokeLater(new Runnable() {@Overridepublic void run() {showMessage(name);}});} }SelectLength.java
測量兩地的距離
SelectMessage.java
按地名查詢地點信息
總結
以上是生活随笔為你收集整理的Java课程设计:校园导游系统的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ISIS(中间系统到中间系统)概述
- 下一篇: AUTOCAD(一)室内平面图绘制 第一