1.sql server創建數據庫和相關表
2.創建數據源
(1).打開控制面板找到管理,打開ODBC選項或直接搜索數據源
(2).打開數據源配置後點擊添加,選擇sql server點擊完成
(3).給數據源命名,服務器選擇本機電腦sql server 服務器名下一步
後面所有點下一步或肯定就能夠了。
3.代碼設計
1.加載數據庫驅動
2.與數據源創建鏈接
3.與數據庫創建會話
4.操做數據庫java
package mybook; import java.sql.*; import javax.swing.JOptionPane; public class bookdb { String drivername="sun.jdbc.odbc.JdbcOdbcDriver";//驅動名 Connection conn; Statement stmt; ResultSet rs; bookdb(){ try { Class.forName(drivername); conn=DriverManager.getConnection("jdbc:odbc:bookdb","sa","zhangyabiao");//與數據源bookdb創建鏈接 stmt=conn.createStatement(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public ResultSet find(String sql){//對數據庫進行數據查詢 try { rs=stmt.executeQuery(sql); return rs; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } public boolean update(String sql){//對增刪改 try { stmt.executeUpdate(sql); //JOptionPane.showInternalMessageDialog(null, "執行成功"); return true; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); //JOptionPane.showInternalMessageDialog(null, "執行失敗"); return false; } } }
package mybook; import java.awt.*; import java.awt.event.*; import java.sql.*; import javax.swing.*; public class denglu extends JFrame implements ActionListener{ JPanel p1,p2,p3; JLabel l1,l2; JTextField f1; JPasswordField f2; JButton b1,b2; ResultSet rs; bookframe bf; bookdb b; zuce z; denglu(){ b=new bookdb(); this.setLayout(new GridLayout(3,1)); p1=new JPanel(); p2=new JPanel(); p3=new JPanel(); l1=new JLabel("帳戶:"); l2=new JLabel("密碼:"); f1=new JTextField(15); f2=new JPasswordField(15); b1=new JButton("登陸"); b2=new JButton("註冊"); p1.add(l1); p1.add(f1); p2.add(l2); p2.add(f2); p3.add(b1); p3.add(b2); this.add(p1); this.add(p2); this.add(p3); b1.addActionListener(this); b2.addActionListener(this); this.setBounds(300, 350, 300, 250); this.setVisible(true); } public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(e.getSource()==b1){//帳戶驗證 String sql="select *from yonghu where password='"+f2.getText()+"'"; rs=b.find(sql); try { if(rs.next()){ bf=new bookframe(); this.dispose();//釋放窗體 }else{ JOptionPane.showMessageDialog(null, "帳戶或密碼錯誤,請從新登陸"); f1.setText(""); f2.setText(""); } } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }else if(e.getSource()==b2){ z=new zuce();//註冊帳戶 } } }
帳戶註冊sql
package mybook; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.sql.*; public class zuce extends JFrame implements ActionListener{ JPanel p1,p2,p3; JLabel l1,l2; JTextField f1; JPasswordField f2; JButton b1,b2; ResultSet rs; bookdb b; zuce(){ b=new bookdb(); this.setLayout(new GridLayout(3,1)); p1=new JPanel(); p2=new JPanel(); p3=new JPanel(); l1=new JLabel("帳戶:"); l2=new JLabel("密碼:"); f1=new JTextField(15); f2=new JPasswordField(15); b1=new JButton("保存"); b2=new JButton("取消"); p1.add(l1); p1.add(f1); p2.add(l2); p2.add(f2); p3.add(b1); p3.add(b2); this.add(p1); this.add(p2); this.add(p3); b1.addActionListener(this); b2.addActionListener(this); this.setBounds(400, 400, 300,250 ); this.setVisible(true); } public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(e.getSource()==b1){ String sql="insert into yonghu values('"+f1.getText()+"','"+f2.getText()+"')"; if(b.update(sql)){ JOptionPane.showMessageDialog(null, "註冊成功"); this.dispose(); }else{ JOptionPane.showMessageDialog(null, "用戶已存在"); f1.setText(""); f2.setText(""); } }else{ this.dispose(); } } }
package mybook; import java.sql.*; import java.awt.*; import javax.swing.*; import java.awt.event.*; public class bookframe extends JFrame implements ActionListener { JLabel l1,l2,l3; JTextField f1,f2,f3; JButton b1,b2,b3,b4; JPanel p1,p2,p3,p4,p5; ResultSet rs; bookdb b; bookframe(){ b=new bookdb(); this.setLayout(new GridLayout(5,1)); l1=new JLabel("書本編號:"); l2=new JLabel("書 名:"); l3=new JLabel("書本做者:"); f1=new JTextField(15); f2=new JTextField(15); f3=new JTextField(15); b1=new JButton("增長"); b2=new JButton("查詢"); b3=new JButton("修改"); b4=new JButton("刪除"); p1=new JPanel(); p2=new JPanel(); p3=new JPanel(); p4=new JPanel(); p5=new JPanel(); p1.add(l1); p1.add(f1); p2.add(l2); p2.add(f2); p3.add(l3); p3.add(f3); p4.add(b1); p4.add(b2); p5.add(b3); p5.add(b4); this.add(p1); this.add(p2); this.add(p3); this.add(p4); this.add(p5); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); b4.addActionListener(this); b3.setEnabled(false); b4.setEnabled(false); this.setSize(350, 450); this.setVisible(true); } public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(e.getSource()==b1){//增長圖書 String sql="insert into book values('"+f1.getText()+"','"+f2.getText()+"','"+f3.getText()+"')"; if(b.update(sql)){ f2.setText(""); f3.setText(""); JOptionPane.showMessageDialog(null, "圖書增長成功"); }else{ JOptionPane.showMessageDialog(null, "圖書編號已存在"); f1.setText(""); f2.setText(""); f3.setText(""); } }else if(e.getSource()==b2){//查找圖書 String sql="select *from book where bookid='"+f1.getText()+"'"; rs=b.find(sql); try { while(rs.next()){ f2.setText(rs.getString(2)); f3.setText(rs.getString(3)); b3.setEnabled(true); b4.setEnabled(true); } } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); JOptionPane.showMessageDialog(null, "您查找的編號不存在,請從新查找"); f1.setText(""); f2.setText(""); f3.setText(""); } }else if(e.getSource()==b3){//修改圖書資料 String sql="update book set bookname='"+f2.getText()+"',bookauthor='"+f3.getText()+"'where bookid='"+f1.getText()+"'"; if(b.update(sql)){ JOptionPane.showMessageDialog(null, "修改爲功"); f2.setText(""); f3.setText(""); b3.setEnabled(false); b4.setEnabled(false); }else{ JOptionPane.showMessageDialog(null, "修改失敗,可能您修改的圖書編號不存在"); } }else if(e.getSource()==b4){//刪除圖書 String sql="delete from book where bookid='"+f1.getText()+"'"; if(b.update(sql)){ JOptionPane.showMessageDialog(null, "圖書成功刪除"); f1.setText(""); f2.setText(""); f3.setText(""); b3.setEnabled(false); b4.setEnabled(false); }else{ JOptionPane.showMessageDialog(null, "沒能查找到您要刪除的圖書編號"); } } } }
package mybook; public class frame_main { public static void main(String[] args) { // TODO Auto-generated method stub denglu d=new denglu(); } }