/*
Netbeans连接数据库 NetBeans项目的“项目属性”中“库”一栏中。Tab页“编译和运行”中已经加上jdbc的驱动文件
*/
Connection conn = null;//连接数据库的对象 PreparedStatement pstmt = null;//执行sql对象 try { try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); } catch (ClassNotFoundException ex) { Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex); } conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=task", "sa", "sa"); String sql = "select * from userInfo where username='" + username + "'and password='" + password + "'"; System.out.println(username); System.out.println(password); pstmt = conn.prepareStatement(sql); ResultSet rs = pstmt.executeQuery(); if (rs.next()) { System.out.println("登录成功!"); //response.setHeader("refresh", "3;URL=userCenter.jsp");//登陆成功跳转到用户中心 } else { System.out.println("失败!"); } } catch (SQLException e) { System.out.print("错误!\n"); System.out.println(e); } //本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/702542