jsp 连接mysql数据库,新建数据表,更新表数据

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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值