java用JDBC連接sqlServer_第1頁
java用JDBC連接sqlServer_第2頁
java用JDBC連接sqlServer_第3頁
java用JDBC連接sqlServer_第4頁
java用JDBC連接sqlServer_第5頁
已閱讀5頁,還剩21頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、Java用JDBC連接sqlServer數(shù)據(jù)表:(sqlServer2012)程序運(yùn)行截圖:登錄/注冊界面主界面/修改密碼/修改信息界面源代碼:登錄界面:sqlsystem.javapackage com.johnson;import java.awt.BorderLayout;import java.awt.EventQueue;import java.awt.Font;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.sq

2、l.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import javax.swing.ButtonGroup;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import ja

3、vax.swing.JPasswordField;import javax.swing.JTextField;import javax.swing.SwingConstants;import javax.swing.JRadioButton;import javax.swing.border.BevelBorder;import javax.swing.border.CompoundBorder;import javax.swing.border.LineBorder;import java.awt.Color;public class sqlsystem private JFrame fra

4、me;private JTextField usernameText;private JPasswordField passwdText;welcome welFrame;revisePasswd rePasswd;reviseInfo reInfo;reg regwin;private JRadioButton userRadioButton;private JRadioButton systemRadioButton; /* * Launch the application. */public static void main(String args) EventQueue.invokeL

5、ater(new Runnable() public void run() try sqlsystem window = new sqlsystem();window.frame.setVisible(true); catch (Exception e) e.printStackTrace(););/* * Create the application. */public sqlsystem() initialize();welFrame=new welcome();rePasswd=new revisePasswd();reInfo=new reviseInfo();regwin=new r

6、eg();/* * Initialize the contents of the frame. */private void initialize() frame = new JFrame();frame.setTitle("u767Bu9646u7CFBu7EDF");/frame.setBounds(100, 100, 450, 300);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);JPanel panel = new JPanel();frame.getContentPane().add(panel, Bo

7、rderLayout.SOUTH);panel.setLayout(new BorderLayout();JPanel panel2=new JPanel();panel2.setBorder(new CompoundBorder(null, new LineBorder(new Color(0, 0, 0);panel.add(panel2,BorderLayout.CENTER);panel2.setLayout(new GridLayout(3,2);JLabel usernameLabel=new JLabel("用戶名:");usernameLabel.setHo

8、rizontalAlignment(SwingConstants.CENTER);JLabel passwdLabel=new JLabel("密碼:");passwdLabel.setHorizontalAlignment(SwingConstants.CENTER);JLabel degreeLabel=new JLabel("身份:");degreeLabel.setHorizontalAlignment(SwingConstants.CENTER);JPanel degreePanel=new JPanel();degreePanel.setBo

9、rder(new LineBorder(new Color(0, 0, 0);userRadioButton = new JRadioButton("用戶");userRadioButton.setSelected(true);systemRadioButton=new JRadioButton("管理員");degreePanel.add(userRadioButton);degreePanel.add(systemRadioButton);ButtonGroup degreeGroup=new ButtonGroup();degreeGroup.ad

10、d(userRadioButton); degreeGroup.add(systemRadioButton);usernameText=new JTextField(10);usernameLabel.setLabelFor(usernameText);passwdText=new JPasswordField(10);passwdLabel.setLabelFor(passwdText);panel2.add(usernameLabel);panel2.add(usernameText);panel2.add(passwdLabel);panel2.add(passwdText);panel

11、2.add(degreeLabel);panel2.add(degreePanel);JPanel panel3=new JPanel();panel.add(panel3,BorderLayout.SOUTH);JButton loginBut=new JButton("登陸");panel3.add(loginBut);/注冊按鈕JButton regBut = new JButton("u6CE8u518C");regBut.addActionListener(new ActionListener() public void actionPerfo

12、rmed(ActionEvent e) regwin.setVisible(true);regwin.launch(););panel3.add(regBut);loginBut.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e) /登陸驗(yàn)證if(usernameText.getText().equals("") | passwdText.getText().equals("")JOptionPane.showMessageDialog(fram

13、e, "用戶名、密碼不能為空!");elseif(sqlcheck(usernameText.getText().trim(),passwdText.getText().trim()welFrame.launch(usernameText.getText(),frame,rePasswd,reInfo);frame.setVisible(false);clear();elseJOptionPane.showMessageDialog(frame, "用戶名、密碼不存在!"););JLabel welcomeLabel = new JLabel("

14、;u6B22u8FCEu767Bu9646");welcomeLabel.setFont(new Font("宋體", Font.BOLD, 40);welcomeLabel.setHorizontalAlignment(SwingConstants.CENTER);frame.getContentPane().add(welcomeLabel, BorderLayout.CENTER);frame.pack();frame.setLocationRelativeTo(null);/數(shù)據(jù)庫處理boolean sqlcheck(String username,Str

15、ing passwd)/-數(shù)據(jù)庫驗(yàn)證- /加載sql數(shù)據(jù)庫驅(qū)動 try Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); catch (ClassNotFoundException e) / TODO 自動生成的 catch 塊e.printStackTrace(); Connection ct = null; Statement st = null; ResultSet rs = null;try /創(chuàng)建數(shù)據(jù)庫鏈接ct = DriverManager.getConnection("jdb

16、c:sqlserver:/localhost:1433;DatabaseName=myuserdat","johnson","123");/創(chuàng)建statementst = ct.createStatement();/查詢數(shù)據(jù)庫rs = st.executeQuery("select passwd,degree from users where username='"+username+"'");/結(jié)果判斷if(rs.next()String realdat=rs.getString(1).

17、trim();if(realdat.equals(passwd)String dex=rs.getString(2);if(dex.trim().equals("0")if(userRadioButton.isSelected()st.close();ct.close();return true;else if(dex.equals("1")if(systemRadioButton.isSelected()st.close();ct.close();return true;st.close();ct.close(); catch (SQLExceptio

18、n e) / TODO 自動生成的 catch 塊e.printStackTrace(); return false;public void clear()usernameText.setText("");passwdText.setText("");主界面:welcome.javapackage com.johnson;import java.awt.BorderLayout;import java.awt.Font;import java.sql.Connection;import java.sql.DriverManager;import java

19、.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import javax.swing.JButton;import javax.swing.JDialog;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTable;import jav

20、ax.swing.SwingConstants;import javax.swing.border.EmptyBorder;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;public class welcome extends JFrame private JPanel contentPane;private JTable table;String title="項(xiàng)目","內(nèi)容"String sqldat=null;/* * Launch the applic

21、ation. */*public static void main(String args) EventQueue.invokeLater(new Runnable() public void run() try welcome frame = new welcome();frame.setVisible(true); catch (Exception e) e.printStackTrace(););*/String username;welcome thisjf;JFrame loginjf;revisePasswd rePasswd;reviseInfo reInfo;public vo

22、id launch(String othername,JFrame otherjf,revisePasswd otherjd,reviseInfo otherjd2)this.setVisible(true);username=othername;sqlInit(username);loginjf=otherjf;rePasswd=otherjd;reInfo=otherjd2;public void launch()sqlInit(username);/* * Create the frame. */public welcome() setTitle("u4E2Au4EBAu7CF

23、Bu7EDF");setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(100, 100, 450, 300);contentPane = new JPanel();contentPane.setBorder(new EmptyBorder(5, 5, 5, 5);contentPane.setLayout(new BorderLayout(0, 0);setContentPane(contentPane);JPanel panel = new JPanel();contentPane.add(panel, BorderLa

24、yout.SOUTH);/修改密碼按鈕JButton revisePasswdBut = new JButton("u4FEEu6539u5BC6u7801");revisePasswdBut.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) rePasswd.setVisible(true);rePasswd.launch(username,thisjf););panel.add(revisePasswdBut);/修改信息按鈕JButton reviseIn

25、foBut = new JButton("u4FEEu6539u4FE1u606F");reviseInfoBut.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) reInfo.setVisible(true);reInfo.launch(username, thisjf););panel.add(reviseInfoBut);/退出登錄按鈕thisjf=this;/本窗口JButton quitBut = new JButton("u9000u51

26、FAu767Bu5F55");quitBut.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) loginjf.setVisible(true);thisjf.setVisible(false););panel.add(quitBut);/賬戶注銷按鈕JButton logoffBut = new JButton("u8D26u6237u6CE8u9500");logoffBut.addActionListener(new ActionListener

27、() public void actionPerformed(ActionEvent e) int rs=JOptionPane.showConfirmDialog(thisjf, "確定注銷該賬戶嗎?","注銷",JOptionPane.YES_NO_OPTION);if(rs=JOptionPane.YES_OPTION)sqlRemove();thisjf.setVisible(false);loginjf.setVisible(true);else/);panel.add(logoffBut);JLabel megLabel = new JLab

28、el("XXu7CFBu7EDF");megLabel.setFont(new Font("宋體", Font.BOLD, 20);megLabel.setHorizontalAlignment(SwingConstants.CENTER);contentPane.add(megLabel, BorderLayout.NORTH);sqldat=new String52;table = new JTable(sqldat,title);JScrollPane jsp=new JScrollPane(table);contentPane.add(jsp,

29、BorderLayout.CENTER);/數(shù)據(jù)庫讀出數(shù)據(jù)void sqlInit(String username) /-數(shù)據(jù)庫驗(yàn)證- /加載sql數(shù)據(jù)庫驅(qū)動 try Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); catch (ClassNotFoundException e) / TODO 自動生成的 catch 塊e.printStackTrace(); Connection ct=null; Statement st = null; ResultSet rs=null;try /創(chuàng)建數(shù)據(jù)庫

30、鏈接ct = DriverManager.getConnection("jdbc:sqlserver:/localhost:1433;DatabaseName=myuserdat","johnson","123");/創(chuàng)建statementst = ct.createStatement();/查詢數(shù)據(jù)庫rs=st.executeQuery("select username,passwd,sex,phone,adress from users where username='"+username+"

31、'");/結(jié)果輸出if(rs.next()sqldat00="用戶名"sqldat01=rs.getString(1);sqldat10="密碼"sqldat11=rs.getString(2);String sex=null;if(rs.getString(3).equals("m") sex="男"else sex="女"sqldat20="性別"sqldat21=sex;sqldat30="電話"sqldat31=rs.getStr

32、ing(4);sqldat40="地址"sqldat41=rs.getString(5);table.updateUI();rs.close();st.close();ct.close(); catch (SQLException e) / TODO 自動生成的 catch 塊e.printStackTrace();/數(shù)據(jù)庫中刪除記錄void sqlRemove()/-數(shù)據(jù)庫驗(yàn)證- /加載sql數(shù)據(jù)庫驅(qū)動 try Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); catch (C

33、lassNotFoundException e) / TODO 自動生成的 catch 塊e.printStackTrace(); Connection ct=null; Statement st = null; ResultSet rs=null;try /創(chuàng)建數(shù)據(jù)庫鏈接ct = DriverManager.getConnection("jdbc:sqlserver:/localhost:1433;DatabaseName=myuserdat","johnson","123");/創(chuàng)建statementst = ct.createS

34、tatement();/查詢數(shù)據(jù)庫rs=st.executeQuery("select username,passwd,sex,phone,adress from users where username='"+username+"'");/結(jié)果輸出if(rs.next()Statement stmt = null;stmt = ct.createStatement();String sql="delete from users where username='"+username+"'&qu

35、ot;stmt.executeUpdate(sql);stmt.close();rs.close();st.close();ct.close(); catch (SQLException e) / TODO 自動生成的 catch 塊e.printStackTrace();注冊界面:reg.javapackage com.johnson;import java.awt.BorderLayout;import java.awt.Color;import java.awt.FlowLayout;import java.awt.GridLayout;import java.awt.event.Act

36、ionEvent;import java.awt.event.ActionListener;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import javax.swing.ButtonGroup;import javax.swing.JButton;import javax.swing.JDialog;import javax.swing.JLabel;impor

37、t javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JPasswordField;import javax.swing.JRadioButton;import javax.swing.JTextField;import javax.swing.SwingConstants;import javax.swing.border.EmptyBorder;import javax.swing.UIManager;import javax.swing.border.LineBorder;public class r

38、eg extends JDialog private final JPanel contentPanel = new JPanel();private JTextField usernameText,phoneText,adressText;private JPasswordField passwdText,passwdText2;private JRadioButton maleBut,femaleBut;/* * Launch the application. */public static void main(String args) /try /reg dialog = new reg

39、();/dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);/dialog.setVisible(true);/ catch (Exception e) /e.printStackTrace();/public void launch()/* * Create the dialog. */public reg() setTitle("u6CE8u518C");/setBounds(100, 100, 450, 300);getContentPane().setLayout(new BorderLayout();

40、contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5);getContentPane().add(contentPanel, BorderLayout.CENTER);contentPanel.setLayout(new BorderLayout(0, 0);JLabel signLabel = new JLabel("u6253*u53F7u7684u4E3Au5FC5u586B");signLabel.setForeground(Color.RED);signLabel.setHorizontalAlignment(Swin

41、gConstants.CENTER);contentPanel.add(signLabel, BorderLayout.NORTH);JLabel nameLabel=new JLabel("*u7528u6237u540D");nameLabel.setHorizontalAlignment(SwingConstants.CENTER);JLabel passwdLabel=new JLabel("*u5BC6u7801");passwdLabel.setHorizontalAlignment(SwingConstants.CENTER);JLabel

42、 passwdLabel2=new JLabel("*u518Du6B21u8F93u5165u5BC6u7801");passwdLabel2.setHorizontalAlignment(SwingConstants.CENTER);JLabel sexLabel=new JLabel("*u6027u522B");sexLabel.setHorizontalAlignment(SwingConstants.CENTER);JLabel phoneLabel=new JLabel("電話");phoneLabel.setHoriz

43、ontalAlignment(SwingConstants.CENTER);JLabel adressLabel=new JLabel("地址");adressLabel.setHorizontalAlignment(SwingConstants.CENTER);JPanel panel = new JPanel();panel.setBorder(new LineBorder(new Color(0, 0, 0);usernameText=new JTextField(10);nameLabel.setLabelFor(usernameText);passwdText=n

44、ew JPasswordField(10);passwdLabel.setLabelFor(passwdText);passwdText2=new JPasswordField(10);passwdLabel2.setLabelFor(passwdText2);maleBut=new JRadioButton("男");maleBut.setSelected(true);femaleBut=new JRadioButton("女");phoneText=new JTextField(10);phoneLabel.setLabelFor(phoneText

45、);adressText=new JTextField(10);adressLabel.setLabelFor(adressText);JPanel sexPanel=new JPanel();sexLabel.setLabelFor(sexPanel);sexPanel.add(maleBut);sexPanel.add(femaleBut);ButtonGroup sexGroup=new ButtonGroup(); sexGroup.add(maleBut); sexGroup.add(femaleBut);contentPanel.add(panel, BorderLayout.CE

46、NTER);panel.setLayout(new GridLayout(6,2);panel.add(nameLabel);panel.add(usernameText);panel.add(passwdLabel);panel.add(passwdText);panel.add(passwdLabel2);panel.add(passwdText2);panel.add(sexLabel);panel.add(sexPanel);panel.add(phoneLabel);panel.add(phoneText);panel.add(adressLabel);panel.add(adres

47、sText);reg thisjd=this;JPanel buttonPane = new JPanel();buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT);getContentPane().add(buttonPane, BorderLayout.SOUTH);JButton okButton = new JButton("OK");okButton.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e)

48、 if(usernameText.getText().trim().equals("")JOptionPane.showMessageDialog(thisjd, "用戶名不能為空!");else if(passwdText.getText().trim().equals(passwdText2.getText().trim()if(sqlInsert()thisjd.setVisible(false);clear();elseclear();elseJOptionPane.showMessageDialog(thisjd, "密碼不一致!&q

49、uot;););okButton.setActionCommand("OK");buttonPane.add(okButton);getRootPane().setDefaultButton(okButton);JButton cancelButton = new JButton("Cancel");cancelButton.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) thisjd.setVisible(false););cancelB

50、utton.setActionCommand("Cancel");buttonPane.add(cancelButton);this.pack();this.setLocationRelativeTo(null);boolean sqlInsert()String username=usernameText.getText().trim();/加載sql數(shù)據(jù)庫驅(qū)動 try Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); catch (ClassNotFoundException

51、 e) / TODO 自動生成的 catch 塊e.printStackTrace(); Connection ct=null; Statement st=null; ResultSet rs=null;try /創(chuàng)建數(shù)據(jù)庫鏈接ct = DriverManager.getConnection("jdbc:sqlserver:/localhost:1433;DatabaseName=myuserdat","johnson","123");/創(chuàng)建statementst = ct.createStatement();/查詢數(shù)據(jù)庫rs = s

52、t.executeQuery("select passwd from users where username='"+username+"'");if(rs.next()return false;elseString sex=null;String sphone=null,sadress=null;String dphone=null,dadress=null;if(maleBut.isSelected() sex="m"else sex="f"if(phoneText.getText().trim().equals("")sphone=""dphone=""elsesphone=",phone"dphone="','"+phoneText.getText().trim();if(adressText.getText().trim().equals

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論