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()
|