user.java

来自「JSP酒店服务管理系统,用于中小型饭店的各种服务」· Java 代码 · 共 135 行

JAVA
135
字号
package manage;

import java.sql.*;



public class user
{
	String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=preconcert"; 
  	String user="sa"; 
  	String password="";
	
	Connection conn =null; 
	Statement stmt =null;
	String sql =null;
	ResultSet rs =null;
	int n;
	

	public user()
	{
		try{
		Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
   		conn= DriverManager.getConnection(url,user,password);   
  		stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); 
		}
		catch(Exception ex)
		{
			System.out.println(ex.getMessage());
		}
	}
	public boolean insert(String username,String password,String name,String mark)
	{
		sql = "insert into login (username,password,name,mark) values(?,?,?,?)";

		try{
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setString(1,username);
		ps.setString(2,password);
		ps.setString(3,name);
		ps.setString(4,mark);
		
		
			n=ps.executeUpdate(); 

			return true;
		}
		catch(Exception e)
		{
			return false;
		}
		
	}
	public boolean del(String username)
	{
		sql = "delete from login where username=?";

		try{
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setString(1,username);
		
		
		n=ps.executeUpdate(); 
		if(n==1)
			return true;
		else
			return false;
		}
		catch(Exception e)
		{
			return false;
		}
		
	}
	public boolean updatepass(String username,String password)
	{
		sql = "update login set password=? where username=?";

		try{
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setString(1,password);
		ps.setString(2,username);
		
		
		n=ps.executeUpdate(); 
		if(n==1)
			return true;
		else
			return false;
		}
		catch(Exception e)
		{
			return false;
		}
		
	}
	public String[] select(String username)
	{
		String result[] =new String[5];
		sql = "select * from login where username=?";

		try{
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setString(1,username);
		
		
		rs=ps.executeQuery(); 
		rs.next();
		result[0]=rs.getString(1);
		result[1]=rs.getString(2);
		result[2]=rs.getString(3);
		result[3]=rs.getString(4);
		result[3]=result[3].trim();
		result[4]=rs.getString(5);
		return result;
		}
		catch(Exception e)
		{
			return null;
		}
		
	}
	public void close()
	{
		try{
 		stmt.close();   
		conn.close();
		}
		catch(Exception ex)
		{
			System.out.println(ex.getMessage());
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?