📄 insert2.jsp
字号:
<%@ page contentType="text/html; charset=gb2312" %>
<%@ page language="java" %>
<%@ page import="com.mysql.jdbc.Driver" %>
<%@ page import="java.sql.*" %>
<HTML>
<head>
<title>The DataBase test</title>
</head>
<BODY>
<%
//驱动程序名、数据库用户名、密码、数据库名、表名
String driverName="com.mysql.jdbc.Driver";
String username="root";
String password="yesterdayn";
String dbName="study";
String tableName="product";
//联结字符串
String url="jdbc:mysql://localhost/"+dbName+"?characterEncoding=GBK";
try{
//加载驱动程序
Class.forName("com.mysql.jdbc.Driver");
//建立同数据库的连接对象
Connection connection=DriverManager.getConnection(url,username,password);
String sql="INSERT INTO product VALUES(?,?,?,?,?,?,?)";
PreparedStatement ps=connection.prepareStatement(sql);
/*--------------------------------*/
ps.setInt(1,0); //AUTO_INCREMENT型列
ps.setString(2,"T001");
ps.setString(3,"长虹电视");
ps.setString(4,"四川长虹");
ps.setString(5,"长虹,引领3C未来");
ps.setInt(6,129);
ps.setFloat(7,1999);
ps.addBatch(); //添加批处理
/*--------------------------------*/
ps.setInt(1,0);
ps.setString(2,"T002");
ps.setString(3,"海信电视");
ps.setString(4,"青岛海信");
ps.setString(5,"海信,数字高清平板");
ps.setInt(6,131);
ps.setFloat(7,2999);
ps.addBatch();
/*--------------------------------*/
ps.setInt(1,0);
ps.setString(2,"M003");
ps.setString(3,"波导手机");
ps.setString(4,"波导");
ps.setString(5,"手机中的战斗机。");
ps.setInt(6,97);
ps.setFloat(7,999);
ps.addBatch();
/*--------------------------------*/
ps.executeBatch(); //执行批处理操作
ps.close();
connection.close();
out.print("向数据库插入记录成功!");
}
catch(SQLException e)
{
out.print(e.toString());
}
%>
</HTML>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -