java mysql

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class FetchValueClass {

	static final String URL = "jdbc:mysql://localhost/cm";
	static final String USERNAME = "user";
	static final String PASSWORD = "pass";

	public static void main(String[] args){

		String sql = "SELECT * FROM music;";

		try ( Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
			PreparedStatement statement = connection.prepareStatement(sql); ){

			ResultSet result = statement.executeQuery();

			result.next();
			System.out.println(result.getString(1));
			System.out.println(result.getString(2));
			System.out.println(result.getString(3));
			System.out.println(result.getString("id"));
			System.out.println(result.getString("name"));
			System.out.println(result.getString("title"));

			System.out.println("");

			result.next();
			System.out.println(result.getString(1));
			System.out.println(result.getString(2));
			System.out.println(result.getString(3));
			System.out.println(result.getString("id"));
			System.out.println(result.getString("name"));
			System.out.println(result.getString("title"));

		} catch(SQLException e){
			e.printStackTrace();
		}
	}
}

get Connect mysql

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

public class sampleActivity extend Activity {
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceSate);
		setContentView(R.layout.main);

		try {
			Connection con = MySqlConnect.getConnection();
			Statement stmt = (Statement) con.createStatement();

			String mySql = "select date from table;";
			ResultSet rs = stmt.executeQuery(mySql);

			while(rs.next()){
				Toast.makeText(getApplicationContext(), rs.getString("date"), Toast.LENGTH_LONG).show();

			}

			rs.close();
			stmt.close();
			con.close();
		} catch(Exception e){
			
		}
	}
}
import java.sql.*;

class MySqlConnect{
	static Connection getConnection() throws Exception {

		Class.forName("com.mysql.jdbc.Driver");

		String url = "jdbc:mysql://xxx.xxx.xxx/db_name";
		String user = "user_name";
		String pass = "password";

		Connection con = DriverManager.getConnection(url, user,pass);
		return con;
	}
}

get Json

in Android

public static InputStream is = null;
public static JSONArray json_array = null;
public static DefaultHeepClient httpClient;

public static void main(String getSERVER, String getURL, String getDB, String getID){
	SchemeRegistry schReg = new SchemeRegistry();
	schReg.register(new Scheme(HttpHost.DEFAULT_SCHEME_NAME, PlainSocketFactory.getSocketFactory(),80));

	HttpParams httpParams;
	httpParams = new BasicHttpParams();
	HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
	HttpProtocolParams.setContentCharset(httpParams, HTTP.UTF_8);

	httpClient = new DefaultHttpClient(new ThreadSafeClientConnManager(httpParams, schReg),httpParams);

	HttpResponse response = null;
	try {
		Uri.Builder uriBuilder = new Uri.Builder();
		uriBuilder.path(getURL);
		uriBuilder.appendQueryParameter("SERVER",getSERVER);
		uriBuilder.appendQueryParameter("ID",getID);
		uriBuilder.appendQueryParameter("db_name", getDB);
		response = httpClient.execute(new HttpHost(getSERVER),new HttpGet(uriBuilder.build().toString()));
	} catch(Exception e){
		Log.e("Error","接続エラー");
		return;
	}

	if(response.getStatusLine().getStatusCode() != HttpStatus.SC_OK){
		Log.e("Error",response.getStatusLine().getStatusCode());
		return;
	}

	StringBuilder json = new StringBuilder();
	try {
		HttpEntity entity = response.getEntity();
		InputStream input = entity.getContent();
		InputStreamReader reader = new InputStreamReader(input);
		BufferedReader bufReader = new BufferedReader(reader);
		String line;
		while((line = bufReader.readLine()) != null){
			json.append(line);
		}
	} catch(IOException e){
		Log.e("Error", "バッファ読み込み失敗");
		return;
	}

	try {
		JSONObject json_data = new JSONObject(json.toString());
		json_array = json_data.getJSONArray("response");
	} catch(JSONException e){
		Log.e("Error","JSONデータが不正");
		return;
	}
	return;
}

タイトルとurlを表示する

private WebView myWebView;
    private EditText urlText;

    private static final String INITIAL_WEBSITE = "http://hpscript.com/blog/";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myWebView = (WebView) findViewById(R.id.myWebView);
        urlText = (EditText) findViewById(R.id.urlText);

        myWebView.getSettings().setJavaScriptEnabled(true);
        myWebView.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageFinished(WebView view, String url){
                getSupportActionBar().setSubtitle(view.getTitle());
                urlText.setText(url);
            }
        });
        myWebView.loadUrl(INITIAL_WEBSITE);
    }

webViewを表示する

webViewにブログを表示させます。

public class MainActivity extends AppCompatActivity {

    private WebView myWebView;

    private static final String INITIAL_WEBSITE = "http://hpscript.com/blog/";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myWebView = (WebView) findViewById(R.id.myWebView);

        myWebView.loadUrl(INITIAL_WEBSITE);
    }
}

WebViewとは?

oracle:https://docs.oracle.com/javase/jp/8/javafx/api/javafx/scene/web/WebView.html
———–
WebViewは、WebEngineを管理し、その内容を表示するNodeです。関連するWebEngineは構築時に自動的に作成され、後で変更できません。WebViewにより、マウス・イベントと一部のキーボード・イベントが処理されて、スクロールが自動的に管理されるため、それをScrollPaneに配置する必要はありません。
WebViewオブジェクトは、FXスレッドからのみ作成およびアクセスする必要があります。

Handlerの使い方

new Handler().postDelayed(<Runnable object>,<m seconds>);
private final Runnable func= new Runnable(){
	@Override
	public void run(){
		
	}
};
private Handler mHandler = new Handler();
private Runnable updateText;

@Override
protected void onCreate(Bundle savedInstanceState){
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);

	updateText = new Runnable(){
		public void run(){
			TextView text = (TextView) findViewById(R.id.count);
			Integer count = Integer.valueOf(text.getText().toString());
			count += 1;
			text.setText(count.toString());
			mHandler.removeCallbacks(updateText);
			mHandler.postDelayed(updateText, 1000);
		}
	};
	mHandler.postDelayed(updateText, 1000);
}

stop watch

public class MainActivity extends AppCompatActivity {

    private long startTime;
    private long elapsedTime = 0l;

    private Handler handler = new Handler();
    private Runnable updateTimer;

    private Button startButton;
    private Button stopButton;
    private Button resetButton;
    private TextView timerLabel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        startButton = (Button) findViewById(R.id.startButton);
        stopButton = (Button) findViewById(R.id.stopButton);
        resetButton = (Button) findViewById(R.id.resetButton);
        timerLabel = (TextView) findViewById(R.id.timerLabel);

        setButtonState(true,false,false);
    }

    public void setButtonState(boolean start, boolean stop, boolean reset){
        startButton.setEnabled(true);
        stopButton.setEnabled(false);
        resetButton.setEnabled(false);

    }

    public void startTimer(View view){
        // startTimeの取得
        startTime = SystemClock.elapsedRealtime(); // 起動してからの経過時間(ミリ秒)

        // 一定時間ごとに現在の経過時間を表示
        // handler -> Runnable(処理) -> UI
        updateTimer = new Runnable(){
            @Override
            public void run(){
                long t = SystemClock.elapsedRealtime() - startTime + elapsedTime;
                SimpleDateFormat sdf = new SimpleDateFormat("mm:ss.SSS", Locale.US);
                timerLabel.setText(sdf.format(t));
                handler.removeCallbacks(updateTimer);
                handler.postDelayed(updateTimer, 10);
            }
        };
        handler.postDelayed(updateTimer, 10);


        // ボタンの操作
        setButtonState(false, true, false);
    }

    public void stopTimer(View view){
        elapsedTime += SystemClock.elapsedRealtime() - startTime;
        handler.removeCallbacks(updateTimer);
        setButtonState(true,false, true);
    }

    public void resetTimer(View view){
        elapsedTime = 0;
        timerLabel.setText("00:00.000");
        setButtonState(true,false, false);
    }
}

runnable、SimpleDateFormat、removeCallbacks、handlerがよくわからんな。

SystemClock.elapsedRealtime();

SystemClock.elapsedRealtime();はdeveloperの公式ページに掲載されています。
https://developer.android.com/reference/android/os/SystemClock
elapsedRealtime() and elapsedRealtimeNanos() return the time since the system was booted, and include deep sleep. This clock is guaranteed to be monotonic, and continues to tick even when the CPU is in power saving modes, so is the recommend basis for general purpose interval timing.

public void startTimer(View view){
        // startTimeの取得
        startTime = SystemClock.elapsedRealtime(); // 起動してからの経過時間(ミリ秒)

        // 一定時間ごとに現在の経過時間を表示
        // handler -> Runnable(処理) -> UI
        updateTimer = new Runnable(){
            @Override
            public void run(){
                long t = SystemClock.elapsedRealtime() - startTime;
                SimpleDateFormat sdf = new SimpleDateFormat("mm:ss.SSS", Locale.US);
                timerLabel.setText(sdf.format(t));
                handler.removeCallbacks(updateTimer);
                handler.postDelayed(updateTimer, 10);
            }
        };
        handler.postDelayed(updateTimer, 10);


        // ボタンの操作
        setButtonState(false, true, false);
    }