您的位置:首页 > 编程语言 > Java开发

MyEclipse------PreparedStatement使用方法

2016-03-26 23:29 399 查看
testPreparedStatement.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<%@page import="java.sql.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'testPrepareStatement.jsp' starting page</title>
</head>

<body>
<%
String url="jdbc:mysql://localhost:3306/student?useSSL=true";
String useName="root";
String password="2277092";

Connection conn=null;
Statement stmt=null;

try{
Class.forName("com.mysql.jdbc.Driver");
//out.print("加载驱动类成功");
}
catch(ClassNotFoundException e){
out.print(e);
}

try{
conn=DriverManager.getConnection(url,useName,password);

PreparedStatement pstmtInsert=conn.prepareStatement(
"insert into classinfo(no,name,sex,major,phone,address,age,scoure) values(?,?,?,?,?,?,?,?);");

pstmtInsert.setInt(1, 6);
pstmtInsert.setString(2,"沐白");
pstmtInsert.setString(3, "男");
pstmtInsert.setString(4,"网络工程");
pstmtInsert.setString(5, "2277092");
pstmtInsert.setString(6,"五邑大学");
pstmtInsert.setInt(7, 23);
pstmtInsert.setInt(8,90);

pstmtInsert.executeUpdate();

pstmtInsert.close();
out.print("插入数据成功");
}
catch(SQLException e){
out.print(e);
}
finally{
try{
if(conn!=null)
conn.close();
}
catch(Exception e){
out.print("断开数据库连接出现异常");
}
}
%>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: