new30.jsp

<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.SQLException"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String driverName = "com.mysql.jdbc.Driver";
String userName = "root";
String userPwd = "mysql123";
String dbName = "test";
String url = "jdbc:mysql://localhost:3306/" + dbName + "?useUnicode=true&characterEncoding=utf8";
Class.forName(driverName);
Connection conn = DriverManager.getConnection(url,userName,userPwd);

String sql = "insert into stu_info(name,sex,age,weight,height) values(?,?,?,?,?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "张三");
pstmt.setString(2, "男");
pstmt.setInt(3, 20);
pstmt.setFloat(4, 70);
pstmt.setFloat(5, 175);
int n = pstmt.executeUpdate();
if(n == 1)
{
out.print("数据插入成功!");
}
else
{
out.print("数据插入失败!");
}
if(pstmt != null)
{
pstmt.close();
}
if(conn != null)
{
conn.close();
}

%>
</body>
</html>