`
happy20070302
  • 浏览: 95133 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

通过properties配置文件连接数据库

    博客分类:
  • j2se
阅读更多
**
* @作者:Jcuckoo
* @日期:2008-11-8
* @版本:V 1.0
*/

db.properties


DBDriver=sun.jdbc.odbc.JdbcOdbcDriver
Connection=jdbc:odbc:login
User=Jcuckoo
Password=
dbPool.java

import java.io.*;
import java.util.*;
import java.sql.*;

public class dbPool{
    private static dbPool instance = null;

    //取得连接
    public static synchronized Connection getConnection() {
        if (instance == null){
            instance = new dbPool();
        }
        return instance._getConnection();
    }

    private dbPool(){
        super();
    }

    private  Connection _getConnection(){
        try{
            String sDBDriver  = null;
            String sConnection   = null;
            String sUser = null;
            String sPassword = null;

            Properties p = new Properties();
            InputStream is = getClass().getResourceAsStream("/db.properties");
            p.load(is);
            sDBDriver = p.getProperty("DBDriver",sDBDriver);
            sConnection = p.getProperty("Connection",sConnection);
            sUser = p.getProperty("User","");
            sPassword = p.getProperty("Password","");

            Properties pr = new Properties();
            pr.put("user",sUser);
            pr.put("password",sPassword);
            pr.put("characterEncoding", "GB2312");
            pr.put("useUnicode", "TRUE");

            Class.forName(sDBDriver).newInstance();
            return DriverManager.getConnection(sConnection,pr);
        }
        catch(Exception se){
            System.out.println(se);
            return null;
        }
    }

    //释放资源
    public static void dbClose(Connection conn,PreparedStatement ps,ResultSet rs)
    throws SQLException
    {
          rs.close();
          ps.close();
          conn.close();

      }
    }


StudentDAO.java


public class StudentDAO {
    public StudentDAO() {
    }
    Connection conn;
    Statement st;
    ResultSet rs;
    public boolean checkLogin(String userName,String userPwd){
        String sql="select * from student where userName='"+userName+"' and userPwd='"+userPwd+"'";
        
        try {
            conn=dbPool.getConnection();
            st=conn.createStatement();
            rs=st.executeQuery(sql);
            if(rs.next()){
                conn.close();
                return true;
            }else{
                conn.close();
                return false;
            }
        } catch (SQLException e) {
            try {
                conn.close();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
            return false;
        }
    }
    public boolean checkLogin(StudentForm student){
        String sql="select * from student where userName='"+student.getUserName()+"' and userPwd='"+student.getUserPwd()+"'";
        
        try {
            conn=dbPool.getConnection();
            st=conn.createStatement();
            rs=st.executeQuery(sql);
            if(rs.next()){
                conn.close();
                return true;
            }else{
                conn.close();
                return false;
            }
        } catch (SQLException e) {
            try {
                conn.close();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
            return false;
        }
    }

    public List getAllStudent(){
        String sql="select * from student ";
        List students=new ArrayList();
        try {
            conn=dbPool.getConnection();
            st=conn.createStatement();
            rs=st.executeQuery(sql);
            while(rs.next()){
                Student student=new Student(rs.getString("xuehao"),
                        rs.getString("userName"),
                        rs.getString("userPwd"),
                        rs.getInt("czxt"),
                        rs.getInt("wjyl"),
                        rs.getInt("sjjg")
                );
                students.add(student);
            }
            
        } catch (SQLException e) {
            try {
                conn.close();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
            return null;
        }
        return students;
        
    }
    
    public Student getStudentByXuehao(String xuehao){
        String sql="select * from student where xuehao='"+xuehao+"'";
        Student student=null;
        try {
            conn=dbPool.getConnection();
            st=conn.createStatement();
            rs=st.executeQuery(sql);
            if(rs.next()){
                student=new Student(rs.getString("xuehao"),
                        rs.getString("userName"),
                        rs.getString("userPwd"),
                        rs.getInt("czxt"),
                        rs.getInt("wjyl"),
                        rs.getInt("sjjg")
                );
            }
            return student;
            
        } catch (SQLException e) {
            try {
                conn.close();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
            return null;
        }
    }
    public boolean deleteStudent(String xuehao){
        String sql="delete from student where xuehao='"+xuehao+"'";
        conn=dbPool.getConnection();
        try {
            st=conn.createStatement();
            st.execute(sql);
            conn.close();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                conn.close();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            return false;
        }
    }
    public boolean updateStudent(Student student){
        String sql="update student set userName='" +student.getUserName()+
                "',userPwd='" +student.getUserPwd()+"',czxt="+student.getCzxt()+
                ",wjyl="+student.getWjyl()+",sjjg="+student.getSjjg()+" where xuehao='"+student.getXuehao()+"'";
        conn=dbPool.getConnection();
        try {
            st=conn.createStatement();
            st.execute(sql);
            conn.close();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                conn.close();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            return false;
        }
    }
    public boolean insertStudent(Student student){
        String sql="insert into student values('"+student.getXuehao()+"','" +student.getUserName()+
                "','" +student.getUserPwd()+"',"+student.getCzxt()+
                ","+student.getWjyl()+","+student.getSjjg()+")";
        //System.out.println(sql);
        conn=dbPool.getConnection();
        try {
            st=conn.createStatement();
            st.execute(sql);
            conn.close();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                conn.close();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            return false;
        }
    }
}


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/guoquanyou/archive/2008/12/01/3418933.aspx
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics