会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132464个问题

老师我这个老师运行不成功

image.png

import pymysql
class DBUtil:
    cinfig={
        'host':'localhost',
        'user':'root',
        'password':'199851',
        'db':'test01',
        'charset':'utf8'
    }
    def __init__(self):
        self. Connection = pymysql.connect(**DBUtil.config)
        self.cursor = self.Connection.cursor()
    def close(self):
        if self.cursor:
            self.cursor.close()
        if self.Connection:
            self.connection.close()    
    #插入  修改  删除调用
    def exeDML(self,sql,*args):
        try:
            #执行sql
            count = self.cursor.execute(sql,args)
            #提交事务
            self0.connection.comit()
            return count
        except Exception as e:
            print(e)
            if self.Connection:
                self.Connection.rollback()
    def query_one(self,sql,*args):
        try:
            #执行sql
            self.cursor.execute(sql,args)
            #获取结果集
            return self.cursor.fetchone()
        except Exception as e:
            print(e)
        finally:
            self.close()
    def query_all(self,sql,*args):
        try:
            #执行sql
            self.cursor.execute(sql,args)
            #获取结果集
            return self.cursor.fetchall()
        except Exception as e:
            print(e)
        finally:
            self.close()
if __name__ =='__main__':
    dbutil = DBUtil()
    # sql = 'insert into emp (empno,ename,sal) values( %s,%s,%s)'
    # conut = dbutil.exeDML(sql,9999,'lili',12000)
    # print(conut)
    # sql = 'select * from emp where empno=%s'
    # emp = dbutil.query_one(sql,7788)
    # print(emp)
    sql = 'select * from emp'
    emps = dbutil.query_all(sql)
    for e in emps:
        print(e,end='\n')


Python 全系列/第五阶段:数据库编程/python操作mysql(旧) 16056楼
WEB前端全系列/第十九阶段:Vue2知识体系(旧)/Vue基础知识 16057楼
Python 全系列/第十二阶段:Python_Django3框架/Django高级 16058楼
Python 全系列/第五阶段:数据库编程/python操作mysql(旧) 16059楼
JAVA 全系列/第六阶段:项目管理与SSM框架/Spring 16062楼
JAVA 全系列/第一阶段:JAVA 快速入门/变量、数据类型、运算符 16063楼

public class UserLoginDaoImpl implements UserLoginDao {
    @Override
    public Users selectUsersByUsernameAndUserpwd(String username, String userpwd) {
        Connection conn = null;
        Users user = null;
        try {
            conn = JDBCUtils.getConnection();
            PreparedStatement ps = conn.prepareStatement("selcet * from users where username=? and userpwd=?");
            ps.setString(1, username);
            ps.setString(2, userpwd);
            ResultSet resultSet = ps.executeQuery();
            while (resultSet.next()) {
                user = new Users();
                user.setUsersex(resultSet.getString("usersex"));
                user.setUserpwd(resultSet.getString("userpwd"));
                user.setPhonenumber(resultSet.getString("phonenumber"));
                user.setQqnumber(resultSet.getString("qqnumber"));
                user.setUserid(resultSet.getInt("userid"));
                user.setUsername(resultSet.getString("username"));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.closeConnection(conn);
        }
        return user;
    }
}

老师能给我讲解一下,这串代码吗?有点迷糊了,特别是这两个传进来的参数String username,String userpwd。

JAVA 全系列/第五阶段:JavaWeb开发/Web实战案例 16065楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637