各位大侠,Swing怎么连数据库?能不能发个例子来,新手求助啊

wangmeiling00 2011-08-17 07:39:32
各位大侠,Swing怎么连数据库?能不能发个例子来,新手求助啊
...全文
101 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
softroad 2011-08-17
  • 打赏
  • 举报
回复
D:\jdk1.6.0_10\demo\jfc\TableExample\src源码
luohuijun619 2011-08-17
  • 打赏
  • 举报
回复
package project_view;

import interface_dao.PersonDAO;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import javabean_ben.Person;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;

import com.sun.org.apache.xerces.internal.util.TeeXMLDocumentFilterImpl;

import personimp_factory.DAOFactory;

public class MainStudent extends JFrame {
Container con = this.getContentPane();

JPanel panel = new JPanel();

JSplitPane split;

JButton btn_add = new JButton("增加");

JButton btn_del = new JButton("删除");

JButton btn_amend = new JButton("修改");

JButton btn_query = new JButton("查询");

JButton btn_likequery = new JButton("模糊查询");

JTable table;

DefaultTableModel tableModel;

JTree tree;

DefaultTreeModel treeModel;

List list;

public MainStudent() {
super("学士信息管理系统");
con.setLayout(new BorderLayout());

PersonDAO person = DAOFactory.getPersonDAOInstance();
try {
list = person.queryALl();
} catch (Exception e) {
System.out.println(e.getMessage());
}

treeMethod(list);
JScrollPane Scrollleft = new JScrollPane(tree);

tableMethod(list);
JScrollPane Scrollright = new JScrollPane(table);

split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true, Scrollleft,
Scrollright);
con.add(split, BorderLayout.CENTER);

MyButtonListener listener = new MyButtonListener();
btn_add.addActionListener(listener);
btn_del.addActionListener(listener);
btn_amend.addActionListener(listener);
btn_query.addActionListener(listener);
btn_likequery.addActionListener(listener);

panel.setLayout(new FlowLayout());
panel.add(btn_add);
panel.add(btn_del);
panel.add(btn_amend);
panel.add(btn_query);
panel.add(btn_likequery);
con.add(panel, BorderLayout.SOUTH);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(600, 400);
this.setResizable(false);
this.setVisible(true);
}

public void treeMethod(List list1) {
DefaultMutableTreeNode root = new DefaultMutableTreeNode("学生名称");
for (int i = 0; i < list1.size(); i++) {
Person person = (Person) list1.get(i);
DefaultMutableTreeNode sub = new DefaultMutableTreeNode(person);
root.add(sub);
}
treeModel = new DefaultTreeModel(root);
tree = new JTree(treeModel);
}

public void tableMethod(List list2) {
Vector tableHead = new Vector();
tableHead.add("学号");
tableHead.add("姓名");
tableHead.add("年龄");
tableHead.add("电子邮箱");
tableHead.add("地址");

Vector tableColumn = new Vector();
for (int i = 0; i < list2.size(); i++) {
Vector v = new Vector();
Person person = (Person) list2.get(i);
v.add(person.getId());
v.add(person.getName());
v.add(person.getAge());
v.add(person.getEmail());
v.add(person.getAddress());
tableColumn.add(v);
}
tableModel = new DefaultTableModel(tableColumn, tableHead);
table = new JTable(tableModel);
}

class MyButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btn_add) {
new addStudent(MainStudent.this);
}
if (e.getSource() == btn_del) {
String num = JOptionPane.showInputDialog(null, "请输入你要删除学生的学号");
if (num == null) {
} else {
PersonDAO p = DAOFactory.getPersonDAOInstance();
try {
int a = p.delete(Integer.parseInt(num));
if (a > 0) {
JOptionPane.showMessageDialog(null, "删除成功");
MainStudent.this.setVisible(false);
new MainStudent();
} else {
JOptionPane
.showMessageDialog(null, "对不起,你删除的学生不存在");
}
} catch (NumberFormatException e1) {
System.out.println(e1.getMessage());
} catch (Exception e1) {
System.out.println(e1.getMessage());
}
}
}
if (e.getSource() == btn_amend) {
String num = null;
num = JOptionPane.showInputDialog(null, "请输入你要修改的学生的学号");
if (num == null) {
} else {
PersonDAO p = DAOFactory.getPersonDAOInstance();
try {
Person person = p.queryById(Integer.parseInt(num));
if (person.toString() == null) {
JOptionPane.showMessageDialog(null,
"对不起你要修改的信息不存在", "提示", 1);
} else {
new amendStudent(person, MainStudent.this);
}
} catch (NumberFormatException e1) {
System.out.println(e1.getMessage());
} catch (Exception e1) {
System.out.println(e1.getMessage());
}
}
}
if (e.getSource() == btn_query) {
String num = JOptionPane.showInputDialog(null, "请输入你要查询学生的学号");
if (num == null) {
} else {
PersonDAO p = DAOFactory.getPersonDAOInstance();
try {
Person person = p.queryById(Integer.parseInt(num));
if (person.getId() == Integer.parseInt(num)) {
JOptionPane.showMessageDialog(null, "学号:"
+ person.getId() + "\n" + "姓名:"
+ person.getName() + "\n" + "年龄:"
+ person.getAge() + "\n" + "电子邮箱:"
+ person.getEmail() + "\n" + "地址:"
+ person.getAddress(), "查询", 1);
} else {
JOptionPane.showMessageDialog(null, "你要查询的学生不存在",
"提示", 1);
}
} catch (Exception e1) {
System.out.println(e1.getMessage());
}
}
}
if (e.getSource() == btn_likequery) {
String num = JOptionPane.showInputDialog(null,
"请输入你要查询学生的姓名或邮箱");
if (num == null) {
} else {
PersonDAO p = DAOFactory.getPersonDAOInstance();
try {
List s = p.queryByLike(num);
if (s.size() > 0) {
for (int i = 0; i < s.size(); i++) {
Person person = (Person) s.get(i);
JOptionPane.showMessageDialog(null, "学号:"
+ person.getId() + "\n" + "姓名:"
+ person.getName() + "\n" + "年龄:"
+ person.getAge() + "\n" + "电子邮箱:"
+ person.getEmail() + "\n" + "地址:"
+ person.getAddress(), "查询", 1);
}
} else {
JOptionPane.showMessageDialog(null,
"对不起,找不到你要查找的信息", "提示", 1);
}
} catch (Exception e1) {
System.out.println(e1.getMessage());
}
}
}
}
}
}


Swing很久没用过了,看来还得好好学学,很多东西都不记得了
luohuijun619 2011-08-17
  • 打赏
  • 举报
回复
package project_view;

import interface_dao.PersonDAO;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import personimp_factory.DAOFactory;

public class Login extends JFrame implements ActionListener {
JTextField login_name;
JPasswordField login_password;
JButton btn_login;
JButton btn_cannel;

public Login() {
super("系统登录");
Container container = this.getContentPane();
container.setLayout(new FlowLayout(1, 10, 20));

login_name = new JTextField(15);
login_password = new JPasswordField(15);

container.add(new JLabel("用户名:"));
container.add(login_name);
container.add(new JLabel("密 码:"));
container.add(login_password);

btn_login = new JButton("登录");
btn_cannel = new JButton("取消");

btn_login.addActionListener(this);
btn_cannel.addActionListener(this);

container.add(btn_login);
container.add(btn_cannel);

this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setResizable(false);
this.setSize(250, 180);
this.setVisible(true);
}

public void actionPerformed(ActionEvent e) {
if (e.getSource() == btn_login) {
PersonDAO person = DAOFactory.getPersonDAOInstance();
try {
if (login_name.getText().equals("")
|| login_password.getPassword().toString().equals("")) {
JOptionPane.showMessageDialog(null, "请输入用户名或密码", "系统提示", 1);
} else {
boolean b = person.login(login_name.getText(), String
.valueOf(login_password.getPassword()));
if (b == true) {
this.setVisible(false);
new MainStudent();
} else {
JOptionPane.showMessageDialog(null, "你输入的用户名或密码错误!",
"系统提示", 1);
}
}
} catch (Exception e1) {
System.out.println(e1.getMessage());
}
}
if (e.getSource() == btn_cannel) {
System.exit(0);
}
}

public static void main(String[] args) {
new Login();
}
}



package project_view;

import interface_dao.PersonDAO;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javabean_ben.Person;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import personimp_factory.DAOFactory;

public class addStudent extends JFrame implements ActionListener {
Container con = this.getContentPane();

JLabel L_name = new JLabel("姓名");
JLabel L_age = new JLabel("年龄");
JLabel L_email = new JLabel("邮箱");
JLabel L_address = new JLabel("住址");

JTextField T_name = new JTextField(15);
JTextField T_age = new JTextField(15);
JTextField T_email = new JTextField(15);
JTextField T_address = new JTextField(15);

JButton btn_ok = new JButton("确定");
JButton btn_cannel = new JButton("取消");

MainStudent mainStudent = null;

public addStudent(MainStudent mainStudent) {
this.setTitle("增加学生");
con.setLayout(new FlowLayout(1, 10, 15));

this.mainStudent = mainStudent;

con.add(L_name);
con.add(T_name);
con.add(L_age);
con.add(T_age);
con.add(L_email);
con.add(T_email);
con.add(L_address);
con.add(T_address);

con.add(btn_ok);
con.add(btn_cannel);

btn_ok.addActionListener(this);
btn_cannel.addActionListener(this);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setSize(250, 240);
this.setVisible(true);
}

public void actionPerformed(ActionEvent e) {
if (e.getSource() == btn_ok) {
PersonDAO dao = DAOFactory.getPersonDAOInstance();
if (T_name.getText().equals("")) {
JOptionPane.showMessageDialog(null, "姓名不能为空", "系统提示", 1);
} else if (T_age.getText().equals("")) {
JOptionPane.showMessageDialog(null, "年龄不能为空", "系统提示", 1);
} else {
Person person = new Person();
person.setName(T_name.getText());
person.setAge(Integer.parseInt(T_age.getText()));
person.setEmail(T_email.getText());
person.setAddress(T_address.getText());
try {
int i = dao.add(person);
if (i > 0) {
mainStudent.setVisible(false);
this.setVisible(false);
new MainStudent();
} else {
JOptionPane.showMessageDialog(null, "添加失败", "系统提示", 1);
}
} catch (Exception e1) {
System.out.println(e1.getMessage());
}
}
}
if (e.getSource() == btn_cannel) {
this.setVisible(false);
}
}
}

luohuijun619 2011-08-17
  • 打赏
  • 举报
回复
package databaseConnection_con;

import java.sql.Connection;
import java.sql.DriverManager;

public class DataBaseConnection {
private final String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";

private final String url = "jdbc:microsoft:sqlserver://localhost:1433;databasename=Student_MIS";

private final String user = "sa";

private final String password = "";

private Connection conn = null;

public DataBaseConnection() {
try {
Class.forName(driver);
this.conn = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}

// 取得数据库连接
public Connection getConnection() {
return this.conn;
}

// 关闭数据库连接
public void close() {
try {
this.conn.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
dingli74 2011-08-17
  • 打赏
  • 举报
回复
学习了
24K純帥 2011-08-17
  • 打赏
  • 举报
回复
1、Oracle8/8i/9i数据库(thin模式)

Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();

String url="jdbc:oracle:thin:@localhost:1521:orcl";

//orcl为数据库的SID

String user="test";

String password="test";

Connection conn= DriverManager.getConnection(url,user,password);


2、DB2数据库

Class.forName("com.ibm.db2.jdbc.app.DB2Driver ").newInstance();

String url="jdbc:db2://localhost:5000/sample";

//sample为你的数据库名

String user="admin";

String password="";

Connection conn= DriverManager.getConnection(url,user,password);


3、Sql Server7.0/2000数据库

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();

String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb";

//mydb为数据库

String user="sa";

String password="";

Connection conn= DriverManager.getConnection(url,user,password);


4、Sybase数据库

Class.forName("com.sybase.jdbc.SybDriver").newInstance();

String url =" jdbc:sybase:Tds:localhost:5007/myDB";

//myDB为你的数据库名

Properties sysProps = System.getProperties();

SysProps.put("user","userid");

SysProps.put("password","user_password");

Connection conn= DriverManager.getConnection(url, SysProps);


5、Informix数据库

Class.forName("com.informix.jdbc.IfxDriver").newInstance();

String url =

"jdbc:informix-sqli://123.45.67.89:1533/myDB:INFORMIXSERVER=myserver;

user=testuser;password=testpassword";

//myDB为数据库名

Connection conn= DriverManager.getConnection(url);


6、MySQL数据库

Class.forName("org.gjt.mm.mysql.Driver").newInstance();

String url ="jdbc:mysql://localhost/myDB?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1"

//myDB为数据库名

Connection conn= DriverManager.getConnection(url);


7、PostgreSQL数据库

Class.forName("org.postgresql.Driver").newInstance();

String url ="jdbc:postgresql://localhost/myDB"

//myDB为数据库名

String user="myuser";

String password="mypassword";

Connection conn= DriverManager.getConnection(url,user,password);

xuwenjiang12 2011-08-17
  • 打赏
  • 举报
回复
顶一下
dinglimin2009 2011-08-17
  • 打赏
  • 举报
回复
无论是swing还是jsp连数据库的方式都是一样的,以oracle为例

Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@localhost:1521:orcl"; //orcl为数据库的SID
String user="test";
String password="test";
Connection conn= DriverManager.getConnection(url,user,password);
lijie45100 2011-08-17
  • 打赏
  • 举报
回复
没做过,百度吧

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧