以下是链接数据库之后往数据库中插入数据的全部代码:
绿色:从其他页面获取需要往数据库添加的数据
红色:定义需要插入的列,列的顺序需要和数据库中的顺序一致(注意单引号和双引号的位置)
String add_stu_num = request.getParameter("add_stu_num");
String add_stu_name = request.getParameter("add_stu_name");
String add_stu_sex = request.getParameter("add_stu_sex");
String add_stu_age = request.getParameter("add_stu_age");
String add_stu_weight = request.getParameter("add_stu_weight");
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/Stu_info_mage_Sys?useUnicode=true&CharacterEncoder=GBK";
con = DriverManager.getConnection(url, "root", "admin123");
stmt = con.createStatement();
String sql = "insert into stu_info_all values('"+add_stu_name+"','"+add_stu_sex+"','"+add_stu_num+"','"+add_stu_age+"','"+add_stu_weight+"')";
stmt.executeUpdate(sql);//更新数据库
stmt.close();//关闭