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