日期:2014-05-20  浏览次数:20907 次

小弟新学java想写个连接数据库显示数据帮看看改怎么写啊
实体类

package com.entity;

public class login {
private int id;
private String name;
private int age;
private String sex;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}

}
连接数据库的类

package com.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;

public class DBhpler {

private static Connection con() {
Connection con = null;
try {
Class.forName("com.microsoft.sqlserver.jdbc");
con = DriverManager.getConnection(
"jdbc:sqlserver://localhost:1433;databasename=zhdb", "sa",
"zhczqwhy");
} catch (Exception e) {
e.printStackTrace();
}
return con;
}

public void closeCon(Connection con) {
if (con != null)
try {
if (!con.isClosed())
con.close();
} catch (Exception e) {
e.printStackTrace();
}

}


}
方法不会写了 希望帮忙补充完整 还有页面(最好用el表达式)
package com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

public class stuDao {
private static Connection con=null;
private static PreparedStatement ps=null;
private static ResultSet rs=null;

}

实在写补到了 急人啊!!!


------解决方案--------------------
Java code
//对数据库的增加、修改和删除的操作
  public boolean executeUpdate(String sql) {

    if (con == null) {
      creatConnection();
    }
    try {
      Statement stmt = con.createStatement();
      int iCount = stmt.executeUpdate(sql);
      System.out.println("操作成功,所影响的记录数为" + String.valueOf(iCount));
    }
    catch (SQLException e) {
      System.out.println(e.getMessage());
      System.out.println("executeUpdaterError!");
    }
    return true;
  }
//对数据库的查询操作
  public ResultSet executeQuery(String sql) {
    ResultSet rs;
    try {
      if (con == null) {
        creatConnection();
      }
      Statement stmt = con.createStatement();
      try {
        rs = stmt.executeQuery(sql);
      }
      catch (SQLException e) {
        System.out.println(e.getMessage());
        return null;
      }
    }
    catch (SQLException e) {
      System.out.println(e.getMessage());
      System.out.println("executeQueryError!");
      return null;
    }
    return rs;
  }
//关闭数据库的操作
  public void closeConnection() {
    if (con != null) {
      try {
        con.close();
      }
      catch (SQLException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
        System.out.println("Failed to close connection!");
      }
      finally {
        con = null;
      }
    }
  }

------解决方案--------------------
public class stuDao
{
private static Connection con = DBhpler.getCon();

public void getResultSet()
{

PreparedStatement ps = null;

ResultSet rs = null;

try
{
con.setAutoCommit(false);
ps = con.prepareStatement("select * from test where id=?");
ps.setInt(1, 12);
rs = ps.executeQuery();
}
catch (SQLException e)