最近我学java碰到了瓶颈
就是我不知道如何编写一个DAO类来
实现用户和SQLServer的联系
希望大师们给点例题

解决方案 »

  1.   

    DAO 操作数据库,JDBC,ORM 也行啊!
      

  2.   

    package com.dao;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.*;
    import org.apache.commons.collections.MultiHashMap;
    import org.apache.commons.collections.MultiMap; public class DaoTest {
    private Connection conn=null;
    List<Nodes> list = null;
    public Connection getConnection() {

    String url = "jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=dbName";
    String user = "sa";
    String password = "sa";
    try {
    if (conn == null) {
    Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
    conn = DriverManager.getConnection(url, user, password);
    }
    } catch (Exception e) {
    e.printStackTrace();
    return null;
    } finally {
    url = null;
    user = null;
    password = null;
    }
    return conn;
    } public List<Nodes> getNodeInfo()  {

    conn = getConnection();
    ResultSet rs = null;
    ArrayList<Nodes> list = new ArrayList<Nodes>();
    try {
    Statement pre =conn.createStatement();
    //省级
    rs =pre.executeQuery("SELECT * FROM tab ");
    while (rs.next()){
     
    System.out.println(rs.getString("字段名"));  
     
    }
     

    rs.close();
    pre.close();
    conn.close();
     
    } catch (SQLException e) {
    e.printStackTrace();
    }finally{
    // pre = null;
     conn = null;
     rs = null;
    }
            return list;
    }
     

    }
      

  3.   

    给一个详细的,这个是公用的。
    package dao;
    import java.sql.*;
    public class BaseDao {
    public final static String DRIVER="com.microsoft.jdbc.sqlserver.SQLServerDriver"; //数据库驱动
    public final static String URL="jdbc:microsoft:sqlserver://localhost:1433;DataBaseName=Tour";
    public final static String DBNAME="sa"; //数据库用户名
    public final static String DBPASS=""; //数据库密码
    /**
     * 得到数据库连接
     * @throws ClassNotFoundException
     * @throws SQLException
     * @return 数据库连接
     */
    public Connection getConn(){
    Connection conn = null;
    try {
    Class.forName(DRIVER); //注册驱动
    conn = DriverManager.getConnection(URL,DBNAME,DBPASS);
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return conn; //返回连接
    }
    /**
     * 释放资源
     * @param conn 数据库连接
     * @param pstmt PreparedStatement 对象
     * @param rs 结果集
     */
    public void closeAll(Connection conn,PreparedStatement pstmt,ResultSet rs){
    /*如果rs不空,关闭rs*/
    if(rs!=null){
    try {
    rs.close();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    /*如果pstmt不空,关闭pstmt*/
    if(pstmt!=null){
    try {
    pstmt.close();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    /*如果conn不空,关闭conn*/
    if(conn!=null){
    try {
    conn.close();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
    /**
     * 执行SQL语句,可以进行增、删、改的操作,不能执行查询
     * @param sql 预编译的SQL语句
     * @param param 预编译的SQL语句中的"?"参数的字符串数组
     * @return 影响的行数
     */
    public int executeSQL(String preparedSql,String[] param){
    Connection conn=null;
    PreparedStatement pstmt=null;
    int num=0;
    conn=getConn();
    try {
    pstmt=conn.prepareStatement(preparedSql);
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    if(param!=null){
    for(int i=0;i<param.length;i++){
    try {
    pstmt.setString(i+1,param[i]); //设置参数
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
    try {
    num=pstmt.executeUpdate(); //执行SQL语句
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }finally{
    closeAll(conn,pstmt,null); //释放资源
    }
    return num;
    }
    }
      

  4.   

    对就是这个典型例子
    现在我是一个新手
    看这个DAO类不懂
    有没有高手指点下
    写一个DAO的步骤
      

  5.   

    DAO 数据库持久化层的类 ,你可以看一下关于hibernate方面的数据,hibernate可以帮你自动生成dao的方法,你在自己研究一下就明白了
      

  6.   


    package base;import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.List;public class BaseDao {
    private static final String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    private static final String URL = "jdbc:sqlserver://localhost:1042;DatabaseName=crmManager";
    private static final String UID = "sa";
    private static final String PWD = "ok"; private ResultSet resultSet;
    private Statement statement;
    private PreparedStatement preparedStatment;
    private Connection connection; /**
     * 打开数据库
     * 
     * @throws ClassNotFoundException
     * @throws SQLException
     */
    public void open() throws ClassNotFoundException, SQLException {
    Class.forName(DRIVER);
    this.connection = DriverManager.getConnection(URL, UID, PWD);
    } /**
     * 关闭数据库
     */
    public void close() {
    if (null != this.resultSet) {
    try {
    this.resultSet.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    } if (null != this.statement) {
    try {
    this.statement.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    } if (null != this.preparedStatment) {
    try {
    this.preparedStatment.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    } if (null != this.connection) {
    try {
    this.connection.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    } /**
     * 根据statement删、修、改
     * 
     * @param sql
     * @return
     * @throws SQLException
     */
    public int executeSql(String sql) throws SQLException {
    this.statement = this.connection.createStatement();
    return this.statement.executeUpdate(sql);
    } /**
     * 根据preparedStatment删、修、改
     * 
     * @param sql
     * @param list
     * @return
     * @throws SQLException
     */
    public int executeSql(String sql, List list) throws SQLException {
    this.preparedStatment = this.connection.prepareStatement(sql);
    if (null != list) {
    for (int i = 0; i < list.size(); i++) {
    this.preparedStatment.setString(i + 1, list.get(i).toString());
    }
    }
    return this.preparedStatment.executeUpdate();
    } /**
     * 根据statement查找
     * 
     * @param sql
     * @return
     * @throws SQLException
     */
    public ResultSet findsql(String sql) throws SQLException {
    this.statement = this.connection.createStatement();
    this.resultSet = this.statement.executeQuery(sql);
    return this.resultSet;
    } /**
     * 根据preparedStatment查找
     * 
     * @param sql
     * @param list
     * @return
     * @throws SQLException
     */
    public ResultSet findsql(String sql, List list) throws SQLException {
    this.preparedStatment = this.connection.prepareStatement(sql);
    if (null != list) {
    for (int i = 0; i < list.size(); i++) {
    this.preparedStatment.setString(i + 1, list.get(i).toString());
    }
    }
    this.resultSet = this.preparedStatment.executeQuery();
    return this.resultSet;
    }
    }