1.操作的环境 STS,mysql,oracle
orcle 所操作的数据库名为 ORCL 表为 m_stu
表结构如下
mysql 的表为:my_stu
表结构如下
工具类完整代码
1 package com.briup.jdbc; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.sql.Statement; 8 9 import com.mysql.jdbc.PreparedStatement; 10 import com.sun.javafx.geom.AreaOp.AddOp; 11 12 public class jdbcUtils { 13 // 获取连接 14 public static Connection getConnection(String str) { 15 16 if ("mysql".equals(str)) { 17 try { 18 Class.forName("com.mysql.jdbc.Driver"); 19 String url = "jdbc:mysql://127.0.0.1:3306/my_database?characterEncoding=UTF-8"; 20 String username = "root"; 21 String password = "yue170305"; 22 try { 23 Connection conn = DriverManager.getConnection(url, username, password); 24 System.out.println("连接到mysql数据库"); 25 return conn; 26 } catch (SQLException e) { 27 e.printStackTrace(); 28 } 29 } catch (ClassNotFoundException e) { 30 e.printStackTrace(); 31 } 32 33 } 34 if ("oracle".equals(str)) { 35 try { 36 Class.forName("oracle.jdbc.driver.OracleDriver"); 37 String url = "jdbc:oracle:thin:@localhost:1521:ORCL"; 38 String username = "scott"; 39 String password = "yue170305"; 40 try { 41 Connection conn = DriverManager.getConnection(url, username, password); 42 System.out.println("连接到orcle数据库"); 43 return conn; 44 } catch (SQLException e) { 45 e.printStackTrace(); 46 } 47 } catch (ClassNotFoundException e) { 48 e.printStackTrace(); 49 } 50 51 } 52 return null; 53 54 } 55 56 // 删除数据 57 public static void delete(Connection conn, String sql) { 58 try { 59 Statement statement = conn.createStatement(); 60 try { 61 statement.execute(sql); 62 System.out.println("删除数据成功!"); 63 } catch (Exception e) { 64 System.err.println("出现问题了" + e.getMessage()); 65 e.printStackTrace(); 66 } finally { 67 close(statement, null, conn); 68 } 69 } catch (SQLException e) { 70 e.printStackTrace(); 71 } 72 } 73 //修改一条数据 74 public static void Update(Connection conn, String sql) { 75 try { 76 Statement statement = conn.createStatement(); 77 try { 78 statement.execute(sql); 79 System.out.println("修改数据成功"); 80 } catch (Exception e) { 81 System.err.println("出现问题了" + e.getMessage()); 82 e.printStackTrace(); 83 } finally { 84 close(statement, null, conn); 85 } 86 87 } catch (SQLException e) { 88 e.printStackTrace(); 89 } 90 } 91 //查询记录 92 public static void select(Connection conn, String sql) { 93 try { 94 Statement statement = conn.createStatement(); 95 ResultSet res = statement.executeQuery(sql); 96 while(res.next()) { 97 int id=res.getInt("id"); 98 String name=res.getString("name"); 99 int age=res.getInt("age"); 100 String sex=res.getString("sex"); 101 System.out.println("id="+id+","+"name="+name+","+"age="+age+","+"sex="+sex); 102 } 103 } catch (SQLException e) { 104 e.printStackTrace(); 105 }finally { 106 close(null, null, conn); 107 } 108 109 } 110 // 添加一条数据 111 public static void add(Connection conn, String sql) { 112 try { 113 Statement statement = conn.createStatement(); 114 try { 115 statement.execute(sql); 116 System.out.println("插入数据成功!"); 117 } catch (Exception e) { 118 System.err.println("出现问题了" + e.getMessage()); 119 e.printStackTrace(); 120 } finally { 121 close(statement, null, conn); 122 } 123 124 } catch (SQLException e) { 125 e.printStackTrace(); 126 } 127 } 128 129 // 关闭连接 130 public static void close(Statement st, ResultSet rs, Connection conn) { 131 if (st != null) { 132 try { 133 st.close(); 134 } catch (SQLException e) { 135 e.printStackTrace(); 136 } 137 } 138 if (rs != null) { 139 try { 140 rs.close(); 141 } catch (SQLException e) { 142 e.printStackTrace(); 143 } 144 } 145 if (conn != null) { 146 try { 147 conn.close(); 148 } catch (SQLException e) { 149 e.printStackTrace(); 150 } 151 } 152 153 } 154 155 }
测试类代码
package com.briup.jdbc; import java.sql.Connection; public class Test { public static void main(String[] args) { //连接到mysql数据库 // Connection connection = jdbcUtils.getConnection("mysql"); // String sql="insert into my_stu values(8,'dadaming',108,'m')"; //jdbcUtils.add(connection, sql); //删除数据(mysql) // String sql2="delete from my_stu where id=1"; // jdbcUtils.delete(connection, sql2); //连接到oracle数据库 Connection connection2 = jdbcUtils.getConnection("oracle"); String sql1="insert into m_stu values(7,'lifd',15,'n')"; String sql2="delete from m_stu where id=1"; String sql3=" update m_stu " + "set name='李悦'" + "where id=1"; String sql4="select * from m_stu"; //jdbcUtils.add(connection2, sql1); //jdbcUtils.delete(connection2, sql2); //jdbcUtils.Update(connection2, sql3); jdbcUtils.select(connection2, sql4); } }