Add, delete, modify, and query Mysql Databases
Create a test database in the mysql database before running the program, as shown in:
The specific implementation program is as follows:
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. required; 8 9 public class News {10 public static void main (String [] args) {11 System. out. println ("<use ArrayList to implement a news management System >>>>>"); 12 13 while (true) {14 rows updated = new partition (System. in); 15 System. out 16. println ("1. Query all news \ n2. Add news \ n3. Delete news \ n4. query news \ n5by title. Modify news by title \ n0. exit \ n select a function ): "); 17 int selected = success. nextInt (); 18 19 switch (selected) {20 case 1: 21 reads (); 22 break; 23 24 case 2: 25 System. out. println ("Enter the news title:"); 26 String title = response. next (); 27 System. out. println ("Enter the news content:"); 28 String content = response. next (); 29 System. out. println ("Enter the news remarks:"); 30 String remark = comment. next (); 31 32 add (title, content, remark); 33 break; 34 35 case 3: 36 System. out. println ("Enter the news title:"); 37 String title2 = response. next (); 38 delete (title2); 39 break; 40 41 case 4: 42 System. out. println ("Enter the news title:"); 43 String title1 = response. next (); 44 read (title1); 45 break; 46 47 case 5: 48 System. out. println ("Enter the news title:"); 49 String title3 = response. next (); 50 update (title3); 51 break; 52 53 case 0: 54 SC Anner. close (); 55 System. exit (0); 56 break; 57 58 default: 59 System. out. println ("input error, please enter again:"); 60 break; 61} 62} 63} 64 65/** 66 * Add news method 67*68 * @ param title 69 * title 70 * @ param content 71 * content 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 use R = "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.exe cuteUpdate (SQL); 84 System. out. println (row> 0? "Added successfully": "failed to add"); 85} catch (Exception e) {86 e. printStackTrace (); 87} 88} 89 90/** 91 * define all the query Methods 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. createS Tatement (); 101 String SQL = "select * from news"; 102 ResultSet list = st.exe cuteQuery (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" + content110 + "\ t" + remark); 111} 112} 113} catch (Exception e) {114 e. printStackTrace (); 115} 116} 117 118/** 119 * Definition of the method for querying news by title 120*121 * @ param title1122 * news title 123 */124 p Ublic 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.exe cu TeQuery (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 * Definition of the method for deleting news by title 149*150 * @ param title151 * news method 152 */153 public stat Ic 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.exe cuteUpdate (SQL), 163 System. out. println (row> 0? "Deleted successfully": "failed to delete"); 164} catch (Exception e) {165 e. printStackTrace (); 166} 167} 168 169/** 170 * Definition of the method for modifying news by title 171*172 * @ param title173 * news title 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. getConnec Tion (url, user, password); 183 Statement st = conn. createStatement (); 184 String SQL = "select * from news where title = '" + title +" '"; 185 ResultSet list = st.exe cuteQuery (SQL); 186 if (list. next () {187 release sca = new release (System. in); 188 System. out. println ("enter a new news title:"); 189 String title4 = sca. next (); 190 System. out. println ("Please enter the news content:"); 191 String content = sca. next (); 192 System. out. println ("Enter Input remarks: "); 193 String remark = sca. next (); 194 195 String sql1 = "update news set title = '" + title4196 + "', content = '" + content + "', remark = '"+ remark +" 'where title =' "+ title +" '"; 197 System. out. println (sql1); 198 int u = st.exe cuteUpdate (sql1); 199 System. out. println (u> 0? "Modification successful": "modification failed"); 200 sca. close (); 201} 202} catch (Exception e) {203 e. printStackTrace (); 204} 205} 206}