import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.sql.*;
class ProductInWindow extends JDialog implements ActionListener
{
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel p3=new JPanel();
JPanel p4=new JPanel();
JPanel p5=new JPanel();
JTextField txtProductName=new JTextField(15);
JTextField txtProductType=new JTextField(15);
JTextField txtProductDate=new JTextField(15);
JTextField txtProductInDate=new JTextField(15);
JTextField txtProductNumber=new JTextField(15);
JTextField txtProductInPrise=new JTextField(15);
JTextField txtProductSalePrise=new JTextField(15);
JTextArea txtProductRes=new JTextArea(5,15);
JButton button1=new JButton("添加");
JButton button2=new JButton("取消");
JButton button3=new JButton("退出"); Connection Con=null;
Statement Stmt=null;
// public static boolean conn(String URL,String username,String password)
public ProductInWindow()
{
try
{
Class.forName("dun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e)
{
}
try
{
Con=DriverManager.getConnection("jdbc:odbc:ccss","sa","");
Stmt=Con.createStatement();
}
catch(SQLException ee)
{
// e.printStackTrace();
// return false;
}
// return true;
// }
/* public static boolean close()
{
try
{
Con.close();
Con=null;
}
catch(SQLException e)
{
e.printStackTrace();
return false;
}
return true;
}
*/
System.out.println("++++++++++++"); setModal(true);
setBackground(Color.LIGHT_GRAY);
Container contentPane=this.getContentPane();
contentPane.setLayout(new GridLayout(5,1));
p1.add(new JLabel("产品名称"));
p1.add(txtProductName);
p1.add(new JLabel("类型"));
p1.add(txtProductType);
p2.add(new JLabel("数量"));
p2.add(txtProductNumber);
p2.add(new JLabel("售出价钱(单价)"));
p2.add(txtProductSalePrise);
p3.add(new JLabel("产品生产日期"));
p3.add(txtProductDate);
p3.add(new JLabel("产品进货日期"));
p3.add(txtProductInDate);
p4.add(new JLabel("备注"));
p4.add(txtProductRes);
p5.add(button1);
p5.add(button2);
p5.add(button3);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
txtProductName.addActionListener(this);
txtProductType.addActionListener(this);
txtProductDate.addActionListener(this);
txtProductInDate.addActionListener(this);
txtProductNumber.addActionListener(this);
txtProductInPrise.addActionListener(this);
txtProductSalePrise.addActionListener(this);
contentPane.add(p1);
contentPane.add(p2);
contentPane.add(p3);
contentPane.add(p4);
contentPane.add(p5);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setSize(750,500);
Dimension screen=Toolkit.getDefaultToolkit().getScreenSize();
setLocation((screen.width-300)/5,(screen.height-110)/5);
setTitle("进货");
setResizable(false);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
try
{
if(e.getSource()==button1)
{
try
{
// statement stmt=ConnectServer.con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultEst.CONCUR_UPDATATABLE);
//
insert();
} catch(SQLException ee)
{
}
}
} catch(Exception ee)
{
}
}
/* else if(e.getSource()==button2)
{
dispose();
}
else if(e.getSource()==button3)
{
dispose();
new MainWindow();
}
*/
public void insert() throws SQLException
{
/* String s1="'"+txtProductName.getText()+"'",
// s2="'"+txtProductType.getText()+"'",
s6="'"+txtProductInDate.getText()+"'",
s5="'"+txtProductDate.getText()+"'";
int s3=txtProductNumber.getText(),
s4=txtProductSalePrise.getText(),
s7=txtProductRes.getText();*/
String temp="insert into ProductIN VALUES ('"+txtProductName.getText()+"','"+txtProductType.getText()+"',"+txtProductNumber.getText()+",'"+txtProductInDate.getText()+"','"+txtProductDate.getText()+"',"+txtProductSalePrise.getText()+","+txtProductRes.getText()+")";
System.out.println(temp);
Con=DriverManager.getConnection("jdbc:odbc:ccss","sa","");
int s=Stmt.executeUpdate(temp);
System.out.println(s);
Con.close();
}
public static void main(String args[])
{
JDialog.setDefaultLookAndFeelDecorated(true);
Font font=new Font("JFrame",Font.PLAIN,14);
Enumeration keys=UIManager.getLookAndFeelDefaults().keys();
while(keys.hasMoreElements())
{
Object key=keys.nextElement();
if(UIManager.get(key) instanceof Font)UIManager.put(key,font);
}
/* if(conn("jdbc:odbc:ccss","sa",""))
{
JOptionPane.showMessageDialog(null,"数据库连接成功!");
close();
}
else
JOptionPane.showMessageDialog(null,"数据库连接失败!");
*/
new ProductInWindow();
}
}
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.sql.*;
class ProductInWindow extends JDialog implements ActionListener
{
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel p3=new JPanel();
JPanel p4=new JPanel();
JPanel p5=new JPanel();
JTextField txtProductName=new JTextField(15);
JTextField txtProductType=new JTextField(15);
JTextField txtProductDate=new JTextField(15);
JTextField txtProductInDate=new JTextField(15);
JTextField txtProductNumber=new JTextField(15);
JTextField txtProductInPrise=new JTextField(15);
JTextField txtProductSalePrise=new JTextField(15);
JTextArea txtProductRes=new JTextArea(5,15);
JButton button1=new JButton("添加");
JButton button2=new JButton("取消");
JButton button3=new JButton("退出"); Connection Con=null;
Statement Stmt=null;
// public static boolean conn(String URL,String username,String password)
public ProductInWindow()
{
try
{
Class.forName("dun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e)
{
}
try
{
Con=DriverManager.getConnection("jdbc:odbc:ccss","sa","");
Stmt=Con.createStatement();
}
catch(SQLException ee)
{
// e.printStackTrace();
// return false;
}
// return true;
// }
/* public static boolean close()
{
try
{
Con.close();
Con=null;
}
catch(SQLException e)
{
e.printStackTrace();
return false;
}
return true;
}
*/
System.out.println("++++++++++++"); setModal(true);
setBackground(Color.LIGHT_GRAY);
Container contentPane=this.getContentPane();
contentPane.setLayout(new GridLayout(5,1));
p1.add(new JLabel("产品名称"));
p1.add(txtProductName);
p1.add(new JLabel("类型"));
p1.add(txtProductType);
p2.add(new JLabel("数量"));
p2.add(txtProductNumber);
p2.add(new JLabel("售出价钱(单价)"));
p2.add(txtProductSalePrise);
p3.add(new JLabel("产品生产日期"));
p3.add(txtProductDate);
p3.add(new JLabel("产品进货日期"));
p3.add(txtProductInDate);
p4.add(new JLabel("备注"));
p4.add(txtProductRes);
p5.add(button1);
p5.add(button2);
p5.add(button3);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
txtProductName.addActionListener(this);
txtProductType.addActionListener(this);
txtProductDate.addActionListener(this);
txtProductInDate.addActionListener(this);
txtProductNumber.addActionListener(this);
txtProductInPrise.addActionListener(this);
txtProductSalePrise.addActionListener(this);
contentPane.add(p1);
contentPane.add(p2);
contentPane.add(p3);
contentPane.add(p4);
contentPane.add(p5);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setSize(750,500);
Dimension screen=Toolkit.getDefaultToolkit().getScreenSize();
setLocation((screen.width-300)/5,(screen.height-110)/5);
setTitle("进货");
setResizable(false);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
try
{
if(e.getSource()==button1)
{
try
{
// statement stmt=ConnectServer.con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultEst.CONCUR_UPDATATABLE);
//
insert();
} catch(SQLException ee)
{
}
}
} catch(Exception ee)
{
}
}
/* else if(e.getSource()==button2)
{
dispose();
}
else if(e.getSource()==button3)
{
dispose();
new MainWindow();
}
*/
public void insert() throws SQLException
{
/* String s1="'"+txtProductName.getText()+"'",
// s2="'"+txtProductType.getText()+"'",
s6="'"+txtProductInDate.getText()+"'",
s5="'"+txtProductDate.getText()+"'";
int s3=txtProductNumber.getText(),
s4=txtProductSalePrise.getText(),
s7=txtProductRes.getText();*/
String temp="insert into ProductIN VALUES ('"+txtProductName.getText()+"','"+txtProductType.getText()+"',"+txtProductNumber.getText()+",'"+txtProductInDate.getText()+"','"+txtProductDate.getText()+"',"+txtProductSalePrise.getText()+","+txtProductRes.getText()+")";
System.out.println(temp);
Con=DriverManager.getConnection("jdbc:odbc:ccss","sa","");
int s=Stmt.executeUpdate(temp);
System.out.println(s);
Con.close();
}
public static void main(String args[])
{
JDialog.setDefaultLookAndFeelDecorated(true);
Font font=new Font("JFrame",Font.PLAIN,14);
Enumeration keys=UIManager.getLookAndFeelDefaults().keys();
while(keys.hasMoreElements())
{
Object key=keys.nextElement();
if(UIManager.get(key) instanceof Font)UIManager.put(key,font);
}
/* if(conn("jdbc:odbc:ccss","sa",""))
{
JOptionPane.showMessageDialog(null,"数据库连接成功!");
close();
}
else
JOptionPane.showMessageDialog(null,"数据库连接失败!");
*/
new ProductInWindow();
}
}
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;class OporatingDataBase {
//创建驱动
private static final String DB_URL ="sun.jdbc.odbc.JdbcOdbcDriver";
//数据连接地址
private static final String URL = "jdbc:ojbc:<数据源名>";
private Connection con = null;
private Statement stmt = null;
private PreparedStatement pstmt = null;
private ResultSet rs = null;
/**
* 这是构造方法
*/
public OporatingDataBase(){
}
/**
* 这是连接数据库的方法
* @return Connection
*/
public Connection getConnection(){
try {
Class.forName(DB_URL);
con = DriverManager.getConnection(URL,"数据库用户名","数据库密码");
} catch (Exception ex){
ex.printStackTrace();
}
return con;
}
/**
* 一个执行语句方法
* @return Statement
*/
public Statement getStatement () {
if (con == null){
con = this.getConnection();
}
try {
stmt = con.createStatement();
} catch (SQLException ex) {
ex.printStackTrace();
}
return stmt;
}
/**
* 一个预处理语句方法
* @param sql String
* @return PreparedStatement
*/
public PreparedStatement getPreparedStatement(String sql){
if (con == null){
con = this.getConnection();
}
try {
pstmt = con.prepareStatement(sql);
} catch (SQLException ex) {
ex.printStackTrace();
}
return pstmt;
}
/**
* 一个结果集方法
* @param stmt Statement
* @param sql String
* @return ResultSet
*/
public ResultSet getResultSet(Statement stmt , String sql){
if (con == null){
con = this.getConnection();
}
try {
rs = stmt.executeQuery(sql);
} catch (SQLException ex) {
ex.printStackTrace();
}
return rs;
}
/**
* 关闭所有数据库连接
*/
public void close() {
try {
if (rs != null) {
rs.close();
rs = null;
}
if (stmt != null) {
stmt.close();
stmt = null;
}
if (pstmt != null) {
pstmt.close();
pstmt = null;
}
if (con != null) {
con.close();
con = null;
} } catch (Exception ex) {
ex.printStackTrace();
}
}
}
仅供你参考,看看这种实现的思想!
思想很重要!