jsp纯JavaSqlserver数据库连接

<%@ page contentType="text/html; charset=utf-8"%>
<%@ page import="java.sql.*" %>
<HTML><BODY>
	<% 
	Connection con;
	Statement sql;
	ResultSet rs;
	try{
	Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
	}catch(Exception e){ out.print(e);}
	
	try{
	String uri="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=jspsql";
	String user="sa";
    String password="123456";
    con=DriverManager.getConnection(uri,user,password);
    sql=con.createStatement();
    rs=sql.executeQuery("select * from score");
    out.print("<table border=2>");
    out.print("<tr>");
    out.print("<th width=100>"+"学号");
    out.print("<th width=100>"+"姓名");
    out.print("<th width=50>"+"数学成绩");
    out.print("<th width=50>"+"英语成绩");
    out.print("<th width=50>"+"英语成绩");
    out.print("</tr>");
    
    while(rs.next()){
    out.print("<tr>");
    out.print("<td >"+rs.getString(1)+"</td>");
    out.print("<td >"+rs.getString(2)+"</td>");
    out.print("<td >"+rs.getFloat(3)+"</td>");
    out.print("<td >"+rs.getFloat(4)+"</td>");
    out.print("<td >"+rs.getFloat(5)+"</td>");
    out.print("</tr>") ;
    }
    out.print("</table>");
    
    con.close();
    }catch(SQLException e){ out.print(e);}
    %> 
<BODY></HTML>
    

你可能感兴趣的:(java)