AI智能
改变未来

Java操作MySQL

  file->project structure->Modules->Dependencies->+->JARs or dir…->选择包->apply

  这里的选择包:mysql-connector-java-5.1.44-bin.jar,导入之后重启idea

2.用IDEA来执行sql语句

2.1 通过反射加载

Class.forName(\"com.mysql.jdbc.Driver\");

2.2 与MySQL建立连接

Connection connection= DriverManager.getConnection(\"jdbc:mysql://master:3306/db01\",\"root\", \"123456\");

2.3创建执行器

创建执行器有两种方式:

createStatement()
prepareStatement()

Statement statement = connection.createStatement();

2.4执行sql语句

String sql =\"select * from student\";ResultSet rs = statement.executeQuery(sql);

2.4显示结果

while (rs.next()){System.out.println(rs.getString(2));}

2.5 关闭连接(从下往上)

rs.close();statement.close();connection.close();

3. 完整代码

3.1 MySQL查询功能

import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.Statement;public class MysqlSelect {public static void main(String[] args) throws Exception {//jdbc的使用//1.通过反射加载驱动Class.forName(\"com.mysql.jdbc.Driver\");//2.建立链接Connection connection= DriverManager.getConnection(\"jdbc:mysql://master:3306/db01\",\"root\", \"123456\");//3.创建执行器//3.1  createStatement()//3.2  prepareStatement(参数)Statement statement = connection.createStatement();//4.执行sql语句String sql =\" select * from user\";ResultSet rs = statement.executeQuery(sql);//5.获取结果while(rs.next()){System.out.println(rs.getString(\"user\"));System.out.println(rs.getString(\"password\"));}//6.关闭rs.close();statement.close();connection.close();}}

3.2 MySQL删除功能

import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.Statement;public class MysqlDelete {public static void main(String[] args) throws Exception {Class.forName(\"com.mysql.jdbc.Driver\");Connection connection= DriverManager.getConnection(\"jdbc:mysql://master:3306/db01\",\"root\", \"123456\");String sql=\"delete from user where user=\'123\'\";String sql1=\"delete from user where password=\'12345\'\";Statement statement = connection.createStatement();int i1 = statement.executeUpdate(sql1);PreparedStatement ps = connection.prepareStatement(sql);int i = ps.executeUpdate();System.out.println(i);System.out.println(i1);ps.close();statement.close();connection.close();}}

3.3 MySQL改表功能

import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;public class MysqlUpdata {public static void main(String[] args) throws Exception {Class.forName(\"com.mysql.jdbc.Driver\");Connection connection = DriverManager.getConnection(\"jdbc:mysql://master:3306/db01\",\"root\",\"123456\");String sql=\"update user set password=\'123\' where user=\'master\'\";PreparedStatement ps = connection.prepareStatement(sql);int i = ps.executeUpdate();System.out.println(i);ps.close();connection.close();}}

3.4MySQL增加功能

import java.sql.Connection;import java.sql.DriverManager;import java.sql.Statement;import java.util.Scanner;public class MysqlInsert {public static void main(String[] args) throws Exception {Scanner scanner = new Scanner(System.in);String user = scanner.next();String password = scanner.next();//1.通过反射加载驱动Class.forName(\"com.mysql.jdbc.Driver\");//2.建立连接Connection connection = DriverManager.getConnection(\"jdbc:mysql://master:3306/db01\",\"root\",\"123456\");//3.创建执行器Statement statement = connection.createStatement();//4.执行sql语句String sql=\"insert into user values(\"+user+\",\"+password+\")\";//5.获取结果int i = statement.executeUpdate(sql);System.out.println(i);if(i==1){System.out.println(\"登陆成功\");}else {System.out.println(\"登录失败\");}statement.close();connection.close();}}

4. MySQL安装Maven

第一步,官网下载地址 http://maven.apache.org/download.cgi (我这里安装安装的3.5.2的版本)

第二步 解压maven文件

第三步 创建一个文件夹叫 Repository

第四步开始setting.xml的配置。首先配置的第一步是本地仓库的配置,打开setting.xml(将自己刚才创建的文件目录放在这)

第五步 将镜像换成阿里的镜像

第六步重启IDEA

选择configure–>Plugin配置maven

进去之后下载maven

没下载成功原因:

  1.网络问题

  2.配置路径问题

5.配置依赖jar包

jar包配置搜索
官方地址:http://mvnrepository.com/

下面我们添加一个依赖

点击网址搜索mysql

点击进去

放到pom.xml文件中对应位置

成功

赞(0) 打赏
未经允许不得转载:爱站程序员基地 » Java操作MySQL