jsp 连接mysql数据库,新建数据表,更新表数据
2010-08-11 10:45
435 查看
1,jsp建立数据表
<html>
<head>
</head>
<body>
<%@ page contentType="text/html; charset=gb2312" %>
<%@ page language="java" %>
<%@ page import="com.mysql.jdbc.Driver" %>
<%@ page import="java.sql.*" %>
<%
//驱动程序名
String driverName="com.mysql.jdbc.Driver";
//数据库用户名
String userName="root";
//密码
String userPasswd="root";
//数据库名
String dbName="test";
//表名
String tableName="user";
//联结字符串
String url="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPasswd;
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection connection=DriverManager.getConnection(url);
Statement statement = connection.createStatement();
String sql="create table haisen(id int)";
int rs = statement.executeUpdate(sql);
statement.close();
connection.close();
int tryint[]=new int[2];
tryint[0]=1;
tryint[1]=2;
%>
</body>
<script>
var aaatry="<%=rs%>";
alert(aaatry);
</script>
</html>
2,jsp向数据库中插入数据
<html>
<head>
</head>
<body>
<%@ page contentType="text/html; charset=gb2312" %>
<%@ page language="java" %>
<%@ page import="com.mysql.jdbc.Driver" %>
<%@ page import="java.sql.*" %>
<%
//驱动程序名
String driverName="com.mysql.jdbc.Driver";
//数据库用户名
String userName="root";
//密码
String userPasswd="root";
//数据库名
String dbName="test";
//表名
String tableName="user";
//联结字符串
String url="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPasswd;
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection connection=DriverManager.getConnection(url);
Statement statement = connection.createStatement();
String sql="insert table user values('2','haisen')";
int rs = statement.executeUpdate(sql);
statement.close();
connection.close();
int tryint[]=new int[2];
tryint[0]=1;
tryint[1]=2;
%>
</body>
<script>
var aaatry="<%=rs%>";
alert(aaatry);
</script>
</html>
<html>
<head>
</head>
<body>
<%@ page contentType="text/html; charset=gb2312" %>
<%@ page language="java" %>
<%@ page import="com.mysql.jdbc.Driver" %>
<%@ page import="java.sql.*" %>
<%
//驱动程序名
String driverName="com.mysql.jdbc.Driver";
//数据库用户名
String userName="root";
//密码
String userPasswd="root";
//数据库名
String dbName="test";
//表名
String tableName="user";
//联结字符串
String url="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPasswd;
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection connection=DriverManager.getConnection(url);
Statement statement = connection.createStatement();
String sql="create table haisen(id int)";
int rs = statement.executeUpdate(sql);
statement.close();
connection.close();
int tryint[]=new int[2];
tryint[0]=1;
tryint[1]=2;
%>
</body>
<script>
var aaatry="<%=rs%>";
alert(aaatry);
</script>
</html>
2,jsp向数据库中插入数据
<html>
<head>
</head>
<body>
<%@ page contentType="text/html; charset=gb2312" %>
<%@ page language="java" %>
<%@ page import="com.mysql.jdbc.Driver" %>
<%@ page import="java.sql.*" %>
<%
//驱动程序名
String driverName="com.mysql.jdbc.Driver";
//数据库用户名
String userName="root";
//密码
String userPasswd="root";
//数据库名
String dbName="test";
//表名
String tableName="user";
//联结字符串
String url="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPasswd;
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection connection=DriverManager.getConnection(url);
Statement statement = connection.createStatement();
String sql="insert table user values('2','haisen')";
int rs = statement.executeUpdate(sql);
statement.close();
connection.close();
int tryint[]=new int[2];
tryint[0]=1;
tryint[1]=2;
%>
</body>
<script>
var aaatry="<%=rs%>";
alert(aaatry);
</script>
</html>
相关文章推荐
- JSP案例_利用JBDC连接Mysql数据库,并查询数据
- vs2015 新建ADO.NET 实体数据模型向导,选择MySql数据库连接后点击下一步就闪退
- C#连接MySQL数据库实现DataGridView定时更新数据——多结果集返回
- jsp连接MySQL数据库显示GIS地理数据乱码问题的解决(select AsText(the_geom))
- jsp连接MySQL数据库显示GIS地理数据乱码问题的解决(select AsText(the_geom))
- 使用JDBC连接MySQL数据库--典型案例分析(四)----更新和插入员工数据
- JSP连接mysql数据库导出数据出现中文乱码(问号)
- 使用java连接mysql数据库进行数据的查询插入更新及删除
- jsp数据库基础之--从MySQL数据库更新数据
- 使用JSP连接MySql数据库读取HTML表单数据进行存贮
- JSP连接SQL数据库实现数据分页显示
- Java连接MySQL数据库并且执行建表和插入数据
- JSP连接mysql数据库
- jsp连接mysql数据库的例子
- 用jsp连接mysql数据库,出现中文乱码,该怎么解决啊
- mysql数据库中创建、删除、更新、插入数据、等语句
- Jsp连接Mysql数据库
- Java连接mysql数据库并插入中文数据显示乱码
- JSP连接MySQL数据库问题
- python脚本――一种连接mysql数据库的方法(取回数据为list非tuple格式)并将数据写入TXT