JSP数据库连接成功

<%@ page language="java" contentType="text/html; charset=utf-8"   pageEncoding="utf-8"%>
<%@ page  import="java.sql.*" %>
<!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>
<%
    PreparedStatement ps = null;
    Connection ct = null;
    ResultSet rs = null;
    String url = "jdbc:sqlserver://localhost:1433;databaseName=test";
    String user="sa";  //超级管理员
    String password="sa";  //密码
    try {
        //1.加载驱动
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        System.out.println("加载驱动成功!");
    }catch(Exception e) {
        e.printStackTrace();
        System.out.println("加载驱动失败!");
    }
    try {  
        //2.连接
        ct=DriverManager.getConnection( url,user,password);
        System.out.println("连接数据库成功!");
    }catch(Exception e) {
        e.printStackTrace();
        System.out.println("连接数据库失败!");
    }
    
      
    
     %>
</body>
</html>

 

posted @ 2018-08-07 23:55  borter  阅读(188)  评论(0编辑  收藏  举报