1.在JSP中链接SQL Server数据库db_user.
<%@page contentType="text/html;charset=utf-8"%>
<%@page import="java.util.*,java.sql.*"%>
<%
String driverClass = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url = "jdbc:sqlserver://1227.0.0.1:1433;DatabaseName=db_user";
String userName = "admin";
String userPass = "123456";
%>
<%
//1.加载驱动
Class.forName(driverClass);
//2.建立连接
Connect conn = DriverManager.getConnection(url,userName,userPass);
Statement st = conn.createStatement();
//3.执行查询语句
ResultSet rs = st.executeQuery("select*from user");
//4.处理结果集
while(rs.next()){
out,println("<br>用户名:"+rs.getString(2));
out.println("密码:"+rs.getString(3)+"<br>");
}
//5.关闭连接
rs.close();
st.close();
conn.close();
%>
2.在JSP中连接MySQL连接数据库db_user.
<%@page contentType="text/html;charset=utf-8"%>
<%@page import="java.util.*,java.sql.*"%>
<%
/**
*1.需下载Mysql连接包,然后复制到项目下的lib文件夹下
*2.右键单击mysql连接包build path->add to buildpath*/
String driverClass = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://127.0.0.1:3306/db_user";
String name="admin";
String pass="123456";
%>