使用Java查询

Posted rzbwyj

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Java查询相关的知识,希望对你有一定的参考价值。

package cn.jbit.epetShop.dao;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

import cn.jbit.epetShop.dao.BaseDao;


public class BaseDao {
    public static String DRIVER; 
    public static String URL ; 
    public static String DBNAME;
    public static String DBPASS; 
    Connection conn = null;
    static{
        init();
    }
        public static void init(){
            Properties params=new Properties();
            String configFile = "database.properties";//配置文件路径
            //加载配置文件到输入流中
            InputStream is=BaseDao.class.getClassLoader().getResourceAsStream(configFile);
            try {
                //从输入流中读取属性列表7
                params.load(is);
            } catch (IOException e) {
                e.printStackTrace();
            }
            //根据指定的获取对应的值
            DRIVER=params.getProperty("driver");
            URL=params.getProperty("url");
            DBNAME=params.getProperty("user");
            DBPASS=params.getProperty("password");
        }   
        //数据库连接
    public Connection getConn() throws ClassNotFoundException, SQLException {
        Connection conn = null;
        try {
            Class.forName(DRIVER); 
            conn = DriverManager.getConnection(URL, DBNAME, DBPASS);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn; 
    }

    public void closeAll(Connection conn, PreparedStatement pstmt, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (pstmt != null) {
            try {
                pstmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }

    public int executeSQL(String preparedSql, Object[] param) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        int num = 0;

        /* 处理SQL,执行SQL */
        try {
            conn = getConn(); // 得到数据库连接
            pstmt = conn.prepareStatement(preparedSql); // 得到PreparedStatement对象
            if (param != null) {
                for (int i = 0; i < param.length; i++) {
                    pstmt.setObject(i + 1, param[i]); // 为预编译sql设置参数
                }
            }
            // System.out.println(preparedSql);
            num = pstmt.executeUpdate(); // 执行SQL语句
        } catch (ClassNotFoundException e) {
            e.printStackTrace(); // 处理ClassNotFoundException异常
        } catch (SQLException e) {
            e.printStackTrace(); // 处理SQLException异常
        } finally {
            this.closeAll(conn, pstmt, null);
        }
        
        return num;
    
}
1 driver=com.mysql.jdbc.Driver
2 url=jdbc:mysql://localhost:3306/petShop
3 user=root
4 password=
package com.myschool.entity;
 
import java.io.Serializable;
import java.util.Date;
 
public class Student implements Serializable {
     
    private static final long serialVersionUID = 6439763802252472361L;
     
    //定义实体属性
    private int studentNo;
    private String longinPwd;
    private String studentName;
    private int sex;
    private grade grade;//把年级对象作为属性
    private String phone;
    private String address;
    private Date bornDate;
    private String email;
    private String identityCard;
     
        //封装
    public int getStudentNo() {
        return studentNo;
    }
    public void setStudentNo(int studentNo) {
        this.studentNo = studentNo;
    }
    public String getLonginPwd() {
        return longinPwd;
    }
    public void setLonginPwd(String longinPwd) {
        this.longinPwd = longinPwd;
    }
    public String getStudentName() {
        return studentName;
    }
    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }
    public int getSex() {
        return sex;
    }
    public void setSex(int sex) {
        this.sex = sex;
    }
    public grade getGrade() {
        return grade;
    }
    public void setGrade(grade grades) {
        this.grade = grades;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public Date getBornDate() {
        return bornDate;
    }
    public void setBornDate(Date bornDate) {
        this.bornDate = bornDate;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getIdentityCard() {
        return identityCard;
    }
    public void setIdentityCard(String identityCard) {
        this.identityCard = identityCard;
    }
     
       //有参构造
    public Student(int studentNo, String longinPwd, String studentName,
            int sex, grade gradeID, String phone, String address, Date bornDate,
            String email, String identityCard) {
        this.studentNo = studentNo;
        this.longinPwd = longinPwd;
        this.studentName = studentName;
        this.sex = sex;
        this.gradeID = gradeID;
        this.phone = phone;
        this.address = address;
        this.bornDate = bornDate;
        this.email = email;
        this.identityCard = identityCard;
    }
 
    //无参构造
    public Student() {
         
    
package com.myschool.entity;

public class grade {
    private int gradeid;
    private String gradeName;
    public int getGradeid() {
        return gradeid;
    }
    public void setGradeid(int gradeid) {
        this.gradeid = gradeid;
    }
    public String getGradeName() {
        return gradeName;
    }
    public void setGradeName(String gradeName) {
        this.gradeName = gradeName;
    }
    public grade(int gradeid, String gradeName) {
        super();
        this.gradeid = gradeid;
        this.gradeName = gradeName;
    }
    public grade() {
        super();
        
    }
    
}
package com.myschool.dao;

import java.util.List;

import com.myschool.entity.Student;

public interface IStudentDao {

    /*
     * 查询所有学生记录,年级名称
     */
    public List<Student> Search() throws Exception;

    
}




package com.mychool.dao.impl;

import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.myschool.dao.IStudentDao;
import com.myschool.dao.baseDao;
import com.myschool.entity.Student;
import com.myschool.entity.grade;

public class IStudentDaoImpl extends baseDao implements IStudentDao {

    
    @Override
    public List<Student> Search() throws Exception {
        //创建student对象泛型集合
        List<Student> stus=new ArrayList<Student>();
        
        String sql="Select studentName,GradeName from Student,grade where Grade.gradeid=Student.gradeid ";

        //定义resultSet对象接收basedao的查询方法查出来的数据
        ResultSet rSet=executeQuery(sql);
        
        if (rSet!=null) {
            while (rSet.next()) {
                Student stu=new Student();
                grade grade=new grade();//创建年级对象;
                grade.setGradeName(rSet.getString("gradeName"));//给年级对象赋值
                stu.setStudentName(rSet.getString("studentName"));//给学生对象赋值
                stu.setGradeID(grade);
                stus.add(stu);//将学生对象添加到对象集合中
            }
            
        }
    //回收释放资源
        closeResouse();
        return stus;
    }

    
}
1.service接口:

package com.myschool.service;

import java.util.List;

import com.myschool.entity.Student;

public interface IService {
    
    public List<Student> Search() throws Exception;
}

    2.service接口实现类:

package com.mychool.service.Impl;

import java.util.List;

import com.mychool.dao.impl.IStudentDaoImpl;
import com.myschool.dao.IStudentDao;
import com.myschool.entity.Student;
import com.myschool.service.IService;

public class IServiceImpl implements IService{

    IStudentDao isd=new IStudentDaoImpl();
    @Override
    public List<Student> Search() throws Exception {
        return isd.Search();
    }
    
}

 

以上是关于使用Java查询的主要内容,如果未能解决你的问题,请参考以下文章

按要求编写Java应用程序。 创建一个叫做机动车的类: 属性:车牌号(String),车速(int),载重量(double) 功能:加速(车速自增)减速(车速自减)修改车牌号,查询车的载重量(代码片段

你如何在 python 中处理 graphql 查询和片段?

LockSupport.java 中的 FIFO 互斥代码片段

如何在片段中使用 GetJsonFromUrlTask​​.java

# Java 常用代码片段

# Java 常用代码片段