JAVA日記

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            10 隨筆 :: 0 文章 :: 0 評論 :: 0 Trackbacks
          package DAO;

          import java.sql.*;

          import entity.student;

          public class studentDAO {
              
          public student login(String username, String password) {
                  
          try {
                      
          new com.mysql.jdbc.Driver();
                  } 
          catch (Exception e) {
                      
          // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
                  Connection conn 
          = null;
                  PreparedStatement pstmt 
          = null;
                  ResultSet rs 
          = null;
                  String sql 
          = "select * from Student where username = ? and password = ?";

                  
          try {
                      conn 
          = java.sql.DriverManager.getConnection(
                              
          "jdbc:mysql://localhost:3306/oltest""root""4873617");
                      pstmt 
          = conn.prepareStatement(sql);
                      pstmt.setString(
          1, username);
                      pstmt.setString(
          2, password);

                      rs 
          = pstmt.executeQuery();

                      
          if (rs != null && rs.next()) {
                          
          // 讀到數(shù)據(jù),生成實體類
                          student student = new student();

                          student.setUsername(rs.getString(
          "username"));
                          student.setPassword(rs.getString(
          "password"));

                          
          return student;
                      }
                  } 
          catch (SQLException e) {
                      
          // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
                   
          return null;
              }
              
          public static void main(String[] args){
                  student s 
          = new studentDAO().login("zncuxt","4873617");
                  System.out.println(s.getUsername());
              }
          }
          posted on 2010-08-03 21:30 辛彤 閱讀(241) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 东乡族自治县| 石门县| 华阴市| 灵丘县| 太白县| 新营市| 修文县| 长丰县| 鹤山市| 临沧市| 丘北县| 班戈县| 台中市| 喀什市| 乌鲁木齐县| 古田县| 徐水县| 本溪市| 抚顺市| 呼图壁县| 左云县| 柞水县| 荥经县| 赣州市| 乐清市| 岳普湖县| 蒲城县| 鄂尔多斯市| 永泰县| 紫阳县| 芜湖市| 昌宁县| 忻州市| 全椒县| 永年县| 垦利县| 通山县| 赤壁市| 榆中县| 兰州市| 永年县|