IDEA 编写JDBC 第一个示例

知心惟有雕梁燕。自来相伴。东风不管琵琶怨。落花吹遍。

一、新建一个Module
在这里插入图片描述
二、在此Module下新建一个包,在包再建一个包,命名为lib
在这里插入图片描述

三、导入mysql驱动
在这里插入图片描述

四、将mysql驱动添加到项目的库里
在这里插入图片描述
五、代码实现

package Connection;

import org.junit.Test;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @author shkstart
 * @create 2021-08-12 23:30
 */
public class ConnectionTest {
    
    
    @Test
    public void testConnection1() throws SQLException {
    
    

        Driver driver=new com.mysql.jdbc.Driver();//driver具体的实现类
        //jdbc:mysql:协议
        //localhost:ip地址
        //3306:默认端口号

        //test_1:数据库名字
        //useUnicode=true  支持中文编码
        //characterEncoding=utf8:设置中文字符集为utf-8
        //usessl=true  使用安全连接
        String url="jdbc:mysql://localhost:3306/test_1?useUnicode=true&characterEncoding=utf8&usessl=true";

        //将用户名封装在Properties
        Properties info=new Properties();
        info.setProperty("user","root");
        info.setProperty("password","123456");

        Connection conn=driver.connect(url,info);
        System.out.println(conn);
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_46457946/article/details/119671647