JDBC--java操作mysql数据库

package Mysql;
import java.sql.*;
public class mysql1 {//Java查看数据
    public static void main(String[] args) throws SQLException {
        Connection root = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql3", "root", "123456");
        Statement statement = root.createStatement();
        ResultSet resultSet = statement.executeQuery("select * from emp3");
        while (resultSet.next()){
            String string = resultSet.getString("eid");
            String name = resultSet.getString("ename");
            int age = resultSet.getInt("age");
            String dept_id = resultSet.getString("dept_id");
            System.out.println("id"+string+"\tename"+name+"\tage"+age+"\tdept"+dept_id);
        }
        resultSet.close();
        statement.close();
        root.close();
   }
}
package Mysql;
import java.sql.*;
public class mysql2 {//java用数据集遍历数据
    public static void main(String[] args) throws SQLException {
        Connection root = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql3", "root", "123456");
        Statement statement = root.createStatement();
        ResultSet resultSet = statement.executeQuery("select * from emp3");
        ResultSetMetaData metaData = resultSet.getMetaData();
        int columnCount = metaData.getColumnCount();
        while (resultSet.next()){
            for (int i = 1; i < columnCount+1; i++) {
                System.out.print(resultSet.getObject(i)+"\t");
            }
            System.out.println();
        }
        resultSet.close();
        statement.close();
        root.close();
   }
}
package Mysql;
import java.sql.*;
import java.util.Scanner;
public class mysql3 {
    public static void main(String[] args) throws SQLException {
        Connection root = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql3", "root", "123456");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名");
        String id = scanner.next();
        System.out.println("请输入密码");
        String pass_w = scanner.next();
        String sql = "select * from id_password where eid= ? and epassword = ?";
        PreparedStatement preparedStatement = root.prepareStatement(sql);
        preparedStatement.setObject(1,id);
        preparedStatement.setObject(2,pass_w);
        ResultSet resultSet = preparedStatement.executeQuery();
        if(resultSet.next()){
            System.out.println("密码输入成功");
        }else{
            System.out.println("密码错误");
        }
        resultSet.close();
        preparedStatement.close();

        root.close();
    }
}

python 利用PyMySQL操纵数据库

import pymysql

conn = pymysql.connect(host='localhost', port=3306, user='root',password='123456',database='mysql3', charset='utf8')
# 获取游标
cursor = conn.cursor()

print("请输入账号")
id=input()
print("请输入密码")
password=input()
sql="select count(*) from id_password where eid=%s and epassword=%s"
data=(id,password)
cursor.execute(sql,data)
a=cursor.fetchone()#元组
if not a[0]:
    print('密码输入错误')
else:
    print('密码输入正确')
#修改数据库要提交conn.
# 关闭游标
cursor.close()
# 关闭连接
conn.close()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值