Exercise 1
Circulation means, inserting data into the database 100, and the query results observed in mysql-front
1 package jdbc; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.SQLException; 6 import java.sql.Statement; 7 8 public class TestJDBC { 9 public static void main(String[] args) { 10 // 初始化驱动 11 try { 12 Class.forName("com.mysql.jdbc.Driver"); 13 } catch (ClassNotFoundException e) { 14 // the TODO Auto-Generated Block the catch 15 e.printStackTrace (); 16 } . 17 // use the try-with-resource manner automatically close the connection, because the Connection and Statement interfaces are implemented AutoCloseable 18 is the try ( . 19 Connection C = the DriverManager. the getConnection ( "JDBC: MySQL: //127.0.0.1:? 3306 / how2java = UTF-characterEncoding. 8" , 20 is "the root", "ADMIN" ); 21 is the Statement S = c.createStatement ();) { 22 is s.execute ( "TRUNCATE Table Hero" ); 23 is String name = null ; 24 float hp = 100.0f; 25 int damage = 100; 26 for (int i = 0; i < 100; i++) { 27 name = "\'" + "Hero-" + String.valueOf(i + 1) + "\'"; //注意格式,必须单引号' 28 String sql = "insert into hero values(null," + name + "," + (hp++) + "," + (damage++) + ")"; 29 s.execute(sql); 30 } 31 } catch (SQLException e) { 32 // TODO Auto-generated catch block 33 e.printStackTrace (); 34 } 35 36 } 37 }
Renderings: