new194.jsp
<%@page import="java.sql.PreparedStatement"%>
<%@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>
<%
Connection conn = null;
try
{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8";
String user = "root";
String pwd = "mysql123";
conn = DriverManager.getConnection(url,user,pwd);
String sql = "insert into sp_goods(shopName,freight,amount,address,goodsType) values(?,?,?,?,?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "苹果");
pstmt.setDouble(2, 12);
pstmt.setInt(3, 33);
pstmt.setString(4, "深圳");
pstmt.setInt(5, 3);
int iResult = pstmt.executeUpdate();
if(iResult != -1)
{
out.print("成功插入一条数据");
}
pstmt.close();
conn.close();
}
catch(Exception e)
{
out.print("添加数据失败" + e.getMessage());
}
%>
</body>
</html>