jdbc连接数据库

主要步骤:
1.加载数据库驱动
2.获取数据库连接
3.创建查询对象
4.执行sql

import java.sql.Connection;
import java.sql.DriverManager;

public class DBUtil {

private static final String URI = "jdbc:mysql://192.168.1.101:3306/jdbc_demo?"  
        + "user=root&password=roo123t&useUnicode=true&characterEncoding=UTF-8";  

private static final String DRIVER = "com.mysql.jdbc.Driver";  

public static Connection connectDB() throws Exception {  
    //1、加载数据库驱动  
    Class.forName(DRIVER);  
    //2、获取数据库连接  
    Connection conn = DriverManager.getConnection(URI);  

    return conn;  
}  

}

Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);

你可能感兴趣的:(java学习笔记)