java Python連接數據庫mysql redis

java

import java.sql.Connection;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class Crawler {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Connection connection ;
        String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/yyqdb";
        String user = "root";
        String password = "123456";
        try {
            Class.forName(driver);
            connection = (Connection) DriverManager.getConnection(url,user,password);
            if(!connection.isClosed())
                System.out.println("Succeeded connecting to the Database!");
            Statement statement = connection.createStatement();
            String sql = "select * from yyq_user";
            ResultSet rs = statement.executeQuery(sql);
            String userName = null;
            String mobile = null;
            while(rs.next()){
                userName = rs.getString("userName");
                mobile = rs.getString("mobile");
                System.out.println(userName + "\t" + mobile);
            }
            rs.close();
            connection.close();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}


python

import pymysql

conn = pymysql.Connect(host="127.0.0.1",port=3306,user="root",passwd="123456",db="yyqdb",charset="utf8")
cur = conn.cursor()
sql = "select * from yyq_user"
cur.execute(sql)
rows = cur.fetchall()
for dr in rows:
    print(dr)



redis

java :

Jedis jedis = new Jedis("127.0.0.1");
        jedis.set("age", "23");
        System.out.println("連接成功"+jedis.get("age"));

python:

r = redis.Redis(host="127.0.0.1",port=6379)
r.set("age","23")
print(r.get("age"))








發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章