锐单电子商城 , 一站式电子元器件采购平台!
  • 电话:400-990-0325

JAVA课程设计作业—— Java与数据库连接操作(未完成)

时间:2023-08-05 09:37:00 51tjl压接型矩形连接器

1connection部分

package com.tjl.jdbc;  import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties;  public class JDBCUtils {  //全局  private static String driver;  private static String url;  private static String username;  private static String password;    //静态语句块  static {   //JDBCUtils.class获得对象   //getClassLoader()类加载器   //getResourceAsStream("db.properties")将资源文件加载到输入流   InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("db.properties");      //创建Property   Properties p= new Properties();      //加载流文件   try {    p.load(is);    driver = p.getProperty("driver");    url = p.getProperty("url");    username = p.getProperty("username");    password =p.getProperty("password");    //加载MySQL驱动    Class.forName(driver);    System.out.println("驱动加载成功!");   } catch (Exception e) {    // TODO 自动生成的 catch 块    e.printStackTrace();   }  }   ////获取连接对象的方法  public static Connection getConnection() {   try {    System.out.println("数据库连接成功");    return DriverManager.getConnection(url, username, password);       } catch (SQLException e) {    // TODO 自动生成的 catch 块    System.out.println("数据库连接失败");    e.printStackTrace();   }      return null;  }    public static void close (Connection conn, Statement statement,ResultSet result){        try {     if(result!=null) {      result.close();      result=null;           }     if(statement!=null) {      statement.close();      statement=null;           }     if(conn!=null) {      conn.close();      conn=null;           }         } catch (SQLException e) {     // TODO 自动生成的 catch 块     e.printStackTrace();    }     } } 

2登陆主页面

package com.tjl.view; import javax.swing.*;  import com.tjl.bean.User; import com.tjl.jdbc.JDBCUtils;  import java.awt.*; import java.awt.event.ActionListener; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.awt.event.ActionEvent; public  class login2 extends JFrame{  //标签  private JLabel lable1;  private JLabel lable2;  //文本框  private JTextField text1;  private JTextField text2;  //按钮  private JButton bt1;  private JButton bt2;      String sname;  String spass;  //构造函数  public login2()  {   this.init();   this.addComponent();   this.addListener();   System.out.println("8");  }    public void init()  {   this.setSize(500,400);   this.setVisible(true);   this.setTitle("登录界面");   this.setLayout(null);   this.setLocation(700, 300);  }  private void addComponent()  {   lable1 = new JLabel("用户名");   lable1.setSize(100,70);   lable1.setLocation(100,80);   this.add(lable1);   lable2 = new JLabel("密    码");   lable2.setSize(100,70);   lable2.setLocation(100,130);   this.add(lable2);      text1 = new JTextField();   text1.setSize(150,30);   text1.setLocation(160,100);   this.add(text1);   text2 = new JTextField();   text2.setSize(150,30);   text2.setLocation(160,150);   this.add(text2);     bt1 = new JButton("登录");   bt1.setSize(70,30);    bt1.setLocation(140,195);   this.add(bt1);   bt2 = new JButton("退出");   bt2.setSize(70,30);   bt2.setLocation(250,195);   this.add(bt2);   this.setBackground(Color.blue);   ///设置单击关闭按钮时默认操作      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  }  private void addListener()  {   System.out.println("0");   bt1.addActionListener(new ActionListener()   {    //System.out.println("0");    public void actionPerformed(ActionEvent e)    {     System.out.println("0");     Connection conn = JDBCUtils.getConnection();          PreparedStatement preparedstatement=null;     System.out.println("0");     ResultSet result =null;     try {      preparedstatement = conn        .prepareStatement("select *from info2 where sname=? and spass=?");      System.out.println("1");      preparedstatement.setString(1, text1.getText());      preparedstatement.setString(2, text2.getText());            System.out.println("2");      result = preparedstatement.executeQuery();      System.out.println("3");      if (result.next()) {       try {        System.out.println("4");        sname=text1.getText();        spass=text2.getText();        //new login();        dispose();       } catch (Exception e1) {        // TODO Auto-generated catch block        System.out.println("5");        e1.printStackTrace();       }              } else {       JOptionPane pane = new JOptionPane("用户或密码错误");                       JDialog dialog  = pane.createDialog("警告");                       dialog.show();      }      } catch (SQLException e1) {      // TODO Auto-generated catch block      e1.printStackTrace();     } //    if(text1.getText().equals("123")&&text2.getText().equals("123")) //    { /					new MainWindow();
//				    dispose();
//				}
//				else
//				{
//					JOptionPane.showMessageDialog(null, "登陆密码错误");
//				}
			}
		});
		
		bt2.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				System.out.println("6");
				dispose();
			}
		});

	}
	public User getUser() {
		System.out.println("7");
		return new User(sname,spass);
	}
	public static void main(String[] args) {
	 new login2();
	}
}

3.进入下一个界面...分支admin和player

待续...

锐单商城拥有海量元器件数据手册IC替代型号,打造电子元器件IC百科大全!

相关文章