天天看點

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>