package com.util.DBtest; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import com.hidata.generalDBconfig.OracleDBConnection; import com.util.entity.Oracle_Table_Structure; public class JDBC_Test { // 创建静态全局变量 static Connection conn; static Statement st; public static void main(String[] args) { // create(); // insert(); //插入 // update(); //更新 // delete(); //删除 // query(); //查询 String tableName = "Kang"; List<Oracle_Table_Structure> list = queryTableStruct(tableName);//查询表结构 }
/*查询表结构*/ public static List<Oracle_Table_Structure> queryTableStruct(String tableName) { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; List<Oracle_Table_Structure> result = new ArrayList<Oracle_Table_Structure>(); conn =OracleDBConnection.getConnection(); // 首先要获取连接,即连接到数据库 try { String sql = "select * from user_tab_columns where Table_Name='Kang'"; ps = conn.prepareStatement(sql); rs = ps.executeQuery(); while (rs.next()) { Oracle_Table_Structure struct = new Oracle_Table_Structure(); struct.setTable_name(rs.getString("Table_name")); struct.setData_type(rs.getString("Data_type")); struct.setData_length(rs.getString("Data_length")); struct.setColumn_name(rs.getString("Column_name")); struct.setColumn_id(rs.getString("Column_id")); struct.setChar_length(rs.getString("Char_length")); System.out.println(struct.getTable_name()+" " + struct.getColumn_name() + " " + struct.getData_type() + " "+ struct.getData_length()); result.add(struct); } OracleDBConnection.closeConection(conn, ps, rs); } catch (SQLException e) { System.out.println("查询数据失败"); } return result; } /* 创建表*/ public static void create() { // TODO Auto-generated method stub conn =OracleDBConnection.getConnection(); String sql = "create table kangtest(id number(9) not null primary key,name varchar2(40) not null)"; // 插入数据的sql语句 try { st = (Statement) conn.createStatement(); st.executeUpdate(sql); conn.close(); st.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("创建表成功"); } /* 插入记录*/ public static void insert() { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; conn =OracleDBConnection.getConnection(); int num = 0; try { String sql = "INSERT INTO kangtest(id, name)" + " VALUES (?,?)"; ps = conn.prepareStatement(sql); ps.setLong(1, num); ps.setString(2, "wufan"); ps.executeUpdate(); System.out.println("insert success"); OracleDBConnection.closeConection(conn, ps, rs); } catch (SQLException e) { System.out.println("insert failed" + e.getMessage()); } } /* 更新记录*/ public static void update() { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; conn =OracleDBConnection.getConnection(); int num = 0; try { String sql = "update kangtest set name='liuping' where id = '1'"; ps = conn.prepareStatement(sql); ps.executeUpdate(); System.out.println("update success"); conn.close(); OracleDBConnection.closeConection(conn, ps, rs); } catch (SQLException e) { System.out.println("update failed" + e.getMessage()); } } /* 查询记录*/ public static void query() { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; conn =OracleDBConnection.getConnection(); // 首先要获取连接,即连接到数据库 try { String sql = "select * from staff"; ps = conn.prepareStatement(sql); rs = ps.executeQuery(); while (rs.next()) { int id = rs.getInt("id"); int name = rs.getInt("name"); System.out.println(id + " " + name); } OracleDBConnection.closeConection(conn, ps, rs); } catch (SQLException e) { System.out.println("查询数据失败"); } } /* 删除记录*/ public static void delete() { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; conn =OracleDBConnection.getConnection(); try { String sql = "delete from staff where name = 'lili'"; st = (Statement) conn.createStatement(); int count = st.executeUpdate(sql); OracleDBConnection.closeConection(conn, ps, rs); } catch (SQLException e) { Navicat Premium 11.2.17激活 System.out.println("删除数据失败"); } } }
今天的文章
Navicat Premium 11.2.17激活(Oracle安装、Navicat for Oracle、JDBCl连接、获取表结构)分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/110803.html