package client;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import dao.impl.*;
public class SqlServer {
JFrame frame;
MainPanel mp=new MainPanel();
CardLayout layout=new CardLayout();
InputInterface input=new InputInterface();
/*
* @sqlclient
*/
public SqlServer(){
frame = new JFrame("SQL查询器 Q9");
frame.setSize(350,275);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(layout);
frame.add("main",mp);
frame.add("second",input);
frame.setVisible(true);
addListeners();
}
private void addListeners(){
mp.getOkbutton().addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
check();
}
});
input.getOkbutton().addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
sendsql();
}
});
}
public void check(){
String driver=mp.getServerfield().getText();
String ip=mp.getIpfield().getText();
String namefield=mp.getNamefield().getText();
String passfield=mp.getPassfield().getText();
int i=1;
if(ip.length()<=1){
JOptionPane.showMessageDialog(frame, "请输入数据库IP地址");
i++;
}else if(namefield.length()<=1){
JOptionPane.showMessageDialog(frame, "请输入登入账户名");
i++;
}else if(passfield.length()<=1){
JOptionPane.showMessageDialog(frame, "登入密码不能为空");
i++;
}
if(i==1){
if(ip.equals("192.168.0.206")){
if(namefield.equals("sd0702")){
if(passfield.equals("sd0702")){
JOptionPane.showMessageDialog(frame, "登录成功");
layout.show(frame.getContentPane(), "second");
frame.setSize(700,700);
}else{
JOptionPane.showMessageDialog(frame, "密码有误");
}
}else{
JOptionPane.showMessageDialog(frame, "账号有误");
}
}else{
JOptionPane.showMessageDialog(frame, "IP有误");
}
}
// int j=0;
// if(i==1){
// if(ip.equals("192.168.0.206")){
// j++;
// }
// if(namefield.equals("sd0702")){
// j++;
// }
// if(passfield.equals("sd0702")){
// j++;
// }
// if(j==3){
// JOptionPane.showMessageDialog(frame, "登录成功");
// layout.show(frame.getContentPane(), "second");
// }else{
// JOptionPane.showMessageDialog(frame, "输入有误");
// }
// }
}
public void sendsql(){
try {
// String driver=null;
// driver=mp.getServerfield().getText();
String ip=null;
ip=mp.getIpfield().getText();
String namefield=mp.getNamefield().getText();
String passfield=mp.getPassfield().getText();
if(mp.getServerbox().getSelectedIndex()==0){
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@"+ip+":1521:tarena",namefield,passfield);
String sql=null;
sql=input.getCommondfield().getText();
input.getCommondfield().setText("");
try{
Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet rs=stmt.executeQuery(sql);
ResultSetMetaData mdtars=rs.getMetaData();
int columnNumber=mdtars.getColumnCount();
input.getCommondarea().setText("");
for(int i=1; i<=columnNumber; i++){
input.getCommondarea().append(mdtars.getColumnName(i)+"/t");
}
input.getCommondarea().append("/n");
while(rs.next()){
for(int i=1; i<=columnNumber; i++){
input.getCommondarea().append(rs.getString(i)+"/t");
}
input.getCommondarea().append("/n");
}
rs.last();
input.getCommondarea().append(rs.getRow()+" rows selected."+"/n");
rs.close();
}catch(Exception e){
JOptionPane.showMessageDialog(frame,"请输select命令");
}finally{
try{
con.close();
}catch(Exception e){
}
}
}
if(mp.getServerbox().getSelectedIndex()==1){
Class.forName("com.mysql.jdbc.Driver");
// Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test",
// "root","298211");
Connection con=DriverManager.getConnection("jdbc:mysql://"+ip+":3306/test",
namefield,passfield);
//sql Message
}
} catch (ClassNotFoundException e) {
JOptionPane.showMessageDialog(frame,e.getMessage());
} catch (SQLException e) {
JOptionPane.showMessageDialog(frame,e.getMessage());
}
}
public static void main(String[] args) {
new SqlServer();
}
}