IDEA使用Maven连接MySQL的jdbc驱动操作数据库

您所在的位置:网站首页 idea数据库连接配置文件 IDEA使用Maven连接MySQL的jdbc驱动操作数据库

IDEA使用Maven连接MySQL的jdbc驱动操作数据库

2023-05-12 19:37| 来源: 网络整理| 查看: 265

Java中使用JDBC连接数据库

加载驱动(告诉Java程序,即将要链接的是哪中类型的数据库)

获取链接【创建数据库连接】(表示JVM的进程和数据库进程之间的通道打开了,数据进程之间通信,重量级的,使用完成之后一定要关闭进程)

获取数据库操作对象【创建执行sql的对象】

执行语句【DQL,DML...】

处理执行结果【数据结果集】(只用在执行查询SELECT语句时,才会处理查询结果集)

释放资源(使用完资源后一i定要关闭资源)

pox.xml文件配置(下载驱动) 1 2 3 mysql 4 mysql-connector-java 5 5.1.46 6 7

 

代码示例一:

直接注册驱动操作数据库

此方法操作链接数据库不推荐使用,操作不方便,更不好维护,数据库之间切换繁琐

import java.sql.*; public class ChengDatebaseInsert { public static void main(String[] arge) { Connection coon = null; PreparedStatement statement = null; try { //注册驱动 Class.forName("com.mysql.jdbc.Driver"); //获取数据库连接(里面依次是jdbc:mysql://主机号:端口号/数据库名),用户名,用户密码 try { coon = DriverManager.getConnection("jdbc:mysql://localhost:3306/center?serverTimezone=UTC&userSSL=false" , "root", "123456"); System.out.println(coon + "获取成功!"); //需要执行的sql语句 String sql = "insert INTO center_user(CENTER_NAME,CENTER_AGE,CENTER_SEX) VALUES(?,?,?) "; //获取预处理对象 statement = coon.prepareCall(sql); statement.setString(1, "王老虎"); statement.setInt(2, 12); statement.setString(3, "男"); //执行sql(执行几行,返回几条记录) int updatecont = statement.executeUpdate(); System.out.println("更新数据总数据是:" + updatecont); } catch (SQLException throwables) { throwables.printStackTrace(); } } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { //关闭jdbc连接 if (statement != null) { try { statement.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } if (coon != null) { try { coon.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } } } }

第二种(新手推荐,实际框架中我们不建议这么定义):

package com.guisha.JDBC; import java.sql.*; public class JDBCTest { public static void main(String[] agre) throws SQLException { PreparedStatement stam = null; Connection conn = null; try { //注册驱动(常用,不需要接受返回值,我们只需要这个类加载动作) Class.forName("com.mysql.jdbc.Driver"); //获取链接 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/user", "root", "guisha"); //许哟啊执行的sql语句 String sql = "update center_user set user_name='王老头' where user_age='21' "; //创建数据库操作对象 stam = conn.prepareStatement(sql); // executeUpdate 执行数据SQL更新 //count 返回执行的条数 int count = stam.executeUpdate(); System.out.println(count == 1 ? "修改成功!" : "修改失败!"); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { //关闭资源 if (stam != null) { stam.close(); } if (conn != null) { conn.close(); } } } } 先了解下简单的DELETE和UPDATE操作

JDBC操作Delete:

package com.guisha.JDBC; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class JdbcDeleteTest { public static void main(String[] args) { //将数据库配置定义为变量(开发中我们定义到properties配置文件中去) String Driver = "jdbc:mysql://localhost:3306/user"; String name = "root"; String password = "guisha"; Connection coon = null; Statement strm = null; try { //注册驱动 Class.forName("com.mysql.jdbc.Driver"); //获取链接 try { coon = DriverManager.getConnection(Driver, name, password); //创建数据库对象操作对象 strm = coon.createStatement(); //创建需要执行的sql语句 String sql = "delete from center_user where USER_NAME='王琳'"; //执行更新操作 int count = strm.executeUpdate(sql); System.out.println(count == 1 ? "删除成功!" : "删除失败!"); } catch (SQLException e) { e.printStackTrace(); } } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { if (strm != null) { try { strm.close(); } catch (SQLException e) { e.printStackTrace(); } } if (coon != null) { try { coon.close(); } catch (SQLException e) { e.printStackTrace(); } } } } }

JDBC操作Update:

package com.guisha.JDBC; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class JdbcDeleteTest { public static void main(String[] args) { //将数据库配置定义为变量(开发中我们定义到properties配置文件中去) String Driver = "jdbc:mysql://localhost:3306/user"; String name = "root"; String password = "guisha"; Connection coon = null; Statement strm = null; try { //注册驱动 Class.forName("com.mysql.jdbc.Driver"); //获取链接 try { coon = DriverManager.getConnection(Driver, name, password); //创建数据库对象操作对象 strm = coon.createStatement(); //创建需要执行的sql语句 String sql = "Update center_user set USER_AGE='19' where USER_NAME='王老头'"; //执行更新操作 int count = strm.executeUpdate(sql); System.out.println(count == 1 ? "更新成功!" : "更新失败!"); } catch (SQLException e) { e.printStackTrace(); } } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { if (strm != null) { try { strm.close(); } catch (SQLException e) { e.printStackTrace(); } } if (coon != null) { try { coon.close(); } catch (SQLException e) { e.printStackTrace(); } } } } }

  

 

代码示例二: 新建数据库配置文件,获取配置文件信息后注册数据库驱动 利用反射获取新建的数据库配置文件db.properties里的配置信息 示例一: 1 driver=com.mysql.jdbc.Driver 2 url=jdbc:mysql://localhost:3306/user 3 user=root 4 password=guisha

 

package com.guisha.JDBC; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import java.util.ResourceBundle; /** * 方法描述 * 注意:实际开发中不建议把数据库链接数据库的信息写死到程序中 * @since: 1.0.0 * @author: Mr.cheng */ public class Properties { public static void main(String[] args) { //使用资源绑定器绑定属性配置文件 ResourceBundle bundle = ResourceBundle.getBundle("db"); String driver = bundle.getString("driver"); String url = bundle.getString("url"); String user = bundle.getString("user"); String password = bundle.getString("password"); Connection coon = null; Statement strm = null; try { //注册驱动 Class.forName(driver); //获取链接 try { coon = DriverManager.getConnection(url, user, password); //创建数据库对象操作对象 strm = coon.createStatement(); //创建需要执行的sql语句 String sql = "Update center_user set USER_AGE='18' where USER_NAME='王老头'"; //执行更新操作 int count = strm.executeUpdate(sql); System.out.println(count == 1 ? "更新成功!" : "更新失败!"); } catch (SQLException e) { e.printStackTrace(); } } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { if (strm != null) { try { strm.close(); } catch (SQLException e) { e.printStackTrace(); } } if (coon != null) { try { coon.close(); } catch (SQLException e) { e.printStackTrace(); } } } } }

  

示例二: driver=com.mysql.jdbc.Driver mysql_url=jdbc:mysql://localhost:3306/center mysql_user=root mysql_password=123456

 

import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.Properties; public class ChengJdbcTest { private static String driver; private static String mysql_url; private static String mysql_user; private static String mysql_password; static { //新建属性对象 Properties property = new Properties(); //通过反射,新建字符输入流,读取mysql.properties文件 InputStream input = ChengJdbcTest.class.getClassLoader().getResourceAsStream("db.properties"); //将输入流中读取到的属性,加载到properties属性及对象中 try { //将input 加载到property对象中 property.load(input); //根据键,获取properties中对应的值赋值 driver = property.getProperty("driver"); mysql_url = property.getProperty("mysql_url"); mysql_user = property.getProperty("mysql_user"); mysql_password = property.getProperty("mysql_password"); } catch (IOException e) { e.printStackTrace(); } } //返回数据库连接 public static Connection getConnection(){ try { //加载驱动 Class.forName(driver); try { //连接数据库 Connection connection = DriverManager.getConnection(mysql_url, mysql_user, mysql_password); //返回值 return connection; } catch (SQLException throwables) { throwables.printStackTrace(); } } catch (ClassNotFoundException e) { e.printStackTrace(); } return null; } }

  

import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; public class TestMysql { public static void main(String[] args) throws SQLException { //获取数据库连接 Connection connection = ChengJdbcTest.getConnection(); //SQL语句 String sql = "insert INTO center_user(CENTER_NAME,CENTER_AGE,CENTER_SEX) VALUES(?,?,?) "; //执行SQL PreparedStatement statement = connection.prepareCall(sql); //数据库传值 statement.setString(1, "小程"); statement.setInt(2, 12); statement.setString(3, "男"); //更新返回的条数,返回Integer int update = statement.executeUpdate(); System.out.println("更新完成:" + update); //关闭资源 if (statement != null) { statement.close(); } if (connection != null) { connection.close(); } } }

  

 

  下一篇: JDBC实现简单登录功能


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3