【java】MyEclipse連接SQL server 2012簡易信息查詢窗體




      在學習java的過程中連接數據庫是必不可少的一課。對於沒有用過數據庫的人而言,數據庫的連接就顯得尤為困難,我也是一樣,第一次使用MyEclipse連接數據路的時候在網上找各式各樣的教程,發現基本都是騙人的,根本就不需要配置什么環境變量,只需要下載相應的jar包就可以了。

    圖片窗體對應的代碼如下:


import java.awt.BorderLayout;


public class InfoShow extends JFrame {

	private JPanel contentPane;
	private JTextField textName;
	private JTextField textNo;
	private JTextField textNumber;
	private JTextField textSearch;
	private JTextField textNumberSearch;
	private JTextField textNewName;

	/**
	 * Launch the application.
	 */
	
	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					
					Icon p[]=new Icon[5];   //圖片數組
					 p[0]=new ImageIcon("1.jpg");      
					 p[1]=new ImageIcon("2.jpg");     
					 p[2]=new ImageIcon("3.jpg");
					 p[3]=new ImageIcon("4.jpg");
					 p[4]=new ImageIcon("5.jpg");
					
					InfoShow frame = new InfoShow(p);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	

	/**
	 * Create the frame.
	 */
	public InfoShow(final Icon p[]) {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 472, 382);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lbName = new JLabel("書名:");
		lbName.setFont(new Font("宋體", Font.PLAIN, 16));
		lbName.setBounds(41, 107, 54, 15);
		contentPane.add(lbName);
		
		JLabel lbNo = new JLabel("編號:");
		lbNo.setFont(new Font("宋體", Font.PLAIN, 16));
		lbNo.setBounds(41, 71, 54, 15);
		contentPane.add(lbNo);
		
		JLabel lbNumber = new JLabel("數量:");
		lbNumber.setFont(new Font("宋體", Font.PLAIN, 16));
		lbNumber.setBounds(41, 146, 54, 15);
		contentPane.add(lbNumber);
		
		textName = new JTextField();
		textName.setEditable(false);
		textName.setBounds(119, 105, 104, 21);
		contentPane.add(textName);
		textName.setColumns(10);
		
		textNo = new JTextField();
		textNo.setEditable(false);
		textNo.setBounds(119, 69, 104, 21);
		contentPane.add(textNo);
		textNo.setColumns(10);
		
		textNumber = new JTextField();
		textNumber.setEditable(false);
		textNumber.setBounds(119, 144, 104, 21);
		contentPane.add(textNumber);
		textNumber.setColumns(10);
		
		final JLabel lbPicture = new JLabel("");
		lbPicture.setBounds(303, 34, 121, 151);
		contentPane.add(lbPicture);
		
		JLabel lbSearch = new JLabel("請輸入編號:");
		lbSearch.setFont(new Font("宋體", Font.PLAIN, 14));
		lbSearch.setBounds(25, 191, 84, 15);
		contentPane.add(lbSearch);
		
		textSearch = new JTextField();
		textSearch.setBounds(119, 188, 104, 21);
		contentPane.add(textSearch);
		textSearch.setColumns(10);
		
		JButton btnOK = new JButton("確定");
		btnOK.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try{
                    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
				
				Connection con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=book","sa", "88888888");
				
				Statement St=con.createStatement();				
				
				ResultSet r=St.executeQuery("select * from books where id = '"+textSearch.getText()+"'");
				  
			    while(r.next())
			    {

			    	textName.setText(r.getString("name"));
			    	textNo.setText(r.getString("id"));
			    	textNumber.setText(r.getString("number"));
			    	int num = Integer.parseInt(r.getString("id"))-100;
			    	lbPicture.setIcon(p[num-1]);	
			    }
			    
			    textSearch.setText(null);
				textNewName.setText(null);
				textNumberSearch.setText(null);
			    
			    		 
		}catch (Exception e1) {
			e1.printStackTrace();
		}
			}
		});
		//btnOK.addActionListener(new ActionListener() {}
		btnOK.setFont(new Font("宋體", Font.PLAIN, 14));
		btnOK.setBounds(339, 293, 93, 23);
		contentPane.add(btnOK);
		
		JLabel lbNumberSearch = new JLabel("請輸入數量:");
		lbNumberSearch.setFont(new Font("宋體", Font.PLAIN, 14));
		lbNumberSearch.setBounds(25, 222, 84, 15);
		contentPane.add(lbNumberSearch);
		
		textNumberSearch = new JTextField();
		textNumberSearch.setBounds(119, 219, 104, 21);
		contentPane.add(textNumberSearch);
		textNumberSearch.setColumns(10);
		
		JButton btnAdd = new JButton("增加");
		btnAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try{
                    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
				
				Connection con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=book","sa", "88888888");
				
				PreparedStatement r=con.prepareStatement("insert into books values('"+textSearch.getText()+"','"+textNewName.getText()+"','"+textNumberSearch.getText()+"')");
				
			      r.executeUpdate();
				
			      textSearch.setText(null);
					textNewName.setText(null);
					textNumberSearch.setText(null);
					
				JOptionPane.showMessageDialog(null, "插入成功!");
				 	 
		}catch (Exception e1) {
			e1.printStackTrace();
		}
			}
		});
		btnAdd.setFont(new Font("宋體", Font.PLAIN, 14));
		btnAdd.setBounds(28, 293, 93, 23);
		contentPane.add(btnAdd);
		
		JButton btnDelete = new JButton("刪除");
		btnDelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try{
					
					if(JOptionPane.OK_OPTION == JOptionPane.showConfirmDialog(InfoShow.this,"確定要刪除嗎?","刪除",JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE))
                    {
						Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                    
				
				Connection con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=book","sa", "88888888");

				PreparedStatement r=con.prepareStatement("delete from books where id = '"+textSearch.getText()+"'");
				
				r.executeUpdate();
				
				textSearch.setText(null);
				textNewName.setText(null);
				textNumberSearch.setText(null);
				
				JOptionPane.showMessageDialog(null, "刪除成功!");
                    }
 
		}catch (Exception e1) {
			e1.printStackTrace();
		}
				
			}
		});
		btnDelete.setFont(new Font("宋體", Font.PLAIN, 14));
		btnDelete.setBounds(130, 293, 93, 23);
		contentPane.add(btnDelete);
		
		JButton btnUpdate = new JButton("修改");
		btnUpdate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try{
                    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
				
				Connection con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=book","sa", "88888888");
				
				PreparedStatement r=con.prepareStatement("update books set name = '"+textNewName.getText()+"',number = '"+textNumberSearch.getText()+"' where id = '"+textSearch.getText()+"'");
				r.executeUpdate();
				
				textSearch.setText(null);
				textNewName.setText(null);
				textNumberSearch.setText(null);
				
				JOptionPane.showMessageDialog(null, "修改成功!");
				
		}catch (Exception e1) {
			e1.printStackTrace();
		}
				
			}
		});
		btnUpdate.setFont(new Font("宋體", Font.PLAIN, 14));
		btnUpdate.setBounds(234, 293, 93, 23);
		contentPane.add(btnUpdate);
		
		JLabel lblNewName = new JLabel("請輸入書名:");
		lblNewName.setFont(new Font("宋體", Font.PLAIN, 14));
		lblNewName.setBounds(25, 253, 84, 15);
		contentPane.add(lblNewName);
		
		textNewName = new JTextField();
		textNewName.setBounds(119, 250, 104, 21);
		contentPane.add(textNewName);
		textNewName.setColumns(10);
		
		JMenuBar menuBar = new JMenuBar();
		menuBar.setBounds(10, 0, 105, 21);
		contentPane.add(menuBar);
		
		JMenu menu = new JMenu("操作");
		menuBar.add(menu);
		
		JMenuItem menuItem = new JMenuItem("增加");
		menuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try{
                    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
				
				Connection con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=book","sa", "88888888");
				
				PreparedStatement r=con.prepareStatement("insert into books values('"+textSearch.getText()+"','"+textNewName.getText()+"','"+textNumberSearch.getText()+"')");
				
			      r.executeUpdate();
				
				JOptionPane.showMessageDialog(null, "插入成功!");
				
				textSearch.setText(null);
				textNewName.setText(null);
				textNumberSearch.setText(null);
				
				 	 
		}catch (Exception e1) {
			e1.printStackTrace();
		}
			}
		});
		menu.add(menuItem);
		
		JMenuItem menuItem_1 = new JMenuItem("刪除");
		menuItem_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try{
					
					if(JOptionPane.OK_OPTION == JOptionPane.showConfirmDialog(InfoShow.this,"確定要刪除嗎?","刪除",JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE))
                    {
                    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
				
				Connection con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=book","sa", "88888888");
				
				PreparedStatement r=con.prepareStatement("delete from books where id = '"+textSearch.getText()+"'");
				r.executeUpdate();
				
				textSearch.setText(null);
				textNewName.setText(null);
				textNumberSearch.setText(null);
				
				JOptionPane.showMessageDialog(null, "刪除成功!");
                    }
 
		}catch (Exception e1) {
			e1.printStackTrace();
		}
			}
		});
		menu.add(menuItem_1);
		
		JMenuItem menuItem_2 = new JMenuItem("修改");
		menuItem_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try{
                    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
				
				Connection con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=book","sa", "88888888");

				PreparedStatement r=con.prepareStatement("update books set name = '"+textNewName.getText()+"',number = '"+textNumberSearch.getText()+"' where id = '"+textSearch.getText()+"'");
				r.executeUpdate();
				
				textSearch.setText(null);
				textNewName.setText(null);
				textNumberSearch.setText(null);
				
				JOptionPane.showMessageDialog(null, "修改成功!");
				
		}catch (Exception e1) {
			e1.printStackTrace();
		}
			}
		});
		menu.add(menuItem_2);
		
		JMenuItem menuItem_3 = new JMenuItem("查詢");
		menuItem_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try{
                    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
				
				Connection con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=book","sa", "88888888");
				
				Statement St=con.createStatement();
				
				ResultSet r=St.executeQuery("select * from books where id = '"+textSearch.getText()+"'");
				  
			    while(r.next())
			    {

			    	textName.setText(r.getString("name"));
			    	textNo.setText(r.getString("id"));
			    	textNumber.setText(r.getString("number"));
			    	int num = Integer.parseInt(r.getString("id"))-100;
			    	lbPicture.setIcon(p[num-1]);	
			}
			    
			    textSearch.setText(null);
				textNewName.setText(null);
				textNumberSearch.setText(null);
				
			    		 
		}catch (Exception e1) {
			e1.printStackTrace();
		}
			}
		});
		menu.add(menuItem_3);
		
		JMenuBar menuBar_1 = new JMenuBar();
		menu.add(menuBar_1);
	}
}

lable中顯示的圖片我使用的是圖片數組,與上一篇一樣,放在本項目文件夾中。

其實連接數據庫很簡單,進行增刪改查的代碼都很類似,我在C#編程時也是如此。





注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com