loginsystemimpl.java

来自「利用google开发的GWT工具」· Java 代码 · 共 161 行

JAVA
161
字号
package org.gjj.gwt.server;import java.io.BufferedReader;import java.io.InputStreamReader;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.LinkedList;import net.sf.jpam.*;import com.google.gwt.user.server.rpc.RemoteServiceServlet;import org.gjj.gwt.client.LoginSystem;import org.gjj.gwt.client.ResultOfReturn;public class LoginSystemImpl extends RemoteServiceServlet implements LoginSystem {		/**	 * implement abstract method isValid(),authentic user's ID	 * @author gaojinjun	 * @return boolean	 */	public boolean isValid(String userName, String password){		//return this.jpam(userName, password);		return true;	}		/**	 * implement abstract method getOutput(String post), get output of post command	 * @author gaojinjun 	 */	public ResultOfReturn getOutput(String post){		return this.postResult(post);	}		/**	 * db conn	 * 	 * Make sure you add a reference library (external jar in build path) JDBC Connector - 	 * You will see I put it in /opt/gwt-linux/mysql-connector-java-5.0.8-bin.jar	 * 	 * @return Connection	 */	private Connection getConn() {			    Connection conn	= null;		    String url 		= "jdbc:mysql://localhost:3306/";		    String db 		= "test1";		    String driver 	= "com.mysql.jdbc.Driver";		    String user 	= "root";		    String pass 	= "gaojinjun07";		    System.out.println("connecting");		    try {		      		    	Class.forName(driver).newInstance();		    	conn = DriverManager.getConnection(url+db, user, pass);		      		    } catch (Exception e) {		    			    	//error		    	System.err.println("Mysql Connection Error: ");		    	//e.printStackTrace();		    }				    return conn;	}			/*	 * get row count	 */	protected static int getResultSetSize(ResultSet resultSet) {	    int size = -1;	    try {	        resultSet.last();	        size = resultSet.getRow();	        resultSet.beforeFirst();	    } catch(SQLException e) {	        return size;	    }	    return size;	}		/**	 * method to test query	 */	public boolean queryMyDB(String userName, String password) {				boolean flag = false;	    String Query = "select * from user;";	        	    try {	        Connection connection = this.getConn();	        Statement select = connection.createStatement();	        ResultSet result = select.executeQuery(Query);	        //get count so I can set my array to the right length	        int rsSize = getResultSetSize(result); 	        	        while (result.next()) {	        		if (result.getString(1).equals(userName) && result.getString(2).equals(password)){	        			flag = true;	        			break;	        		}	        }	        connection.close();	    } catch(Exception e) {	        	        //debug out output this way	        System.err.println("Mysql Statement Error: " + Query);	        e.printStackTrace();	        	    }	    System.out.println(flag);	     return flag;   	}		/**	 * use Jpam to authenticate users	 * @author gaojinjun	 * @return boolean	 */	public boolean jpam(String userName, String password){		System.out.println(userName+password);		boolean flag = false;		Pam pam = new Pam();		flag = pam.authenticateSuccessful(userName, password);		return flag;	}		/**	 * run system call, execute command of post, which is front end transforms to here	 * @author gaojinjun	 * @return String[] 	 */	private ResultOfReturn postResult(String post){		String str = null;		LinkedList<String> result = new LinkedList<String>();		ResultOfReturn ret = new ResultOfReturn();		Runtime run = Runtime.getRuntime();		Process process = null;		try{			process = run.exec(post);			BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));			while(null != (str = br.readLine())){				System.out.println(str);				result.add(str);			}		}		catch(java.io.IOException e){			System.out.println("Error while run " + post);		}		ret.setResult(result.toArray(new String[0]));		return ret;	}}

⌨️ 快捷键说明

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