在運行程序之前需要在mysql數據庫中創建test數據庫,如下圖所示:
下面是具體是實現程序:
1 package News; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.ResultSet; 6 import java.sql.Statement; 7 import java.util.Scanner; 8 9 public class News { 10 public static void main(String[] args) { 11 System.out.println("< < < < < < 使用ArrayList實現新聞管理系統 > > > > > >"); 12 13 while (true) { 14 Scanner scanner = new Scanner(System.in); 15 System.out 16 .println("1. 查詢全部新聞\n2. 添加新聞\n3. 刪除新聞\n4. 按標題查詢新聞\n5. 按標題修改新聞 \n0.退出\n請選擇功能(1,2,3,4,5,0):"); 17 int selected = scanner.nextInt(); 18 19 switch (selected) { 20 case 1: 21 reads(); 22 break; 23 24 case 2: 25 System.out.println("請輸入新聞標題:"); 26 String title = scanner.next(); 27 System.out.println("請輸入新聞內容:"); 28 String content = scanner.next(); 29 System.out.println("請輸入新聞備注:"); 30 String remark = scanner.next(); 31 32 add(title, content,remark); 33 break; 34 35 case 3: 36 System.out.println("請輸入新聞標題:"); 37 String title2 = scanner.next(); 38 delete(title2); 39 break; 40 41 case 4: 42 System.out.println("請輸入新聞標題:"); 43 String title1 = scanner.next(); 44 read(title1); 45 break; 46 47 case 5: 48 System.out.println("請輸入新聞標題:"); 49 String title3 = scanner.next(); 50 update(title3); 51 break; 52 53 case 0: 54 scanner.close(); 55 System.exit(0); 56 break; 57 58 default: 59 System.out.println("輸入錯誤,請重新輸入:"); 60 break; 61 } 62 } 63 } 64 65 /** 66 * 定義添加新聞方法 67 * 68 * @param title 69 * 標題 70 * @param content 71 * 內容 72 */ 73 public static void add(String title, String content,String remark) { 74 try { 75 Class.forName("com.mysql.jdbc.Driver"); 76 String url = "jdbc:mysql://localhost:3306/test"; 77 String user = "root"; 78 String password = "root"; 79 Connection conn = DriverManager.getConnection(url, user, password); 80 Statement st = conn.createStatement(); 81 String sql = "insert into news values(null,'" + title + "','" 82 + content + "','"+ remark + "')"; 83 int row = st.executeUpdate(sql); 84 System.out.println(row > 0 ? "添加成功" : "添加失敗"); 85 } catch (Exception e) { 86 e.printStackTrace(); 87 } 88 } 89 90 /** 91 * 定義查詢全部方法 92 */ 93 public static void reads() { 94 try { 95 Class.forName("com.mysql.jdbc.Driver"); 96 String url = "jdbc:mysql://localhost:3306/test"; 97 String user = "root"; 98 String password = "root"; 99 Connection conn = DriverManager.getConnection(url, user, password); 100 Statement st = conn.createStatement(); 101 String sql = "select * from news"; 102 ResultSet list = st.executeQuery(sql); 103 if (list != null) { 104 while (list.next()) { 105 int id = list.getInt(1); 106 String title = list.getString(2); 107 String content = list.getString(3); 108 String remark = list.getString(4); 109 System.out.println(id + "\t" + title + "\t" + content 110 + "\t" + remark); 111 } 112 } 113 } catch (Exception e) { 114 e.printStackTrace(); 115 } 116 } 117 118 /** 119 * 定義根據標題查詢新聞的方法 120 * 121 * @param title1 122 * 新聞標題 123 */ 124 public static void read(String title1) { 125 try { 126 Class.forName("com.mysql.jdbc.Driver"); 127 String url = "jdbc:mysql://localhost/test"; 128 String user = "root"; 129 String password = "root"; 130 Connection conn = DriverManager.getConnection(url, user, password); 131 Statement st = conn.createStatement(); 132 String sql = "select * from news where title='" + title1 + "'"; 133 ResultSet row = st.executeQuery(sql); 134 if (row.next()) { 135 int id = row.getInt(1); 136 String title = row.getString(2); 137 String content = row.getString(3); 138 String remark = row.getString(4); 139 System.out.println(id + "\t" + title + "\t" + content + "\t" 140 + remark); 141 } 142 } catch (Exception e) { 143 e.printStackTrace(); 144 } 145 } 146 147 /** 148 * 定義按標題刪除新聞的方法 149 * 150 * @param title 151 * 新聞方法 152 */ 153 public static void delete(String title) { 154 try { 155 Class.forName("com.mysql.jdbc.Driver"); 156 String url = "jdbc:mysql://localhost:3306/test"; 157 String user = "root"; 158 String password = "root"; 159 Connection conn = DriverManager.getConnection(url, user, password); 160 Statement st = conn.createStatement(); 161 String sql = "delete from news where title='" + title + "'"; 162 int row = st.executeUpdate(sql); 163 System.out.println(row > 0 ? "刪除成功" : "刪除失敗"); 164 } catch (Exception e) { 165 e.printStackTrace(); 166 } 167 } 168 169 /** 170 * 定義根據標題修改新聞的方法 171 * 172 * @param title 173 * 新聞標題 174 */ 175 public static void update(String title) { 176 177 try { 178 Class.forName("com.mysql.jdbc.Driver"); 179 String url = "jdbc:mysql://localhost:3306/test"; 180 String user = "root"; 181 String password = "root"; 182 Connection conn = DriverManager.getConnection(url, user, password); 183 Statement st = conn.createStatement(); 184 String sql = "select * from news where title='" + title + "'"; 185 ResultSet list = st.executeQuery(sql); 186 if (list.next()) { 187 Scanner sca = new Scanner(System.in); 188 System.out.println("請輸入新的新聞標題:"); 189 String title4 = sca.next(); 190 System.out.println("請輸入新聞內容:"); 191 String content = sca.next(); 192 System.out.println("請輸入備注:"); 193 String remark = sca.next(); 194 195 String sql1 = "update news set title='" + title4 196 + "',content='" + content + "',remark='" + remark + "' where title='"+title+"'"; 197 System.out.println(sql1); 198 int u = st.executeUpdate(sql1); 199 System.out.println(u > 0 ? "修改成功" : "修改失敗"); 200 sca.close(); 201 } 202 } catch (Exception e) { 203 e.printStackTrace(); 204 } 205 } 206 }