现在的位置: 首页 > 综合 > 正文

Netbeans连接数据库

2018年04月09日 ⁄ 综合 ⁄ 共 1209字 ⁄ 字号 评论关闭

 

  1. Netbeans连接数据库  
  2.  
  3. NetBeans项目的“项目属性”中“库”一栏中。Tab页“编译和运行”中已经加上jdbc的驱动文件  
  4.  
  5. Connection conn = null;//连接数据库的对象  
  6.  
  7. PreparedStatement pstmt = null;//执行sql对象  
  8.  
  9. try {  
  10.       
  11.     try {  
  12.           
  13.         Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");  
  14.           
  15.     } catch (ClassNotFoundException ex) {  
  16.           
  17.         Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);  
  18.           
  19.     }  
  20.       
  21.     conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=task""sa""sa");  
  22.       
  23.     String sql = "select * from userInfo where username='" + username + "'and password='" + password + "'";  
  24.       
  25.     System.out.println(username);  
  26.       
  27.     System.out.println(password);  
  28.       
  29.     pstmt = conn.prepareStatement(sql);  
  30.       
  31.     ResultSet rs = pstmt.executeQuery();  
  32.       
  33.     if (rs.next()) {  
  34.           
  35.         System.out.println("登录成功!");  
  36.           
  37.         //response.setHeader("refresh", "3;URL=userCenter.jsp");//登陆成功跳转到用户中心  
  38.           
  39.     } else {  
  40.           
  41.         System.out.println("失败!");  
  42.           
  43.     }  
  44.       
  45. catch (SQLException e) {  
  46.       
  47.     System.out.print("错误!\n");  
  48.       
  49.     System.out.println(e);  
  50.       
  51. }  
  52.  
  53.  

 

本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/702542

抱歉!评论已关闭.