我想从JAVA里面取出数据库里面的数据怎么取出来呢? 求各位写写代码!
麻烦了!

解决方案 »

  1.   

    package StudentManager.demo;import java.sql.Connection;
    import java.sql.Date;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.ArrayList;
    import java.util.List;
    public class StudentDaoImp implements StudentDao {
    private  Connection con =null;
    private PreparedStatement ps=null;
    private ResultSet rs=null;
    private List list=null;
    private String addSql="insert into Student"+
    "(studentNumber,name ,password,sex ,birthday ,address  ,polity ,brief)"+
    " values(?,?,?,?,?,?,?,?)";
    private String delSql="delete from Student  where id=?";
    private  String selAll="select * from Student";
    private String selId="select  * from Student where id=?";
    private String selName="select  * from Student where name=?";
    private String login="insert into Student(name,password) values(?,?)";
    private String edit="update Student set password=?  where studentNumber=?";           
              
     public boolean add(Student stu) {
    try {
    if(con==null){
    con=ConnectionFactory.getConnection();
    }
    ps=con.prepareStatement(addSql);
    ps.setString(1, stu.getStudentNumber());
    ps.setString(2, stu.getName());
    ps.setString(3, stu.getPassword());
    ps.setInt(4,stu.getSex());
    ps.setDate(5,stu.getBirthday());
    ps.setString(6, stu.getAddress());
    ps.setInt(7, stu.getPolity());
    ps.setString(8,stu.getBrief());
    int cout=ps.executeUpdate();
    if(cout>0){
    return true;
    }
    else{

    return false;
    }
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }finally{
    ConnectionFactory.closeAll(con, ps, null);

    }
    return false;
    }
      public boolean delete(Student stu) {
    try {
    if(con==null){
    con=ConnectionFactory.getConnection();
    }
    ps=con.prepareStatement(delSql);
    ps.setLong(1, stu.getId());
    int cout=ps.executeUpdate();
    if(cout>0){
    return true;
    }
    else{

    return false;
    }
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return false;
    } public boolean edit(Student stu) {
    try {
    if(con==null){
    con=ConnectionFactory.getConnection();
    }
    ps=con.prepareStatement(edit);
    ps.setString(1,stu.getPassword());
    ps.setString(2,stu.getStudentNumber());
    int cout=ps.executeUpdate();
    if(cout>0){
    return true;
    }
    else{

    return false;
    }
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }finally{
    ConnectionFactory.closeAll(con, ps, null);

    }
    return false;
    } public List findAll() {
    list=new ArrayList();
    try {

    if(con==null){
    con=ConnectionFactory.getConnection();
    }
    ps=con.prepareStatement(selAll);
    rs=ps.executeQuery();
    while(rs.next()){
    Student stu=new Student();
    stu.setId(rs.getLong("id"));
    stu.setStudentNumber(rs.getString("studentNumber"));
    stu.setName(rs.getString("name"));
    stu.setPassword(rs.getString("password"));
    stu.setSex(rs.getInt("sex"));
    stu.setBirthday(rs.getDate("birthday"));
    stu.setAddress(rs.getString("address"));
    stu.setPolity(rs.getInt("polity"));
    stu.setBrief(rs.getString("brief"));
    list.add(stu);
    }
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }finally{
    ConnectionFactory.closeAll(con, ps, rs);

    }
    return list;
    } public Student findById(int id) {
    Student stu=null;
    try {

    if(con==null){
    con=ConnectionFactory.getConnection();
    }
    ps=con.prepareStatement(selId);
    ps.setInt(1,id);
    rs=ps.executeQuery();
    while(rs.next()){
     stu=new Student();
    stu.setId(rs.getLong("id"));
    stu.setStudentNumber(rs.getString("studentNumber"));
    stu.setName(rs.getString("name"));
    stu.setPassword(rs.getString("password"));
    stu.setSex(rs.getInt("sex"));
    stu.setBirthday(rs.getDate("birthday"));
    stu.setAddress(rs.getString("address"));
    stu.setPolity(rs.getInt("polity"));
    stu.setBrief(rs.getString("brief"));
    }
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }finally{
    ConnectionFactory.closeAll(con, ps, rs);

    }
    return stu;


    } public Student findByName(String name) {
    Student stu=null;
    try {

    if(con==null){
    con=ConnectionFactory.getConnection();
    }
    ps=con.prepareStatement(selName);
    ps.setString(1, name);
    rs=ps.executeQuery();
    while(rs.next()){
     stu=new Student();
    stu.setId(rs.getLong("id"));
    stu.setStudentNumber(rs.getString("studentNumber"));
    stu.setName(rs.getString("name"));
    stu.setPassword(rs.getString("password"));
    stu.setSex(rs.getInt("sex"));
    stu.setBirthday(rs.getDate("birthday"));
    stu.setAddress(rs.getString("address"));
    stu.setPolity(rs.getInt("polity"));
    stu.setBrief(rs.getString("brief"));
    }
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }finally{
    ConnectionFactory.closeAll(con, ps, rs);

    }
    return stu;

    } public boolean login(String name, String pwd) {
    try {
    if(con==null){
    con=ConnectionFactory.getConnection();
    }
    ps=con.prepareStatement(login);
    ps.setString(1,name);
    ps.setString(2, pwd);
    int cout=ps.executeUpdate();
    if(cout>0){
    return true;
    }
    else{

    return false;
    }
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }finally{
    ConnectionFactory.closeAll(con, ps, null);

    }
    return false;


    }}
    不知道楼主使用的是jdbc查询还是使用框架呢?
      

  2.   

    SQL 数据库里面提取数据!
      

  3.   

    不是我说 的是怎么用JAVA代码把SQL里面的数据取出来 
    求各位大大行行好吧  急啊  做个示范都OK  有些代码 我看不懂 麻烦 您们谢谢注释谢谢!