赞
踩
今天的学习内容为jdbc
package cn.tedu.jdbc; /** * 作者:陈二胖 * 时间:2021/7/15 9:43 * 目的:TODO jdbc */ import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; /** * JDBC: * 1.什么是JDBC? * java语言与数据库交互的技术 * JDBC是java程序与关系型数据库的交互标准,提供了一套统一的操作界面 * JDBC是一组接口,制定jav程序与各种数据库软件交互的一套API * 数据库产品:Oracle/mysql/db2/sqlServer * JDBC制定的标准,程序员会根据这套标准,去写java程序 * 软件运行期间,java程序调用的是数据库厂商提供的具体的API * 数据库厂商提供的具体的驱动程序,也就实现了JDBC的标准 * JDBC程序--驱动程序----mysql * JDBC程序--驱动程序----oracle * JDBC程序--驱动程序----db2 * * 2.JDBC的编码步骤? * 1)加载驱动 * 2)创建java与数据库的链接 * 3)发送sql * 4)查询等,处理数据 * 5)关闭连接 */ //向数据库增加 public class Demo01 { public static void main(String[] args) throws ClassNotFoundException, SQLException { String className = "com.mysql.jdbc.Driver"; /** * "jdbc:mysql://localhost:3306/tedu_store?useSSL=true"; * jdbc:mysql: 设置驱动程序类 * localhost:3306 作为数据库的地址 端口号:3306 * useSSL=true 高版本的mysql安全性数据交互的选项 * 想插入中文需要characterEncoding=utf8 * uesr:数据库的用户名 * password:数据库密码 */ String url = "jdbc:mysql://localhost:3306/tedu_store?characterEncoding=utf8"; //固定写法 String user = "root"; String password = "root"; //1.加载驱动程序 Class.forName(className); System.out.println("加载成功!!!"); //2.创建连接 Connection con = DriverManager.getConnection(url,user,password); System.out.println(con); System.out.println("已经成功与数据库进行建立!!!"); //3.发送sql语句 // Statement语句,发送并执行sql语句 Statement st = con.createStatement(); String sql = "INSERT INTO t_user(username,password,email,phone,image,gander,created_user,created_time,modified_user,modified_time\n) " + "VALUES ('郑','123123','23333','13733086271',1,1,'1001','2021-07-15','1001','2021-07-15')"; //自己写 int row = st.executeUpdate(sql); if (row>0){ System.out.println("数据注册成功!"); } //4.关闭 if (con!=null){ con.close(); } } }
package cn.tedu.jdbc; /** * 作者:陈二胖 * 时间:2021/7/15 9:43 * 目的:TODO jdbc */ import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; /** * 2.JDBC的编码步骤? * 1)加载驱动 * 2)创建java与数据库的链接 * 3)发送sql * 4)查询等,处理数据 * 5)关闭连接 */ //从数据库删除 public class Demo02 { public static void main(String[] args) throws ClassNotFoundException, SQLException { String className = "com.mysql.jdbc.Driver"; /** * "jdbc:mysql://localhost:3306/tedu_store?useSSL=true"; * jdbc:mysql: 设置驱动程序类 * localhost:3306 作为数据库的地址 端口号:3306 * useSSL=true 高版本的mysql安全性数据交互的选项 * 想插入中文需要characterEncoding=utf8 * uesr:数据库的用户名 * password:数据库密码 */ String url = "jdbc:mysql://localhost:3306/tedu_store?characterEncoding=utf8"; //固定写法 String user = "root"; String password = "root"; //1.加载驱动程序 Class.forName(className); System.out.println("加载成功!!!"); //2.创建连接 Connection con = DriverManager.getConnection(url,user,password); System.out.println(con); System.out.println("已经成功与数据库进行建立!!!"); //3.发送sql语句 // Statement语句,发送并执行sql语句 Statement st = con.createStatement(); String sql = "DELETE FROM t_user WHERE id = 9"; int row = st.executeUpdate(sql); if (row>0){ System.out.println("删除成功!"); } //4.关闭 if (con!=null){ con.close(); } } }
package cn.tedu.jdbc; /** * 作者:陈二胖 * 时间:2021/7/15 9:43 * 目的:TODO jdbc */ import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; /** * 2.JDBC的编码步骤? * 1)加载驱动 * 2)创建java与数据库的链接 * 3)发送sql * 4)查询等,处理数据 * 5)关闭连接 */ //修改密码 public class Demo03 { public static void main(String[] args) throws ClassNotFoundException, SQLException { String className = "com.mysql.jdbc.Driver"; /** * "jdbc:mysql://localhost:3306/tedu_store?useSSL=true"; * jdbc:mysql: 设置驱动程序类 * localhost:3306 作为数据库的地址 端口号:3306 * useSSL=true 高版本的mysql安全性数据交互的选项 * 想插入中文需要characterEncoding=utf8 * uesr:数据库的用户名 * password:数据库密码 */ String url = "jdbc:mysql://localhost:3306/tedu_store?characterEncoding=utf8"; //固定写法 String user = "root"; String password = "root"; //1.加载驱动程序 Class.forName(className); System.out.println("加载成功!!!"); //2.创建连接 Connection con = DriverManager.getConnection(url,user,password); System.out.println(con); System.out.println("已经成功与数据库进行建立!!!"); //3.发送sql语句 // Statement语句,发送并执行sql语句 Statement st = con.createStatement(); String sql = "UPDATE t_user SET password = '12312' WHERE id = 7"; int row = st.executeUpdate(sql); if (row>0){ System.out.println("修改密码成功!"); } //4.关闭 if (con!=null){ con.close(); } } }
package cn.tedu.jdbc; /** * 作者:陈二胖 * 时间:2021/7/15 9:43 * 目的:TODO jdbc */ import java.sql.*; /** * 2.JDBC的编码步骤? * 1)加载驱动 * 2)创建java与数据库的链接 * 3)发送sql * 4)查询等,处理数据 * 5)关闭连接 */ //查询数据库信息 public class Demo04 { public static void main(String[] args) throws ClassNotFoundException, SQLException { String className = "com.mysql.jdbc.Driver"; /** * "jdbc:mysql://localhost:3306/tedu_store?useSSL=true"; * jdbc:mysql: 设置驱动程序类 * localhost:3306 作为数据库的地址 端口号:3306 * useSSL=true 高版本的mysql安全性数据交互的选项 * 想插入中文需要characterEncoding=utf8 * uesr:数据库的用户名 * password:数据库密码 */ String url = "jdbc:mysql://localhost:3306/tedu_store?characterEncoding=utf8"; //固定写法 String user = "root"; String password = "root"; //1.加载驱动程序 Class.forName(className); System.out.println("加载成功!!!"); //2.创建连接 Connection con = DriverManager.getConnection(url,user,password); System.out.println(con); System.out.println("已经成功与数据库进行建立!!!"); //3.发送sql语句 // Statement语句,发送并执行sql语句 Statement st = con.createStatement(); String sql = "SELECT * FROM t_user"; ResultSet rs = st.executeQuery(sql); //查询信息用executeQuery() //4.处理结果集对象 while (rs.next()){ String username = rs.getString("username"); Timestamp created_time = rs.getTimestamp("created_time"); System.out.println(username + "\t"+ created_time); } //5.关闭 if (con!=null){ con.close(); } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。