JDBC操纵MySQL数据库

public class JDBCDemo {
    
    
    public static void main(String[] args) throws Exception {
    
      //抛出异常
    	// 0.创建project后需导入驱动jar包,并将其add as library
        // 1.注册驱动
        Class.forName("com.mysql.jdbc.Driver");  //mysql5之后的驱动jar包该行代码可以不写
        
        // 2.获取连接
        //String url = "jdbc:mysql://IP地址(域名):端口号/数据库名称?键值对1&键值对2";
        //若连接的为本地服务器,且mysql服务默认端口为3306则url可以写作“jdbc:mysql:///数据库名称”
        String url = "jdbc:mysql://127.0.0.1:3306/mysql?useSSL=false";
        String username = "用户名";
        String password = "口令";
        Connection connection = DriverManager.getConnection(url, username, password);

        // 3.定义sql
        String sql = "sql指令";

        // 4.获取执行sql的对象 statement
        Statement statement = connection.createStatement();

        // 5.执行sql
        int count = statement.executeUpdate(sql);  //受影响的行数

        // 6.处理结果
        System.out.println(count);

        // 7.释放资源
        connection.close();
        statement.close();
    }
}

猜你喜欢

转载自blog.csdn.net/WuwuwuH_/article/details/131222354