一直以来都很喜欢开源的东东,对Mysql数据库情有独钟,对于一个在windows下的Mysql用户来说,Naticat让我们可以很方便的使用Mysql。于是特别想用java写一个类似的东东,一来为了能跨平台,二来呢也是因为ubuntu的那个MySQL Navigator居然让用户自己写SQL,这让我非常的郁闷。最近不是刚签了HAND,于是终于能有点时间,码点代码。用了二天时间写了这个浏览程序,为了方便我把Mysql的用户和密码写死在程序里面了,而且没有实现更新数据库的操作。但是总算是可以拿出来了。
好了言归正转,为了方便我先把代码paste上来。如果各位看官有人能有更好的设计思路麻烦回复告诉下,:-),争取能完善这个程序。然后我会谈点这个东东设计时候的一点个人想法。请各位高手批评指正。
首先是一个connectfactory.java的工厂类,为了能为我们产生一个一个数据库连接(新手注意,跑这个程序你需要一个mysql的jdbc Driver,CSDN可以下到)。
package com.tools;
import java.sql.Connection;
import java.sql.DriverManager;
import com.tools.connectfactory;
public class connectfactory {
public static void main(String[] args)
{
Connection conn = connectfactory.getconnect();
if(conn!=null)
System.out.println("ok");
}
public static Connection getconnect()
{
Connection conn = null;
try{
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/?user=root&password=root";
conn = DriverManager.getConnection(url);
}
catch(Throwable e)
{
e.printStackTrace();
}
return conn;
}
}
然后就是主体了
//数据库选择
package com.compoment;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JComboBox;
import javax.swing.JPanel;
/**
* @version 1.0
* @author liangdong
*
*/
public class DataPanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
}
public DataPanel(final Connection conn) {
try {
Statement stmt = conn.createStatement();
String sql = "show databases;";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
box.addItem(rs.getString(1));
}
rs.close();
stmt.close();
} catch (Throwable e) {
e.printStackTrace();
}
box.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String name = (String) box.getSelectedItem();
setDataname(name);
try {
Statement stmt = conn.createStatement();
stmt.execute("use " + name);
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
});
add(new Label("数据库名 :"));
add(box);
}
public String getDataname() {
return dataname;
}
public void setDataname(String dataname) {
this.dataname = dataname;
}
private JComboBox box = new JComboBox();
private String dataname = "";
}
//表选择
package com.compoment;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;
/**
* @version 1.0
* @author liangdong
*
*/
public class TablePanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
public TablePanel(final Connection conn) {
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
Statement stmt = conn.createStatement();
String sql = "show tables;";
ResultSet rs = stmt.executeQuery(sql);
box.removeAllItems();
while (rs.next()) {
box.addItem(rs.getString(1));
}
rs.close();
stmt.close();
} catch (Throwable e) {
e.printStackTrace();
}
}
});
add(button);
add(new Label("选择表"));
box.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name = (String) box.getSelectedItem();
setTablename(name);
}
});
add(box);
}
public String getTablename() {
return tablename;
}
public void setTablename(String tablename) {
this.tablename = tablename;
}
private JComboBox box = new JComboBox();
private JButton button = new JButton("显示表");
private String tablename = "";
}
//查询执行,数据表显示
package com.compoment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
/**
* @version 1.0
* @author liangdong
*
*/
public class ResPanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
}
public ResPanel(final Connection conn, final JPanel tp) {
model = new TModel();
table = new JTable(model);
button = new Mbutton(conn, tp, model);
JScrollPane scrollpane = new JScrollPane(table);
tp.add(button);
add(scrollpane);
}
private JButton button;
private JTable table;
private TModel model;
}
class TModel extends AbstractTableModel {
private static final long serialVersionUID = 1L;
public TModel() {
store = new String[MAXR][MAXC];
names = new String[MAXC];
}
public int getColumnCount() {
return c;
}
public int getRowCount() {
return r;
}
public Object getValueAt(int arg0, int arg1) {
return store[arg0][arg1];
}
public String getColumnName(int columnIndex) {
return names[columnIndex];
}
public void setName(String s1, int pos) {
names[pos] = s1;
}
public void setC(int c) {
this.c = c;
}
public void setR(int r) {
this.r = r;
}
public void setRC(String rs, int r1, int c1) {
store[r1][c1] = rs;
}
private int r = 0;
private int c = 0;
private String[][] store;
private String[] names;
public static final int MAXR = 1000;
public static final int MAXC = 100;
}
class Mbutton extends JButton {
/**
*
*/
private static final long serialVersionUID = 1L;
public Mbutton(final Connection conn, final JPanel tp, final TModel m1) {
this.setText("查表");
this.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String tablename = ((TablePanel) tp).getTablename();
String sql = "select * from " + tablename;
try {
int R = 0, C = 0;
Statement stmt = conn.createStatement();
ResultSet rs1 = stmt.executeQuery("desc " + tablename);
while (rs1.next()) {
m1.setName(rs1.getString(1), C);
C++;
}
m1.setC(C);
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
for (int i = 0; i < C; i++) {
m1.setRC(rs.getString(i + 1), R, i);
}
R++;
}
m1.setR(R);
m1.fireTableStructureChanged();
} catch (SQLException e) {
e.printStackTrace();
}
}
});
}
}
//主程序入口
package com.compoment;
import java.awt.BorderLayout;
import java.sql.Connection;
import javax.swing.JFrame;
import javax.swing.JPanel;
import com.tools.connectfactory;
/**
* @version 1.0
* @author liangdong
*
*/
public class databases {
public static void main(String[] args) {
JFrame frame = new DFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class DFrame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
public DFrame() {
setTitle("Frame");
setBounds(STARTX, STARTY, DEFAULT_WIDTH, DEFAULT_HEIGHT);
conn = connectfactory.getconnect();
setLayout(new BorderLayout());
dp = new DataPanel(conn);
tp = new TablePanel(conn);
rp = new ResPanel(conn, tp);
add(dp, BorderLayout.NORTH);
add(tp, BorderLayout.CENTER);
add(rp, BorderLayout.SOUTH);
}
public static final int STARTX = 100;
public static final int STARTY = 100;
public static final int DEFAULT_WIDTH = 800;
public static final int DEFAULT_HEIGHT = 600;
private Connection conn;
private JPanel dp;
private JPanel tp;
private JPanel rp;
}
运行效果如下图:
好了paste完代码了。这里的ResPanel有点混乱,它把一个button加入了上面的传入的一个参数panel里面,因为我发现将button和一个带滑块的panel放在一起,让显示结果的table感觉很小。本人的swing界面技术有点烂,好丑好丑的。:-)希望大家谅解,总的来说,这个程序设计还是比较清晰的,后续文章会对这个东东进行完善。另外祝福CSDN java版越来越好。
这个实现还有一点不好的地方就在那个TableModel。其实TableModel并没有要求数据存储的格式,其实我们完全可以用1维的HashTable或者HashMap或者ArrayList来存储能节省下不少的空间。这个改动会在后续完成。
爱生活,爱编程,希望大家周末愉快。