📄 dbwindows.java.bak
字号:
//Java core packages
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.sql.DriverManager;
// Java extension packages
import javax.swing.*;
public class dbwindows extends JFrame
{
public static void main(String args[])
{
dbwindows mcd = new dbwindows();
mcd.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
}
private Container container;
private JButton execute,reset,exit;
private JTextArea demand,result;
private JLabel l1,l2,mark;
public dbwindows()
{
super("A window for database management");
container = getContentPane();
container.setLayout(null);
execute = new JButton("Execute");
exit = new JButton("Exit");
reset = new JButton("Reset");
demand = new JTextArea("",10,30);
result = new JTextArea("",10,30);
result.setEditable(false);
l1 = new JLabel("SQL Query:");
l2 = new JLabel("Query Result:");
mark = new JLabel("Database Management Window -- 0361120");
JScrollPane js1 = new JScrollPane(demand);
JScrollPane js2 = new JScrollPane(result);
js1.setBounds(25,50,550,200);
js2.setBounds(25,290,680,300);
execute.setBounds(600,75,100,35);
reset.setBounds(600,125,100,35);
exit.setBounds(600,175,100,35);
l1.setBounds(25,25,100,25);
l2.setBounds(25,265,100,25);
mark.setBounds(400,600,300,30);
execute.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent actionEvent)
{
//result.setText("");
StringBuffer sqlbuffer = execsql(demand.getText());
result.setText(sqlbuffer.toString());
}
}
);
reset.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent actionEvent)
{
demand.setText("");
}
}
);
exit.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent actionEvent)
{
System.exit(0);
}
}
);
container.add(js1);
container.add(js2);
container.add(execute);
container.add(reset);
container.add(exit);
container.add(l1);
container.add(l2);
container.add(mark);
setSize(750, 680); //set the size of the window
setVisible(true); //make the window visible
}
/* transmit sql query to oracle to get the query results,
* and show the results in the result textarea
*/
public StringBuffer execsql(String sql)
{
StringBuffer resultString = new StringBuffer("");
try
{
System.out.print(sql+"\n");
/* connect to oracle database */
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@localhost:1521:myoracle";
String user = "db1";
String password = "db1";
Connection Con = DriverManager.getConnection(url,user,password);
Statement stmt = Con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
ResultSetMetaData rsmd = rs.getMetaData();
resultString.append("\n");
/* get each column */
int col_num = rsmd.getColumnCount();
if (col_num == 0)
{
System.out.print("无内容 \n");
return resultString;
}
System.out.print(col_num);
resultString.append(" ");
for (int i=0; i<col_num; i++)
{
resultString.append(rsmd.getColumnName(i+1)+"\t\t");
}
resultString.append("\n");
resultString.append("----------------------------------------------------\n");
int rows=0;
for (; rs.next();)
{
resultString.append(" ");
for (int i=0; i<col_num; i++)
{
String type = rsmd.getColumnTypeName(i+1);
if (type.equals("NUMBER"))
{
resultString.append(rs.getInt(i+1)+"\t\t");
}
else
{
resultString.append(rs.getString(i+1)+"\t\t");
}
}
resultString.append("\n");
rows++;
}
resultString.append("----------------------------------------------------\n");
if (rows==0)
{
resultString.append("未选定行");
}
else
resultString.append("已选定"+rows+"行");
rs.close();
stmt.close();
Con.close();
}
catch(Exception e)
{
System.err.println(e);
}
return resultString;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -