写这个文章的原因是为了巩固自己连接数据库步骤
数据库连接步骤加载驱动程序,获取数据库连接对象
package cn.sg.xx.test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class Main { public static void main(String[] args) throws SQLException, ClassNotFoundException { try { //数据库驱动参数 String Driver = "com.mysql.cj.jdbc.Driver";//"com.mysql.jdbc.Driver"-->驱动地址已经过时 //连接的URL,数据库名为test String URL = "jdbc:mysql://localhost:3306/test?serverTimezone=UTC&characterEncoding=utf8";//后边的参数serverTimezone-->指定时区,characterEncoding-->编码格式 //数据库用户名 String Username = "root"; //数据库密码 String Password = "123"; //加载MySQL驱动程序 Class.forName(Driver); //与MySQL数据库创立连接 Connection con = null; con = DriverManager.getConnection(URL, Username, Password); } catch (ClassNotFoundException e) { System.out.println("驱动程序没有找到!"); } catch (SQLException e) { System.out.println("SQL异常!"); } } }
String sql = "INSERT INTO test(id,NAME,sex) VALUES(?,?,?);";
建议写成这种语法,指定插入的列,可以防止表字段修改后,程序报错
使用PreparedStatement 进行sql语句的装配,可以防止sql注入
package cn.sg.xx.test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; public class Main { public static void main(String[] args) { try { //数据库驱动参数 String Driver = "com.mysql.cj.jdbc.Driver";//"com.mysql.jdbc.Driver"-->驱动地址已经过时 //连接的URL,数据库名为test String URL = "jdbc:mysql://localhost:3306/test?serverTimezone=UTC&characterEncoding=utf8";//后边的参数serverTimezone-->指定时区,characterEncoding-->编码格式 //数据库用户名 String Username = "root"; //数据库密码 String Password = "123"; //加载MySQL驱动程序 Class.forName(Driver); //与MySQL数据库创立连接 Connection con = null; con = DriverManager.getConnection(URL, Username, Password); //sql语句装配,?为占位符 String sql = "INSERT INTO test(id,NAME,sex) VALUES(?,?,?);"; PreparedStatement stmt = con.prepareStatement(sql); stmt.setInt(1, 2); stmt.setString(2, "张三"); stmt.setString(3, "男"); stmt.executeUpdate(); //执行完数据库的操作有指令之后要释放资源,否则会导致内存的溢出 stmt.close(); con.close(); } catch (ClassNotFoundException e) { System.out.println("驱动程序没有找到!"); } catch (SQLException e) { System.out.println("SQL异常!"); } } }
删除表数据的操作和插入操作基本相同,只有sql语句不同
String sql = "DELETE FROM test WHERE id = 1"; PreparedStatement stmt = con.prepareStatement(sql); stmt.executeUpdate();
同插入数据一样,仅仅修改sql语句为
表数据的查找需要比上边的插入、删除操作多关闭ResoultSet资源
ResoultSet rs;
rs.close();
//查找表数据 String sql = "SELECT * FROM test"; PreparedStatement stmt = con.prepareStatement(sql); ResultSet rs = stmt.executeQuery(); //从 rs 获取数据 while (rs.next()) { //rs.getMetaData().getColumnCount() --> 获取表的列数 for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) { String colunmName = rs.getMetaData().getColumnName(i);//获取列名 Object temp = rs.getString(i);//获取该列对应元素的值 System.out.print(colunmName + "-->" + temp + "/t"); } System.out.println();
package cn.sg.xx.test; import java.sql.*; public class Main { public static void main(String[] args) throws SQLException { Connection con = null; PreparedStatement stmt = null; ResultSet rs = null; try { //数据库驱动参数 String Driver = "com.mysql.cj.jdbc.Driver";//"com.mysql.jdbc.Driver"-->驱动地址已经过时 //连接的URL,数据库名为test String URL = "jdbc:mysql://localhost:3306/test?serverTimezone=UTC&characterEncoding=utf8";//后边的参数serverTimezone-->指定时区,characterEncoding-->编码格式 //数据库用户名 String Username = "root"; //数据库密码 String Password = "123"; //加载MySQL驱动程序 Class.forName(Driver); //与MySQL数据库创立连接 con = DriverManager.getConnection(URL, Username, Password); //sql语句装配,?为占位符 String sql = "INSERT INTO test(id,NAME,sex) VALUES(?,?,?);"; stmt = con.prepareStatement(sql); stmt.setInt(1, 2); stmt.setString(2, "张三"); stmt.setString(3, "男"); stmt.executeUpdate(); //删除表数据 sql = "DELETE FROM test WHERE id = 1"; stmt = con.prepareStatement(sql); stmt.executeUpdate(); //修改表数据 sql = "UPDATE test SET sex = '女' WHERE id = 1"; stmt = con.prepareStatement(sql); stmt.executeUpdate(); //查找表数据 sql = "SELECT * FROM test"; stmt = con.prepareStatement(sql); rs = stmt.executeQuery(); //从 rs 获取数据 while (rs.next()) { //rs.getMetaData().getColumnCount() --> 获取表的列数 for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) { String colunmName = rs.getMetaData().getColumnName(i);//获取列名 Object temp = rs.getString(i);//获取该列对应元素的值 System.out.print(colunmName + "-->" + temp + "/t"); } System.out.println(); } //执行完数据库的操作有指令之后要释放资源,否则会导致内存的溢出 } catch (ClassNotFoundException e) { System.out.println("驱动程序没有找到!"); } catch (SQLException e) { System.out.println("SQL异常!"); } finally { //关闭资源 if (rs != null) rs.close(); if (stmt != null) stmt.close(); if (con != null) con.close(); } } }