oracle数据库连接实例
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
?package OracleConnectionTool;
import java.awt.BorderLayout;
import java.awt.Choice;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.Vector;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
import javax.swing.*;
import javax.swing.border.Border;
public class OracleConnectionTool {
public static void main(String[] args) {
Visualize vis = new Visualize();
vis.setVisible(true);
Lower.input.requestFocus();
vis.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class JdbcOracle {
static Connection con = null;
String toOracle = null;
JdbcOracle(String ip_input, String port_input, String db_input,String username_input,String password_input) {
try {
String name = "oracle.jdbc.driver.OracleDriver";
Class.forName(name);
toOracle = "jdbc:oracle:thin:@" + ip_input + ":" + port_input + ":"
+ db_input;
System.out.println(toOracle);
String userName =username_input;
String pwds = password_input;
con = DriverManager.getConnection(toOracle, userName, pwds);
Lower.input.requestFocus();
rmation.setText("");
rmation.setText("数据库连接成功!");
// System.out.println(Lower.input.getText());
} catch (Exception e) {
rmation.setText("数据库连接失败!");
}
}
public static void main(String[] args) {
}
}
class Visualize extends JFrame {
Lower lower = new Lower();
Upper upper = new Upper();
JTable jt = new JTable();
JScrollPane jsb = new JScrollPane();
Visualize() {
setTitle("Demo");
setLayout(new GridLayout(3, 0,0,5));
add(upper);
add(lower);
add(jsb);
setSize(400, 400);
lower.connect.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JdbcOracle jd = new JdbcOracle(upper.ip_input.getText(),
upper.port_input.getText(), upper.db_input.getText(),
ername_input.getText(),upper.password_input.getText());
}
});
lower.input.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(JdbcOracle.con==null){
rmation.setText("请先连接数据库!");
return;
}
String temp = Lower.input.getText().trim();
temp=temp.substring(0,temp.length()-1);
boolean judge=false;
if(temp.startsWith("desc")){
String[] ss=temp.split(" ");
temp="select * from "+ss[1];
judge=true;
}
PreparedStatement ps = null;
ResultSet rs = null;
ResultSetMetaData rsmd;
try {
ps = JdbcOracle.con.prepareStatement(temp);
System.out.println(temp);
if (ps.execute()) {
System.out.println("查询");
rs = ps.getResultSet();
rsmd = rs.getMetaData();
int number = rsmd.getColumnCount();
Vector colums = new Vector();
Vector rows = new Vector();
for (int i = 1; i <= number; i++) {
colums.addElement(rsmd.getColumnName(i));
}
// rows.addElement(colums);
if(judge==false){
while (rs.next()) {
Vector currentrows = new Vector();
for (int i = 1; i <= number; i++) {
String tempagain = rs.getString(rsmd
.getColumnName(i));
currentrows.addElement(tempagain);
}
rows.addElement(currentrows);
}
}
remove(jsb);
jt = new JTable(rows, colums);
jsb = new JScrollPane(jt);
Border etched=BorderFactory.createEtchedBorder();
jsb.setBorder(etched);
add(jsb);
validate();
Lower.input.requestFocus();
rmation.setText("操作数据库成功!");
}
else {
System.out.println("执行");
Lower.input.requestFocus();
rmation.setText("操作数据库成功!");
}
} catch (Exception ex) {
rmation.setText("操作数据库失败!");
}
}
});
}
public void paint(Graphics g){
Dimension d=size();
g.drawRect(0,0,d.width-1,d.height-1);
}
public Insets insets() {
return new Insets(35, 5, 5, 5); }
}
class Upper extends JPanel {
JLabel ip = new JLabel("IP:");
JLabel port = new JLabel("PORT:");
JLabel db = new JLabel("DB Name:");
JLabel username=new JLabel("USERNAME:");
JLabel password=new JLabel("PASSWORD:");
static JTextField ip_input = new JTextField("127.0.0.1");
static JTextField port_input = new JTextField("1521");
static JTextField db_input = new JTextField("MIS");
static JTextField username_input = new JTextField("scott");
static JPasswordField password_input = new JPasswordField("tiger");
Upper() {
Border etched=BorderFactory.createEtchedBorder();
setBorder(etched);
GridBagConstraints c = new GridBagConstraints();
GridBagLayout gridbag = new GridBagLayout();
setLayout(gridbag);
c.fill = GridBagConstraints.BOTH;
c.gridwidth = 1;
c.weightx = 1.0;
gridbag.setConstraints(ip, c);
add(ip);
c.gridwidth = GridBagConstraints.REMAINDER;
gridbag.setConstraints(ip_input, c);
add(ip_input);
c.gridwidth = 1;
gridbag.setConstraints(port, c);
add(port);
c.gridwidth = GridBagConstraints.REMAINDER;
gridbag.setConstraints(port_input, c);
add(port_input);
c.gridwidth = 1;
gridbag.setConstraints(db, c);
add(db);
c.gridwidth = GridBagConstraints.REMAINDER;
gridbag.setConstraints(db_input, c);
add(db_input);
c.gridwidth = 1;
gridbag.setConstraints(username, c);
add(username);
c.gridwidth = GridBagConstraints.REMAINDER;
gridbag.setConstraints(username_input, c);
add(username_input);
c.gridwidth = 1;
gridbag.setConstraints(password, c);
add(password);
c.gridwidth = GridBagConstraints.REMAINDER;
gridbag.setConstraints(password_input, c);
add(password_input);
}
// public void paint(Graphics g){
// super.paint(g);
// Dimension d=size();
// g.drawRect(0,0,d.width-1,d.height-1); }
//
public Insets insets() {
return new Insets(5, 5, 5, 10);
}
}
class Lower extends JPanel {
JButton connect = new JButton("连接");
static JTextField input = new JTextField(); // 全局变量,获得sql语句
JComboBox choose = new JComboBox();
static JLabel information = new JLabel("");
Lower() {
Border etched=BorderFactory.createEtchedBorder();
setBorder(etched);
GridBagConstraints c = new GridBagConstraints();
GridBagLayout gridbag = new GridBagLayout();
setLayout(gridbag);
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
String[] info = { "建表", "删表", "插入", "更新" };
for (int i = 0; i < 4; i++) {
choose.addItem(info[i]);
}
// input.requestFocus();
gridbag.setConstraints(connect, c);
add(connect);
c.weightx = 3.0;
c.gridwidth = GridBagConstraints.REMAINDER;
gridbag.setConstraints(choose, c);
add(choose);
c.weightx = 3.0;
gridbag.setConstraints(input, c);
add(input);
c.gridwidth = GridBagConstraints.REMAINDER;
c.weighty = 1.0;
gridbag.setConstraints(information, c);
add(information);
choose.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Object s = choose.getSelectedItem();
System.out.println(s);
}
});
}
// public void paint(Graphics g){
// super.paint(g);
// Dimension d=size();
// g.drawRect(0,0,d.width-1,d.height-1); }
//
public Insets insets() {
return new Insets(5, 5, 5, 10);
}
}