1.操作的环境 STS,mysql,oracle
orcle 所操作的数据库名为 ORCL 表为 m_stu
表结构如下
mysql 的表为:my_stu
表结构如下
工具类完整代码
package com.briup.jdbc; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement; import com.mysql.jdbc.PreparedStatement;
import com.sun.javafx.geom.AreaOp.AddOp; public class jdbcUtils {
// 获取连接
public static Connection getConnection(String str) { if ("mysql".equals(str)) {
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://127.0.0.1:3306/my_database?characterEncoding=UTF-8";
String username = "root";
String password = "yue170305";
try {
Connection conn = DriverManager.getConnection(url, username, password);
System.out.println("连接到mysql数据库");
return conn;
} catch (SQLException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} }
if ("oracle".equals(str)) {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@localhost:1521:ORCL";
String username = "scott";
String password = "yue170305";
try {
Connection conn = DriverManager.getConnection(url, username, password);
System.out.println("连接到orcle数据库");
return conn;
} catch (SQLException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} }
return null; } // 删除数据
public static void delete(Connection conn, String sql) {
try {
Statement statement = conn.createStatement();
try {
statement.execute(sql);
System.out.println("删除数据成功!");
} catch (Exception e) {
System.err.println("出现问题了" + e.getMessage());
e.printStackTrace();
} finally {
close(statement, null, conn);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
//修改一条数据
public static void Update(Connection conn, String sql) {
try {
Statement statement = conn.createStatement();
try {
statement.execute(sql);
System.out.println("修改数据成功");
} catch (Exception e) {
System.err.println("出现问题了" + e.getMessage());
e.printStackTrace();
} finally {
close(statement, null, conn);
} } catch (SQLException e) {
e.printStackTrace();
}
}
//查询记录
public static void select(Connection conn, String sql) {
try {
Statement statement = conn.createStatement();
ResultSet res = statement.executeQuery(sql);
while(res.next()) {
int id=res.getInt("id");
String name=res.getString("name");
int age=res.getInt("age");
String sex=res.getString("sex");
System.out.println("id="+id+","+"name="+name+","+"age="+age+","+"sex="+sex);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
close(null, null, conn);
} }
// 添加一条数据
public static void add(Connection conn, String sql) {
try {
Statement statement = conn.createStatement();
try {
statement.execute(sql);
System.out.println("插入数据成功!");
} catch (Exception e) {
System.err.println("出现问题了" + e.getMessage());
e.printStackTrace();
} finally {
close(statement, null, conn);
} } catch (SQLException e) {
e.printStackTrace();
}
} // 关闭连接
public static void close(Statement st, ResultSet rs, Connection conn) {
if (st != null) {
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
} } }
测试类代码
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); }
}