jdbc连接mysql代码块
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.Connection,java.sql.DriverManager" %>
<%@ page import="java.sql.PreparedStatement" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
//加载数据库驱动给jdbc
Class.forName("org.gjt.mm.mysql.Driver");
out.print("成功加载驱动");
String url ="jdbc:mysql://127.0.0.1:3306/test2?user=root&password=123";
//获取数据库连接,让java可以操作mysql
Connection conn = DriverManager.getConnection(url);
//定义一条sql命令
String sql ="create table tt1(name varchar(20))";
//从Connection对象中,获取一个sql执行者
PreparedStatement ps = conn.prepareStatement(sql);
ps.execute();
out.print(conn);
//首先关闭连接
ps.close();
conn.close();
%>
</body>
</html>