Java 通过 JDBC 连接Mysql

xiaoxiao2021-02-28  75

Application通过 JDBC与 oracle, sqlserver, mysql 进行连接。 我们对driver编程, 而driver对数据的操作由JDBC实现。

一个连接的简单例子

import java.sql.*; public class Main { public static void main(String[] args) { String ClassName = "com.mysql.jdbc.Driver";//固定格式 String url = "jdbc:mysql://localhost:3306/test";//test是在mysql下自定义建立的数据库 String userName = "root";//mysql的user,就是你在安装mysql时设定的用户名,和下面的用户密码 同理 String password = "root"; Connection connect = null; try { Class.forName(ClassName); connect = DriverManager.getConnection(url, userName, password); System.out.println("222Success connent the Mysql server"); //下面开始查询 你在表里输入的内容; //statement存在安全隐患 //PreparedStatement 有预编译 String sql = "select * from user"; // String sql2 = "update user set name = '+ "+ userName + " + ' where password = '00000'"; Statement statement = connect.createStatement(); // statement.executeUpdate(); ResultSet rSet = statement.executeQuery(sql); while ( rSet.next() ) { System.out.println(rSet.getString("name")); System.out.println(rSet.getString("password")); } } catch (ClassNotFoundException e) { System.out.println("Error loading Mysql Driver"); e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } }

一:首先 你需要导入mysql jar 包 1.在eclipse 中先新建java项目, 2.然后在项目上右键 build Path -> add external archives 3.然后选择你下载好在本地的相应位置的driver

之后新建一个class类,放入上面一段代码,就能简单尝试执行。

然后对于 Statement与 PrepareStatement 的区别就是,PrepareStatement 更安全,因为它执行的是预编译; 所以用PrepareStatement的如下;

import java.sql.*; public class Main { public static void main(String[] args) { String ClassName = "com.mysql.jdbc.Driver";//固定格式 String url = "jdbc:mysql://localhost:3306/test";//test是在mysql下自定义建立的数据库 String userName = "root";//mysql的user,就是你在安装mysql时设定的用户名,和下面的用户密码 同理 String password = "root"; Connection connect = null; try { Class.forName(ClassName); connect = DriverManager.getConnection(url, userName, password); System.out.println("222Success connent the Mysql server"); //下面开始查询 你在表里输入的内容; //statement存在安全隐患 //PreparedStatement 有预编译 String sql = "select * from user"; // String sql2 = "update user set name = '+ "+ userName + " + ' where password = '00000'"; PreparedStatement statement = connect.prepareStatement(sql); // statement.executeUpdate(); ResultSet rSet = statement.executeQuery(); while ( rSet.next() ) { System.out.println(rSet.getString("name")); System.out.println(rSet.getString("password")); } } catch (ClassNotFoundException e) { System.out.println("Error loading Mysql Driver"); e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } }

再者,如果要更新,删除,啥的就比较简单了,就是简单的sql语句会就行,举个例子,更新名字为”呵呵” 的password字段;

import java.sql.*; public class Main { public static void main(String[] args) { String ClassName = "com.mysql.jdbc.Driver";//固定格式 String url = "jdbc:mysql://localhost:3306/test";//test是在mysql下自定义建立的数据库 String userName = "root";//mysql的user,就是你在安装mysql时设定的用户名,和下面的用户密码 同理 String password = "root"; Connection connect = null; try { Class.forName(ClassName); connect = DriverManager.getConnection(url, userName, password); System.out.println("222Success connent the Mysql server"); //下面开始查询 你在表里输入的内容; //statement存在安全隐患 //PreparedStatement 有预编译 String sql2 = "update user set password = '8888' where name = '呵呵'"; PreparedStatement statement = connect.prepareStatement(sql2); statement.executeUpdate(); } catch (ClassNotFoundException e) { System.out.println("Error loading Mysql Driver"); e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } }

执行结果

转载请注明原文地址: https://www.6miu.com/read-58340.html

最新回复(0)