日期:2014-05-17 浏览次数:20772 次
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*"%> <% Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); String url = "jdbc:sqlserver://localhost:1433;" + "databaseName=exam;user=admin;password=admin;"; Connection conn = DriverManager.getConnection(url); Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); String sql="select * from Teacher"; ResultSet rs=stmt.executeQuery(sql); while(rs.next()){ out.print("用户名:"); out.print(rs.getString("uid")+" 密码:"); out.println(rs.getString("pwd")+"<br>"); } rs.close(); stmt.close(); conn.close(); %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> </body> </html>
private Connection getConn() { try { Class.forName(driver); Connection conn = DriverManager.getConnection(url, userName, password); return conn; } catch (Exception e) { e.printStackTrace(); } return null; }
------解决方案--------------------
数据库的驱动JAR包导进去
------解决方案--------------------
要给工程导入jar包