privateConnection connection;privateString sql;@Beforepublicvoidinto()throwsException{
connection =DriverManager.getConnection("jdbc:mysql://localhost:3306/bailiban","root","123456");}@TestpublicvoidtestAdd()throwsException{
sql ="insert into bailiban.hero VALUES (null,'哈哈','男',16)";}@TestpublicvoidtestUpdate()throwsSQLException{
sql ="update bailiban.hero set age = 13 where name = '露娜'";}@TestpublicvoidtestDelete()throwsSQLException{
sql ="delete from bailiban.hero where id = 3";}@Afterpublicvoidend()throwsException{Statement statement = connection.createStatement();int i = statement.executeUpdate(sql);System.out.println(i);
statement.close();
connection.close();}}
2.创建工具类
publicstaticvoidmain(String[] args)throwsException{}publicstaticvoidupDate(String sql)throwsException{Connection conn =DriverManager.getConnection("jdbc:mysql://localhost:3306/bailiban","root","123456");Statement stat = conn.createStatement();int i = stat.executeUpdate(sql);System.out.println(i);
stat.close();
conn.close();}publicstaticvoidqueryOne(String sql)throwsException{Connection conn =DriverManager.getConnection("jdbc:mysql://localhost:3306/bailiban","root","123456");Statement stat = conn.createStatement();ResultSet query = stat.executeQuery(sql);if(query.next()){String id = query.getString("id");String name = query.getString("name");String sex = query.getString("sex");String age = query.getString("age");System.out.println(id+"--"+name+"--"+sex+"--"+age);}System.out.println(query.next());
stat.close();
conn.close();}publicstaticvoidqueryMore(String sql)throwsException{Connection conn =DriverManager.getConnection("jdbc:mysql://localhost:3306/bailiban","root","123456");Statement stat = conn.createStatement();ResultSet query = stat.executeQuery(sql);while(query.next()){String id = query.getString("id");String name = query.getString("name");String sex = query.getString("sex");String age = query.getString("age");System.out.println(id+"--"+name+"--"+sex+"--"+age);}System.out.println(query.next());
stat.close();
conn.close();}}