赞
踩
1:用Windows身份验证登陆
2. 展开安全性,登陆名,找到sa,然后双击它
3.设置密码,一会儿连接的时候要用到,我这里设置成2000
4.在状态这里设置授予和启用
5.右键服务器名,属性,安全性,选择SQL server和Windows身份验证模式
6.右键服务器名,重新启动
7.建立数据库和表,这里的数据库名为yu,表名为text1、text2、text3、
8:下载sqljdbc4.jar连接数据库的驱动
9.右键项目名(),建立包名(门票)选择刚刚下载好的sqljdbc4.jar导入
10.在刚建的包(门票)下建立类(Conn2),在这个类中输入以下代码连接:
代码:
import java.sql.*;
public class Conn2 {
static Connection con;
public Connection getConnection() {
try {//加载驱动
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
System.out.println("驱动加载成功");
}catch(ClassNotFoundException e){
e.printStackTrace();
}
try {
String url = "jdbc:sqlserver://localhost:1433;database = yu";
String user = "sa";
String password = "2000";
con = DriverManager.getConnection(url,user,password;//数据源
System.out.println("数据库连接成功");
}catch(SQLException e) {
e.printStackTrace();
}
return con;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Conn2 c = new Conn2();
Conn2.con = c.getConnection();
}
}
有可能出现的问题:出现【The server selected protocol version TLS10 is not accepted by client preferences [TLS12]、[ TLS13]】,数据库连接失败,意思是数据库服务端的协议版本是TLS10,而客户端协议版本是TLS12、TLS13。
解决办法:
只需要在你的设备上找到java.security改改安全协议的配置即可。删掉这两个:TLSv1、TLSv1.1或者直接在此电脑查询java.security后ctrl+a选中并删除即可。
11:在SqlServer中输入属性值并添加内容
Text1:
Text2:
Text3:
12:在eclipse中刚建的包(门票)下建立类(ssm),在该类中输入源程序。
源程序:
package 门票;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.util.Vector;
public class ssm {
JFrame mainFrame=new JFrame("景点门票销售系统");
JFrame czFrame=new JFrame("营业员管理");
JFrame c1=new JFrame("查询");
JFrame c2=new JFrame("修改");
JFrame c3=new JFrame("删除");
JFrame c4=new JFrame("添加");
JFrame c5=new JFrame("门票管理");
JFrame c6=new JFrame("游客管理");
JFrame c7=new JFrame("查询");
JFrame c8=new JFrame("添加");
JFrame c9=new JFrame("修改");
JFrame c10=new JFrame("删除");
JButton btn=new JButton("景点门票销售系统");
JButton btn1=new JButton("营业员管理");
JButton btn2=new JButton("门票管理");
JButton btn3=new JButton("游客管理");
JButton btn4=new JButton("修改");
JButton btn5=new JButton("添加");
JButton btn6=new JButton("删除");
JButton btn7=new JButton("返回");
public void win1(){
mainFrame.getContentPane().setLayout(null);
mainFrame.setVisible(true);
mainFrame.setBounds(300,150,600,400);
//设置背景图片
ImageIcon bg=new ImageIcon("C:\\data\\素材.jpg");
//把背景图片显示在一个标签
JLabel label=new JLabel(bg);
//把标签的大小位置设置为图片刚好填充整个面
label.setBounds(0,0,bg.getIconWidth(),bg.getIconHeight());
//添加图片到frame的第二层
mainFrame.getLayeredPane().add(label);
//获取frame的最上层面板为了设置其背景颜色(JPanel有设置透明的方法)
//测试用的JPanel
btn.setSize(391,66);
btn.setLocation(98,116);
btn.setFocusPainted(false);//设置成透明
btn.setBorderPainted(false);//聚焦阴影不显示
btn.setContentAreaFilled(false);//外边框不显示
Font fnt = new Font("Dialog", Font.PLAIN + Font.BOLD, 40);//文字样式,
btn.setFont(fnt);
btn.setForeground(Color.black);//文字颜色
mainFrame.getContentPane().add(btn);
btn1.setSize(140,40);
btn1.setLocation(30,300);
btn2.setSize(140,40);
btn2.setLocation(215,300);
btn3.setSize(140,40);
btn3.setLocation(400,300);
mainFrame.getContentPane().add(btn1);
mainFrame.getContentPane().add(btn2);
mainFrame.getContentPane().add(btn3);
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
mainFrame.setVisible(false);
win2();
}
});
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
mainFrame.setVisible(false);
c5();
}
});
btn3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
mainFrame.setVisible(false);
c6();
}
});
}
public void win2(){
JButton b1=new JButton("查询");
czFrame.getContentPane().setLayout(null);
czFrame.setVisible(true);
czFrame.setBounds(300,150,600,400);
btn4.setSize(80,40);
btn4.setLocation(160,100);
btn5.setSize(80,40);
btn5.setLocation(160,200);
btn6.setSize(80,40);
btn6.setLocation(310,200);
btn7.setSize(80,40);
btn7.setLocation(235,300);
b1.setSize(80,40);
b1.setLocation(310,100);
czFrame.getContentPane().add(btn4);
czFrame.getContentPane().add(btn5);
czFrame.getContentPane().add(btn6);
czFrame.getContentPane().add(btn7);
czFrame.getContentPane().add(b1);
btn7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
czFrame.setVisible(false);
win1();
}
});
btn4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
czFrame.setVisible(false);
c2();
}
});
btn6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
c1.setVisible(false);
c3();
}
});
btn5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
czFrame.setVisible(false);
c4();
}
});
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {//管理员查询功能
Connection conn = null;
PreparedStatement ps = null;
ResultSet res = null;
JTable jtable;
JScrollPane jscrollpane = new JScrollPane();
Vector<String> columnNames = null;
Vector<Vector<String>> rowData = null;//存放所有管理员信息
JPanel jpforbutton = new JPanel(new GridLayout(1,1));
columnNames = new Vector<String>();
columnNames.add("姓名");
columnNames.add("年龄");
columnNames.add("工作编号");
rowData = new Vector<Vector<String>>();
//查询所有营业员的信息
try {
String dbURL = "jdbc:sqlserver://localhost:1433;databasename=yu"; // 1433是端口,"STUDB"是数据库名称
String userName = "sa"; // 用户名
String userPwd = "2000"; // 密码
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection(dbURL,userName,userPwd);
ps = conn.prepareStatement("SELECT * FROM text3");
res = ps.executeQuery();
while (res.next())
{
Vector<String> hang = new Vector<String>();
hang.add(res.getString(1));
hang.add(res.getString(2));
hang.add(res.getString(3));
rowData.add(hang);
}
System.out.println("load ok!");
}catch (Exception q){
q.printStackTrace();
System.out.println("go die");
}finally{
try{
res.close();
ps.close();
conn.close();
System.out.println("close ok");
}catch (SQLException o){
o.printStackTrace();
System.out.println("go die 2");
}
}
czFrame.setVisible(false);
c1();
jtable = new JTable(rowData,columnNames);
jscrollpane = new JScrollPane(jtable);
c1.getContentPane().add(jscrollpane);
c1.getContentPane().setLayout(new GridLayout(2,5));
c1.getContentPane().add(jpforbutton);
JButton b=new JButton("返回");
b.setSize(80,40);
b.setLocation(310,50);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
c1.setVisible(false);
win2();
}
});
c1.getContentPane().add(b);
}
});
}
public void c1(){
c1.getContentPane().setLayout(null);
c1.setVisible(true);
c1.setBounds(300,150,600,400);
btn7.setSize(80,40);
btn7.setLocation(460,300);
btn7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
c1.setVisible(false);
win1();
}
});
}
public void c2(){
c2.getContentPane().setLayout(null);
c2.setVisible(true);
c2.setBounds(300,150,600,400);
JLabel lab1= new JLabel("姓名:");
JLabel lab2= new JLabel("年龄:");
JLabel lab3= new JLabel("工作编号:");
JLabel lab4= new JLabel("修改人姓名:");
JTextField jtex1= new JTextField();
JTextField jtex2= new JTextField();
JTextField jtex3= new JTextField();
JTextField jtex4= new JTextField();
lab1.setSize(50,30);
lab1.setLocation(100,90);
lab2.setSize(50,30);
lab2.setLocation(100,160);
lab3.setSize(100,30);
lab3.setLocation(100,230);
lab4.setSize(100,30);
lab4.setLocation(100,20);
c2.getContentPane().add(lab1);
c2.getContentPane().add(lab2);
c2.getContentPane().add(lab3);
c2.getContentPane().add(lab4);
jtex1.setSize(50,30);
jtex1.setLocation(180,90);
jtex2.setSize(50,30);
jtex2.setLocation(180,160);
jtex3.setSize(50,30);
jtex3.setLocation(180,230);
jtex4.setSize(50,30);
jtex4.setLocation(180,20);
c2.getContentPane().add(jtex1);
c2.getContentPane().add(jtex2);
c2.getContentPane().add(jtex3);
c2.getContentPane().add(jtex4);
JButton bt1=new JButton("修改");
JButton bt2=new JButton("返回");
bt1.setSize(100,50);
bt1.setLocation(400,80);
bt2.setSize(100,50);
bt2.setLocation(400,200);
c2.getContentPane().add(bt1);
c2.getContentPane().add(bt2);
bt2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
c2.setVisible(false);
win2();
}
});
bt1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String Name=jtex4.getText();
String updateName=jtex1.getText();
String age=jtex2.getText();
String id=jtex3.getText();
//修改营业员的姓名,年龄,工作编号;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//加载对应的jdbc驱动
String url="jdbc:sqlserver://localhost:1433; DatabaseName=yu";
//配置连接字符串
String user="sa";//sa超级管理员
String password="2000";//密码
Connection conn=DriverManager.getConnection(url,user,password);
//创建数据库连接对象
Statement st=conn.createStatement();
//创建SQL语句执行对象
String strSQL1="update dbo.yyy set 姓名='"+updateName+"' where 姓名='"+Name+"'";
String strSQL2="update dbo.yyy set 年龄='"+age+"' where 姓名='"+Name+"'";
String strSQL3="update dbo.yyy set 工作编号='"+id+"' where 姓名='"+Name+"'";
int rs1=st.executeUpdate(strSQL1);
int rs2=st.executeUpdate(strSQL2);
int rs3=st.executeUpdate(strSQL3);
if(rs1==1||rs2==1||rs3==1) {
JOptionPane.showMessageDialog(null,"修改成功");
}
else{
JOptionPane.showMessageDialog(null,"修改失败");
}
conn.close();
//关闭数据库连接
}
catch (ClassNotFoundException ex) {
System.out.println("没有找到对应的数据库驱动类");
}
catch (SQLException ex) {
System.out.println("数据库连接或者是数据库操作失败");
}
}
});
}
public void c3(){
c3.getContentPane().setLayout(null);
c3.setVisible(true);
c3.setBounds(300,150,600,400);
JButton bt1=new JButton("删除");
JButton bt2=new JButton("返回");
JLabel lab1= new JLabel("姓名");
JTextField jtf=new JTextField();
lab1.setSize(50,30);
lab1.setLocation(100,60);
jtf.setSize(50,30);
jtf.setLocation(200,60);
c3.getContentPane().add(lab1);
c3.getContentPane().add(jtf);
bt1.setSize(100,50);
bt1.setLocation(400,80);
bt2.setSize(100,50);
bt2.setLocation(400,200);
c3.getContentPane().add(bt1);
c3.getContentPane().add(bt2);
bt2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
c3.setVisible(false);
win2();
}
});
bt1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String delName=jtf.getText();
// 通过姓名删除营业员
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//加载对应的jdbc驱动
String url="jdbc:sqlserver://localhost:1433; DatabaseName=yu";
//配置连接字符串
String user="sa";//sa超级管理员
String password="2000";//密码
Connection conn=DriverManager.getConnection(url,user,password);
//创建数据库连接对象
Statement st=conn.createStatement();
//创建SQL语句执行对象
String strSQL="delete from dbo.text3 where 姓名='"+delName+"' ";
int rs=st.executeUpdate(strSQL);
if(rs==1) {
JOptionPane.showMessageDialog(null,"删除成功");
}
else{
JOptionPane.showMessageDialog(null,"删除失败");
}
conn.close();
//关闭数据库连接
}
catch (ClassNotFoundException ex) {
System.out.println("没有找到对应的数据库驱动类");
}
catch (SQLException ex) {
System.out.println("数据库连接或者是数据库操作失败");
}
}
});
}
public void c4() {
c4.getContentPane().setLayout(null);
c4.setVisible(true);
c4.setBounds(300, 150, 600, 400);
JLabel lab1 = new JLabel("姓名");
JLabel lab2 = new JLabel("年龄");
JLabel lab3 = new JLabel("编号");
JTextField jtex1 = new JTextField();
JTextField jtex2 = new JTextField();
JTextField jtex3 = new JTextField();
lab1.setSize(50, 30);
lab1.setLocation(100, 60);
lab2.setSize(50, 30);
lab2.setLocation(100, 130);
lab3.setSize(50, 30);
lab3.setLocation(100, 200);
c4.getContentPane().add(lab1);
c4.getContentPane().add(lab2);
c4.getContentPane().add(lab3);
jtex1.setSize(50, 30);
jtex1.setLocation(180, 60);
jtex2.setSize(50, 30);
jtex2.setLocation(180, 130);
jtex3.setSize(50, 30);
jtex3.setLocation(180, 200);
c4.getContentPane().add(jtex1);
c4.getContentPane().add(jtex2);
c4.getContentPane().add(jtex3);
JButton bt1 = new JButton("添加");
JButton bt2 = new JButton("返回");
bt1.setSize(100, 50);
bt1.setLocation(400, 80);
bt2.setSize(100, 50);
bt2.setLocation(400, 200);
c4.getContentPane().add(bt1);
c4.getContentPane().add(bt2);
bt2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
c4.setVisible(false);
win2();
}
});
bt1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String addName=jtex1.getText();
String addage=jtex2.getText();
String addid=jtex3.getText();
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//加载对应的jdbc驱动
String url="jdbc:sqlserver://localhost:1433; DatabaseName= yu";
//配置连接字符串
String user="sa";//sa超级管理员
String password="2000";//密码
Connection conn=DriverManager.getConnection(url,user,password);
//创建数据库连接对象
Statement st=conn.createStatement();
//创建SQL语句执行对象
// 添加营业员
String strSQL="insert into dbo.text3 values('"+addName+"','"+addage+"','"+addid+"')";
int rs=st.executeUpdate(strSQL);
if(rs==1) {
JOptionPane.showMessageDialog(null,"添加成功");
}
else{
JOptionPane.showMessageDialog(null,"添加失败");
}
conn.close();
//关闭数据库连接
}
catch (ClassNotFoundException ex) {
System.out.println("没有找到对应的数据库驱动类");
}
catch (SQLException ex) {
System.out.println("数据库连接或者是数据库操作失败");
}
}
});
}
public void c5(){
c5.getContentPane().setLayout(null);
c5.setVisible(true);
c5.setBounds(300,150,600,400);
JLabel lab1= new JLabel("需查(编号)");
JLabel lab2= new JLabel("门票编号");
JLabel lab3= new JLabel("数量");
JLabel lab4= new JLabel("门票名");
JLabel lab5= new JLabel("票价");
JTextField jtex1= new JTextField();
JTextField jtex2= new JTextField();
JTextField jtex3= new JTextField();
JTextField jtex4= new JTextField();
JTextField jtex5= new JTextField();
lab1.setSize(100,30);
lab1.setLocation(100,20);
lab2.setSize(100,30);
lab2.setLocation(100,70);
lab3.setSize(100,30);
lab3.setLocation(100,120);
lab4.setSize(100,30);
lab4.setLocation(100,170);
lab5.setSize(50,30);
lab5.setLocation(100,230);
c5.getContentPane().add(lab1);
c5.getContentPane().add(lab2);
c5.getContentPane().add(lab3);
c5.getContentPane().add(lab4);
c5.getContentPane().add(lab5);
jtex1.setSize(50,30);
jtex1.setLocation(180,20);
jtex2.setSize(50,30);
jtex2.setLocation(180,70);
jtex3.setSize(50,30);
jtex3.setLocation(180,120);
jtex4.setSize(50,30);
jtex4.setLocation(180,170);
jtex5.setSize(50,30);
jtex5.setLocation(180,230);
c5.getContentPane().add(jtex1);
c5.getContentPane().add(jtex2);
c5.getContentPane().add(jtex3);
c5.getContentPane().add(jtex4);
c5.getContentPane().add(jtex5);
JButton bt1=new JButton("查询");
JButton bt2=new JButton("删除");
JButton bt3=new JButton("修改");
JButton bt4=new JButton("添加");
JButton bt5=new JButton("返回");
JButton bt6=new JButton("清空");
bt1.setSize(100,50);
bt1.setLocation(400,10);
bt2.setSize(100,50);
bt2.setLocation(400,100);
bt3.setSize(100,50);
bt3.setLocation(400,190);
bt4.setSize(100,50);
bt4.setLocation(400,280);
bt5.setSize(100,50);
bt5.setLocation(50,280);
bt6.setSize(100,50);
bt6.setLocation(200,280);
c5.getContentPane().add(bt1);
c5.getContentPane().add(bt2);
c5.getContentPane().add(bt3);
c5.getContentPane().add(bt4);
c5.getContentPane().add(bt5);
c5.getContentPane().add(bt6);
bt6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jtex1.setText("");
jtex2.setText("");
jtex3.setText("");
jtex4.setText("");
jtex5.setText("");
}
});
bt5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
c5.setVisible(false);
win1();
}
});
bt4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String add1=jtex2.getText();
String add2=jtex3.getText();
String add3=jtex4.getText();
String add4=jtex5.getText();
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//加载对应的jdbc驱动
String url="jdbc:sqlserver://localhost:1433; DatabaseName= yu";
//配置连接字符串
String user="sa";//sa超级管理员
String password="2000";//密码
Connection conn=DriverManager.getConnection(url,user,password);
//创建数据库连接对象
Statement st=conn.createStatement();
//创建SQL语句执行对象
// 添加营业员
String strSQL="insert into dbo.text1 values('"+add1+"','"+add2+"','"+add3+"','"+add4+"')";
int rs=st.executeUpdate(strSQL);
if(rs==1) {
JOptionPane.showMessageDialog(null,"添加成功");
}
else{
JOptionPane.showMessageDialog(null,"添加失败");
}
conn.close();
//关闭数据库连接
}
catch (ClassNotFoundException ex) {
System.out.println("没有找到对应的数据库驱动类");
}
catch (SQLException ex) {
System.out.println("数据库连接或者是数据库操作失败");
}
}
});
bt3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String updateName=jtex1.getText();
String a=jtex2.getText();
String b=jtex3.getText();
String c=jtex4.getText();
String d=jtex5.getText();
//修改营业员的姓名,年龄,工作编号;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//加载对应的jdbc驱动
String url="jdbc:sqlserver://localhost:1433; DatabaseName=yu";
//配置连接字符串
String user="sa";//sa超级管理员
String password="2000";//密码
Connection conn=DriverManager.getConnection(url,user,password);
//创建数据库连接对象
Statement st=conn.createStatement();
//创建SQL语句执行对象
String strSQL1="update dbo.text1 set 门票编号='"+a+"' where 姓名='"+updateName+"'";
String strSQL2="update dbo.text1 set 数量='"+b+"' where 姓名='"+updateName+"'";
String strSQL3="update dbo.text1 set 门票名='"+c+"' where 姓名='"+updateName+"'";
String strSQL4="update dbo.text1 set 票价='"+d+"' where 姓名='"+updateName+"'";
int rs1=st.executeUpdate(strSQL1);
int rs2=st.executeUpdate(strSQL2);
int rs3=st.executeUpdate(strSQL3);
int rs4=st.executeUpdate(strSQL4);
if(rs1==1||rs2==1||rs3==1||rs4==1) {
JOptionPane.showMessageDialog(null,"修改成功");
}
else{
JOptionPane.showMessageDialog(null,"修改失败");
}
conn.close();
//关闭数据库连接
}
catch (ClassNotFoundException ex) {
System.out.println("没有找到对应的数据库驱动类");
}
catch (SQLException ex) {
System.out.println("数据库连接或者是数据库操作失败");
}
}
});
bt2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String delName=jtex1.getText();
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//加载对应的jdbc驱动
String url="jdbc:sqlserver://localhost:1433; DatabaseName=yu";
//配置连接字符串
String user="sa";
String password="2000";//密码
Connection conn=DriverManager.getConnection(url,user,password);
//创建数据库连接对象
Statement st=conn.createStatement();
//创建SQL语句执行对象
String strSQL="delete from dbo.text1 where 门票编号='"+delName+"' ";
int rs=st.executeUpdate(strSQL);
if(rs==1) {//影响了一行数据
JOptionPane.showMessageDialog(null,"删除成功");
}
else{
JOptionPane.showMessageDialog(null,"删除失败");
}
conn.close();
//关闭数据库连接
}
catch (ClassNotFoundException ex) {
System.out.println("没有找到对应的数据库驱动类");
}
catch (SQLException ex) {
System.out.println("数据库连接或者是数据库操作失败");
}
}
});
bt1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String inputName=jtex1.getText();
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//加载对应的jdbc驱动
String url="jdbc:sqlserver://localhost:1433; DatabaseName=yu";
//配置连接字符串
String user="sa";//sa超级管理员
String password="2000";//密码
Connection conn=DriverManager.getConnection(url,user,password);
//创建数据库连接对象
Statement st=conn.createStatement();
//创建SQL语句执行对象
//通过门票编号查询门票信息
String strSQL="(Select* from dbo.text1 where 门票编号='"+inputName+"' )";
ResultSet rs=st.executeQuery(strSQL);
if(rs.next())
{
jtex2.setText(rs.getString(1));
jtex3.setText(rs.getString(2));
jtex4.setText(rs.getString(3));
jtex5.setText(rs.getString(4));
}
else
{
JOptionPane.showMessageDialog(null, "您查询的不存在,请重新输入");
}
conn.close();
//关闭数据库连接
}
catch (ClassNotFoundException ex) {
System.out.println("没有找到对应的数据库驱动类");
}
catch (SQLException ex) {
System.out.println("数据库连接或者是数据库操作失败");
}
}
});
}
public void c6(){
c6.getContentPane().setLayout(null);
c6.setVisible(true);
c6.setBounds(300,150,600,400);
JButton b1=new JButton("查询");
JButton b2=new JButton("添加");
JButton b3=new JButton("修改");
JButton b4=new JButton("删除");
JButton b5=new JButton("返回");
b1.setSize(60, 50);
b1.setLocation(180, 100);
b2.setSize(60, 50);
b2.setLocation(180, 200);
b3.setSize(60, 50);
b3.setLocation(330, 200);
b4.setSize(60, 50);
b4.setLocation(330, 100);
b5.setSize(60, 50);
b5.setLocation(255, 300);
c6.getContentPane().add(b1);
c6.getContentPane().add(b2);
c6.getContentPane().add(b3);
c6.getContentPane().add(b4);
c6.getContentPane().add(b5);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
c6.setVisible(false);
c7();
}
});
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
c6.setVisible(false);
c8();
}
});
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
c6.setVisible(false);
c9();
}
});
b4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
c6.setVisible(false);
c10();
}
});
b5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
c6.setVisible(false);
win1();
}
});
}
public void c7(){
c7.getContentPane().setLayout(null);
c7.setVisible(true);
c7.setBounds(300,150,600,400);
JLabel lab1= new JLabel("需查(姓名)");
JLabel lab2= new JLabel("姓名");
JLabel lab3= new JLabel("年龄");
JLabel lab4= new JLabel("身份证号");
JLabel lab5= new JLabel("性别");
JLabel lab6= new JLabel("类型");
JTextField jtex1= new JTextField();
JTextField jtex2= new JTextField();
JTextField jtex3= new JTextField();
JTextField jtex4= new JTextField();
JTextField jtex5= new JTextField();
JTextField jtex6= new JTextField();
lab1.setSize(100,30);
lab1.setLocation(100,20);
lab2.setSize(50,30);
lab2.setLocation(100,70);
lab3.setSize(50,30);
lab3.setLocation(100,120);
lab4.setSize(100,30);
lab4.setLocation(100,170);
lab5.setSize(50,30);
lab5.setLocation(100,230);
lab6.setSize(50,30);
lab6.setLocation(100,280);
c7.getContentPane().add(lab1);
c7.getContentPane().add(lab2);
c7.getContentPane().add(lab3);
c7.getContentPane().add(lab4);
c7.getContentPane().add(lab5);
c7.getContentPane().add(lab6);
jtex1.setSize(50,30);
jtex1.setLocation(180,20);
jtex2.setSize(50,30);
jtex2.setLocation(180,70);
jtex3.setSize(50,30);
jtex3.setLocation(180,120);
jtex4.setSize(50,30);
jtex4.setLocation(180,170);
jtex5.setSize(50,30);
jtex5.setLocation(180,230);
jtex6.setSize(50,30);
jtex6.setLocation(180,280);
c7.getContentPane().add(jtex1);
c7.getContentPane().add(jtex2);
c7.getContentPane().add(jtex3);
c7.getContentPane().add(jtex4);
c7.getContentPane().add(jtex5);
c7.getContentPane().add(jtex6);
JButton bt1=new JButton("查询");
JButton bt2=new JButton("返回");
bt1.setSize(100,50);
bt1.setLocation(400,80);
bt2.setSize(100,50);
bt2.setLocation(400,200);
c7.getContentPane().add(bt1);
c7.getContentPane().add(bt2);
bt2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
c7.setVisible(false);
c6();
}
});
bt1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String inputName=jtex1.getText();
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//加载对应的jdbc驱动
String url="jdbc:sqlserver://localhost:1433; DatabaseName=yu";
//配置连接字符串
String user="sa";//sa超级管理员
String password="2000";//密码
Connection conn=DriverManager.getConnection(url,user,password);
//创建数据库连接对象
Statement st=conn.createStatement();
//创建SQL语句执行对象
//通过游客姓名查询游客信息
String strSQL="(Select* from dbo.text2 where 姓名='"+inputName+"' )";
ResultSet rs=st.executeQuery(strSQL);
if(rs.next())
{
jtex2.setText(rs.getString(1));
jtex3.setText(rs.getString(2));
jtex4.setText(rs.getString(3));
jtex5.setText(rs.getString(4));
jtex6.setText(rs.getString(5));
jtex1.setText("");
}
else
{
JOptionPane.showMessageDialog(null, "您查询的不存在,请重新输入");
}
conn.close();
//关闭数据库连接
}
catch (ClassNotFoundException ex) {
System.out.println("没有找到对应的数据库驱动类");
}
catch (SQLException ex) {
System.out.println("数据库连接或者是数据库操作失败");
}
}
});
}
public void c8(){
c8.getContentPane().setLayout(null);
c8.setVisible(true);
c8.setBounds(300,150,600,400);
JLabel lab1= new JLabel("姓名");
JLabel lab2= new JLabel("年龄");
JLabel lab3= new JLabel("身份证号");
JLabel lab4= new JLabel("性别");
JLabel lab5= new JLabel("类型");
JTextField jtex1= new JTextField();
JTextField jtex2= new JTextField();
JTextField jtex3= new JTextField();
JTextField jtex4= new JTextField();
JTextField jtex5= new JTextField();
lab1.setSize(50,30);
lab1.setLocation(100,20);
lab2.setSize(50,30);
lab2.setLocation(100,70);
lab3.setSize(100,30);
lab3.setLocation(100,120);
lab4.setSize(50,30);
lab4.setLocation(100,170);
lab5.setSize(50,30);
lab5.setLocation(100,230);
c8.getContentPane().add(lab1);
c8.getContentPane().add(lab2);
c8.getContentPane().add(lab3);
c8.getContentPane().add(lab4);
c8.getContentPane().add(lab5);
jtex1.setSize(50,30);
jtex1.setLocation(180,20);
jtex2.setSize(50,30);
jtex2.setLocation(180,70);
jtex3.setSize(50,30);
jtex3.setLocation(180,120);
jtex4.setSize(50,30);
jtex4.setLocation(180,170);
jtex5.setSize(50,30);
jtex5.setLocation(180,230);
c8.getContentPane().add(jtex1);
c8.getContentPane().add(jtex2);
c8.getContentPane().add(jtex3);
c8.getContentPane().add(jtex4);
c8.getContentPane().add(jtex5);
JButton bt1=new JButton("查询");
JButton bt2=new JButton("返回");
bt1.setSize(100,50);
bt1.setLocation(400,80);
bt2.setSize(100,50);
bt2.setLocation(400,200);
c8.getContentPane().add(bt1);
c8.getContentPane().add(bt2);
bt2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
c8.setVisible(false);
c6();
}
});
bt1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String addName=jtex1.getText();
String addage=jtex2.getText();
String addid=jtex3.getText();
String addsex=jtex4.getText();
String addtype=jtex5.getText();
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//加载对应的jdbc驱动
String url="jdbc:sqlserver://localhost:1433; DatabaseName=yu";
//配置连接字符串
String user="sa";//sa超级管理员
String password="2000";//密码
Connection conn=DriverManager.getConnection(url,user,password);
//创建数据库连接对象
Statement st=conn.createStatement();
//创建SQL语句执行对象
//添加游客信息
String strSQL="insert into dbo.text2 values('"+addName+"','"+addage+"','"+addid+"','"+addsex+"','"+addtype+"')";
int rs=st.executeUpdate(strSQL);
if(rs==1) {
JOptionPane.showMessageDialog(null,"添加成功");
}
else{
JOptionPane.showMessageDialog(null,"添加失败");
}
conn.close();
//关闭数据库连接
}
catch (ClassNotFoundException ex) {
System.out.println("没有找到对应的数据库驱动类");
}
catch (SQLException ex) {
System.out.println("数据库连接或者是数据库操作失败");
}
}
});
}
public void c9(){
c9.getContentPane().setLayout(null);
c9.setVisible(true);
c9.setBounds(300,150,600,400);
JLabel lab1= new JLabel("需修改姓名");
JLabel lab2= new JLabel("姓名");
JLabel lab3= new JLabel("年龄");
JLabel lab4= new JLabel("身份证号");
JLabel lab5= new JLabel("性别");
JLabel lab6= new JLabel("类型");
JTextField jtex1= new JTextField();
JTextField jtex2= new JTextField();
JTextField jtex3= new JTextField();
JTextField jtex4= new JTextField();
JTextField jtex5= new JTextField();
JTextField jtex6= new JTextField();
lab1.setSize(100,30);
lab1.setLocation(100,60);
lab2.setSize(50,30);
lab2.setLocation(100,110);
lab3.setSize(50,30);
lab3.setLocation(100,160);
lab4.setSize(100,30);
lab4.setLocation(100,210);
lab5.setSize(50,30);
lab5.setLocation(100,260);
lab6.setSize(50,30);
lab6.setLocation(100,310);
c9.getContentPane().add(lab1);
c9.getContentPane().add(lab2);
c9.getContentPane().add(lab3);
c9.getContentPane().add(lab4);
c9.getContentPane().add(lab5);
c9.getContentPane().add(lab6);
jtex1.setSize(50,30);
jtex1.setLocation(180,60);
jtex2.setSize(50,30);
jtex2.setLocation(180,110);
jtex3.setSize(50,30);
jtex3.setLocation(180,160);
jtex4.setSize(50,30);
jtex4.setLocation(180,210);
jtex5.setSize(50,30);
jtex5.setLocation(180,260);
jtex6.setSize(50,30);
jtex6.setLocation(180,310);
c9.getContentPane().add(jtex1);
c9.getContentPane().add(jtex2);
c9.getContentPane().add(jtex3);
c9.getContentPane().add(jtex4);
c9.getContentPane().add(jtex5);
c9.getContentPane().add(jtex6);
JButton bt1=new JButton("修改");
JButton bt2=new JButton("返回");
bt1.setSize(100,50);
bt1.setLocation(400,80);
bt2.setSize(100,50);
bt2.setLocation(400,200);
c9.getContentPane().add(bt1);
c9.getContentPane().add(bt2);
bt2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
c9.setVisible(false);
c6();
}
});
bt1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String Name=jtex1.getText();
String updateName=jtex2.getText();
String age=jtex3.getText();
String id=jtex4.getText();
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//加载对应的jdbc驱动
String url="jdbc:sqlserver://localhost:1433; DatabaseName=yu";
//配置连接字符串
String user="sa";//sa超级管理员
String password="2000";//密码
Connection conn=DriverManager.getConnection(url,user,password);
//创建数据库连接对象
Statement st=conn.createStatement();
//创建SQL语句执行对象
//通过游客姓名更新游客信息
String strSQL1="update dbo.text2 set 姓名='"+updateName+"' where 姓名='"+Name+"'";
String strSQL2="update dbo.text2 set 年龄='"+age+"' where 姓名='"+Name+"'";
String strSQL3="update dbo.text2 set 身份证号='"+id+"' where 姓名='"+Name+"'";
int rs1=st.executeUpdate(strSQL1);
int rs2=st.executeUpdate(strSQL2);
int rs3=st.executeUpdate(strSQL3);
if(rs1==1||rs2==1||rs3==1) {
JOptionPane.showMessageDialog(null,"修改成功");
}
else{
JOptionPane.showMessageDialog(null,"修改失败");
}
conn.close();
//关闭数据库连接
}
catch (ClassNotFoundException ex) {
System.out.println("没有找到对应的数据库驱动类");
}
catch (SQLException ex) {
System.out.println("数据库连接或者是数据库操作失败");
}
}
});
}
public void c10(){
c10.getContentPane().setLayout(null);
c10.setVisible(true);
c10.setBounds(300,150,600,400);
JButton bt1=new JButton("删除");
JButton bt2=new JButton("返回");
JLabel lab1= new JLabel("姓名");
JTextField jtf=new JTextField();
lab1.setSize(50,30);
lab1.setLocation(100,60);
jtf.setSize(50,30);
jtf.setLocation(200,60);
c10.getContentPane().add(lab1);
c10.getContentPane().add(jtf);
bt1.setSize(100,50);
bt1.setLocation(400,80);
bt2.setSize(100,50);
bt2.setLocation(400,200);
c10.getContentPane().add(bt1);
c10.getContentPane().add(bt2);
bt2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
c10.setVisible(false);
c6();
}
});
bt1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String delName=jtf.getText();
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//加载对应的jdbc驱动
String url="jdbc:sqlserver://localhost:1433; DatabaseName=yu";
//配置连接字符串
String user="sa";
String password="2000";//密码
Connection conn=DriverManager.getConnection(url,user,password);
//创建数据库连接对象
Statement st=conn.createStatement();
//创建SQL语句执行对象
String strSQL="delete from dbo.text2 where 姓名='"+delName+"' ";
int rs=st.executeUpdate(strSQL);
if(rs==1) {//影响了一行数据
jtf.setText("");
JOptionPane.showMessageDialog(null,"删除成功");
}
else{
JOptionPane.showMessageDialog(null,"删除失败");
}
conn.close();
//关闭数据库连接
}
catch (ClassNotFoundException ex) {
System.out.println("没有找到对应的数据库驱动类");
}
catch (SQLException ex) {
System.out.println("数据库连接或者是数据库操作失败");
}
}
});
}
public static void main(String[] args) {
ssm caidan=new ssm();
caidan.win1();;
}
protected static void btnActionPerformed(ActionEvent arg0) {
}
}
13.运行结果:
14.对运行结果进行测试:
(1)
(2)
(3)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。