用資料庫撰寫快遞e站(本文只寫了idea方面的代碼)
隨著快遞業的不斷發展,快遞e站也越來越多,我們來撰寫一個簡單的快遞e站小程式,本文就介紹了如何用資料庫知識撰寫快遞e站,
##成品如下:
一、IDEA如何連接資料庫
第一種方法:直接在方法體中增加連接資訊
優點:如果僅使用一次資料庫操作可選擇
缺點:多次資料庫操作每次都需要寫,麻煩
public void select() {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
//1.注冊驅動
Class.forName("com.mysql.jdbc.Driver");
//2.定義sql
String sql = "select * from kuaidi";
//3.獲取conn
conn = DriverManager.getConnection("jdbc:mysql:///kuaidi", "root", "123");
//4.獲取執行sql物件Statement
stmt = conn.createStatement();
//5.執行sql
rs = stmt.executeQuery(sql);
//6處理結果
while (rs.next()) {
int danhao = rs.getInt(1);
int qujianma = rs.getInt(2);
String gongsi = rs.getString("gongsi");
String guizi = rs.getString(4);
System.out.println("單號為:" + danhao + " " + "取件碼為:" + qujianma + " " + "公司是:" + gongsi + " " + "柜子在第" + guizi + "個");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (rs != null) {
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
方法二:
建立一個JDBCHelper和一個存盤資料庫賬號密碼的Properties,來幫助快速加載驅動以及釋放記憶體
優點:只需要寫一次,用的時候呼叫即可
缺點:一次要寫很多

釋放記憶體的時候可能傳入兩個或者三個引數需要釋放,所以用多載形式來解決
private static String url;
private static String user;
private static String password;
private static String driver;
/**
* 檔案的讀取,只需要讀取一次即可
*/
static {
//讀取資源檔案,并獲取值
try {
//1.創建properties集合類
Properties pro = new Properties();
//2.加載檔案
//獲取src路徑下的檔案的方式--->ClassLoader類加載器
ClassLoader classLoader = JDBCHelper.class.getClassLoader();
URL res = classLoader.getResource("jdbc.properties");
String path = res.getPath();
//pro.load(new FileReader("src/jdbc.properties"));
pro.load(new FileReader(path));
url = pro.getProperty("url");
user = pro.getProperty("user");
password = pro.getProperty("password");
driver = pro.getProperty("driver");
Class.forName(driver);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* 獲取連接
*
* @return連接物件
*/
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, user, password);
}
/**
* 釋放資源
*
* @param stmt
* @param conn
*/
public static void close(Statement stmt, Connection conn) {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
//釋放記憶體
public static void close(ResultSet rs, Statement stmt, Connection conn) {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (rs != null) {
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
二、方法代碼的實作
1.快遞員增加快遞
代碼如下:
public class Add {
Connection conn = null;
Statement stmt = null;
Random r= new Random();
public void addq(int danhao,String gongsi){
try {
conn = JDBCHelper.getConnection();
int qujianma = r.nextInt((999999)+1);
String guizi = Integer.toString(r.nextInt(100)+1);
String sql = "insert into kuaidi values(" + danhao + "," + qujianma+",'"+gongsi+"','"+guizi+"')";
stmt = conn.createStatement();
int a = stmt.executeUpdate(sql);
System.out.println("取件碼為:"+qujianma);
if(a>0){
System.out.println("添加成功");
}
else {
System.out.println("添加失敗");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
2.快遞員洗掉快遞
代碼如下:
public class Delete {
Connection conn = null;
Statement stmt = null;
public void delete(int danhao) {
try {
conn = JDBCHelper.getConnection();
String sql = "delete from kuaidi where danhao = "+danhao;
stmt = conn.createStatement();
int a = stmt.executeUpdate(sql);
if(a>0){
System.out.println("洗掉成功");
}
else {
System.out.println("洗掉失敗");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
finally {
JDBCHelper.close(stmt,conn);
}
}
主要內容代碼:
public class Imp {
public static void main(String[] args) {
kuaidi();
}
public static void kuaidi() {
System.out.println("====歡迎使用新職課快遞柜====");
Scanner sc = new Scanner(System.in);
Random r = new Random();
while (true) {
System.out.println("請輸入你的身份:1-快遞員,2-用戶");
try {
int a = sc.nextInt();
if (a < 1 || a > 2) {
System.out.println("輸入有誤,請重新輸入");
} else if (a == 1) {
System.out.println("====歡迎使用新職課快遞柜====");
System.out.println("請選擇操作:1-存快遞 2-洗掉快遞 3-修改快遞資訊 4-查看所有快遞");
int b = sc.nextInt();
switch (b) {
case 1: {
System.out.println("====歡迎使用新職課快遞柜====");
System.out.println("請輸入快遞單號:");
int danhao = sc.nextInt();
System.out.println("請輸入公司名稱:");
String gongsi = sc.next();
Add add = new Add();
add.addq(danhao, gongsi);
break;
}
case 2: {
System.out.println("====歡迎使用新職課快遞柜====");
System.out.print("請輸入要洗掉的訂單號:");
int dingdan = sc.nextInt();
Delete delete = new Delete();
delete.delete(dingdan);
break;
}
case 3: {
System.out.println("====歡迎使用新職課快遞柜====");
System.out.print("請輸入要修改的訂單號:");
int danhao = sc.nextInt();
Alter al = new Alter();
boolean q = al.select(danhao);
if (q = true) {
System.out.println("請輸入更改后的單號");
int afdanhao = sc.nextInt();
al.alter(afdanhao, danhao);
} else {
System.out.println("請核實訂單號");
}
break;
}
case 4: {
System.out.println("====歡迎使用新職課快遞柜====");
SelectAll s = new SelectAll();
s.select();
}
}
} else if (a == 2) {
System.out.println("====歡迎使用新職課快遞柜====");
System.out.println("請輸入取件碼");
int qujianma = sc.nextInt();
Take t = new Take();
t.take(qujianma);
}
} catch (InputMismatchException e) {
System.out.println();
System.out.println("請輸入數字序號!!!!!!!!");
System.out.println();
kuaidi();
}
}
}
}
別的幾個代碼塊都大同小異,只需要修改sql陳述句即可
如有需要全部代碼,或者有疑問的同學私信我就可以了
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/246163.html
標籤:java
