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>
相关文章推荐
- Java-ConcurrentHashMap源码分析
- JAVA多线程——线程运行时异常的处理
- JAVAEE 框架 2016-3-26
- java中的Arrays类
- 牛客网每日打卡-Java基础-20160326
- 学习javaEE每一天2016.3.26
- Hibernate配置及持久化操作(CRUD)
- java线程池ThreadPoolExecutor
- UTM 用户线程模型
- Java入门编程(作业)Time3实现总结(二)
- java之二分查找
- Spring Batch(5): Step详解
- Java——新IO 通道
- Spring boot 入门 实例
- Java-HashTable源码分析
- Java基础之List实现篇
- Java并发:线程安全的单例模式
- Spring框架的两个简化XML配置文件的p-namespace和c-namespace
- Java集合对象排序
- 弹出提示框的方式——java