java IDEA&&NetBeans连接MySQL(附封装好的数据库代码)

文件链接(腾讯微云):
链接:https://share.weiyun.com/pG2P7QHb 密码:py69cp

首先先将下载下来的文件解压,复制一下两个文件。

IDEA

1.在IDEA中新建一个目录

2.命名目录名为:MySQL

3.然后将刚刚复制好的文件粘贴至MySQL目录

4.将这两个文件右键选择添加为库,然后就可以使用mysql了

NetBeans

1.右键选择库,将刚刚两个文件选择添加为jar文件

2.然后点击服务

3.右键数据库选择新建连接

4.下拉选择驱动程序为mysql

5.点击添加,将刚刚两个文件添加至驱动程序文件

6.然后配置自己的数据库属性,点击测试连接,会出现测试成功

7.然后点击下一步,完成

数据库代码

public class Table {
   
    private String tablename;
    private Vector<String> columename = new Vector<String>();

    Table(String name,String...vec){
   
        tablename = name;
        for(int i=0;i<vec.length;i++){
   
            columename.add(vec[i]);
        }
    }

    /** * @return 表的列信息 */
    public Vector<String> getColumename(){
   
        return columename;
    }

    /** * @return 表名 */
    public String getname(){
   
        return tablename;
    }
}

数据库

public class DataBase {
   
    /** * JDBC驱动名、URL、用户名、密码 */
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String URL = "jdbc:mysql://localhost:3306/";
    static final String USER = "ik";
    static final String PASSWORD = "123456";

    private Connection connection = null;
    private Statement statement = null;
    /** * database 数据库 * table 表 */
    private String dataBase = null;
    private Table table = null;

    /** * 表存在 */
    DataBase(String Dbname, Table TBname) {
   
        try {
   
            //注册JDBC驱动
            Class.forName(JDBC_DRIVER);

            //链接到数据库
            dataBase = Dbname;
            connection = DriverManager.getConnection(URL + Dbname, USER, PASSWORD);

            //实例化statement对象
            statement = connection.createStatement();

            table = TBname;
        } catch (Exception e) {
   
            System.out.println("construct error!");
        }
    }

    public void showALL() {
   
        try {
   
            //sql为查询语句
            String sql = "SELECT * FROM " + table.getname()+";";
            //resultset存储查询结果
            ResultSet resultSet = statement.executeQuery(sql);

            Vector<String> vec = table.getColumename();
            for (int i = 0; i < vec.size(); i++) {
   
                System.out.print(vec.get(i) + " ");
            }
            System.out.println();
            while (resultSet.next()) {
   
                for (int i = 0; i < vec.size(); i++) {
   
                    System.out.print(resultSet.getString(vec.get(i)) + " ");
                }
                System.out.println();
            }
        } catch (Exception e) {
   
            System.out.println("select all error!");
        }
    }

    /** * 得到column列value所对应的值(值为选定的几个列) */
    public Vector<String> getValue(String condition,String value,String...column){
   
        Vector<String> vecret = new Vector<String>();
        try{
   
            String sql = "SELECT "+column[0];
            for(int i=1;i<column.length;i++){
   
                sql+=","+column[i];
            }
            sql+=" FROM "+table.getname()+" WHERE "+condition+"='"+value+"';";
            ResultSet resultSet = statement.executeQuery(sql);

            while (resultSet.next()){
   
                String ret="";
                for(int i=0;i<column.length;i++) {
   
                    ret += resultSet.getString(column[i])+" ";
                }
                vecret.add(ret);
            }
        }
        catch(Exception e){
   
            System.out.println("getValue error!");
        }
        finally {
   
            return vecret;
        }
    }

    public Vector<String> getValue(Pair<String,String>...condValue){
   
        Vector<String> vecret = new Vector<String>();
        try{
   
            String sql = "SELECT * ";
            sql += " FROM " + table.getname() + " WHERE ";
            for(int i=0;i<condValue.length;i++) {
   
                sql+= condValue[i].getKey() + "='" + condValue[i].getValue()+"'";
                if(i!=condValue.length-1){
   
                    sql+=" AND ";
                }
            }
            sql+=";";
            ResultSet resultSet = statement.executeQuery(sql);

            Vector<String> tableColumn = table.getColumename();
            while (resultSet.next()){
   
                String ret="";
                for(int i=0;i<tableColumn.size();i++) {
   
                    ret += resultSet.getString(tableColumn.get(i))+" ";
                }
                vecret.add(ret);
            }
        }
        catch(Exception e){
   
            System.out.println("getValue error!");
        }
        finally {
   
            return vecret;
        }
    }
    /** * 得到column列value所对应的所有值 */
    public Vector<String> getValue(String condition,String value){
   
        Vector<String> retvec = new Vector<String>();

        try{
   
            String sql = "SELECT *"+" FROM "+table.getname()+" WHERE "+condition+"='"+value+"';";
            ResultSet resultSet = statement.executeQuery(sql);

            while(resultSet.next()) {
   
                String ret="";
                Vector<String> vec = table.getColumename();
                for (int i = 0; i < vec.size(); i++) {
   
                    ret+=resultSet.getString(vec.get(i))+" ";
                }
                retvec.add(ret);
            }
        }
        catch(Exception e){
   
            System.out.println("getValue error!");
        }
        return retvec;
    }

    public Vector<String> getAll(){
   
        Vector<String> retvec = new Vector<String>();
        try{
   
            String sql =  "SELECT * FROM "+table.getname();
            ResultSet resultSet = statement.executeQuery(sql);

            while(resultSet.next()) {
   
                String ret="";
                Vector<String> vec = table.getColumename();
                for (int i = 0; i < vec.size(); i++) {
   
                    ret+=resultSet.getString(vec.get(i))+" ";
                }
                retvec.add(ret);
            }
        }
        catch(Exception e){
   
            System.out.println("getAll error!");
        }
        return retvec;
    }
    /** * 判断colunm列中是否有value */
    public boolean isExist(Pair<String,String>...condValue){
   
        boolean flag = false;
        try{
   
            String sql = "SELECT * ";
            sql += " FROM " + table.getname() + " WHERE ";
            for(int i=0;i<condValue.length;i++) {
   
            sql+= condValue[i].getKey() + "='" + condValue[i].getValue()+"'";
            if(i!=condValue.length-1){
   
                sql+=" AND ";
            }
            sql+=";";
            ResultSet resultSet = statement.executeQuery(sql);


            if(resultSet.next()){
   
                flag = true;
            }
            return flag;
            }
        }
        catch(Exception e){
   
            System.out.println("is exist error!");
        }
        finally {
   
            return flag;
        }
    }

    public void insertInfor(String... values) {
   
        try {
   
            String sql = "INSERT INTO " + table.getname() + " VALUES(";
            for (int i = 0; i < values.length; i++) {
   
                sql += "'" + values[i] + "'";
                if (i != values.length - 1) {
   
                    sql += ",";
                }
            }
            sql += ");";
            System.out.println(sql);
            statement.executeUpdate(sql);
        } catch (Exception e) {
   
            System.out.println("insert error!");
        }
    }

    public void deleteInfor(Pair<String,String>...condValue) {
   
        try{
   
            String sql = "DELETE FROM "+table.getname()+ " WHERE ";
            for(int i=0;i<condValue.length;i++) {
   
                sql+= condValue[i].getKey() + "='" + condValue[i].getValue()+"'";
                if(i!=condValue.length-1){
   
                    sql+=" AND ";
                }
            }
            sql+=";";
            statement.executeUpdate(sql);
        }
        catch(Exception e){
   
            System.out.println("getValue error!");
        }
    }

    public void changeInfor(String columnName, String newvalues, String condition) {
   
        try {
   
            String sql = "UPDATE " + table.getname() + " SET " + columnName + "=" + newvalues + " WHERE " + condition+";";
            statement.executeUpdate(sql);
        } catch (Exception e) {
   
            System.out.println("change error!");
        }
    }

    public void changeTable(Table tableName){
   
        table=tableName;
    }
}

参考文献

[1] 无
全部评论

相关推荐

爱看电影的杨桃allin春招:我感觉你在炫耀
点赞 评论 收藏
分享
美丽的查理斯不讲武德:包kpi的啊,感觉虾皮一点hc都没有
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务