也許你可以發送一個請求到有問題的端口,假設它是WebServer或TCP服務器(默認端口分別是8082或9092)。你知道呼叫
drewmac:BIN drewpierce $ java命令H2的* .jar org.h2.tools.Server
創建3個服務器和吐出類似:
TCP服務器上運行在tcp://192.168.1.3:9092
(僅限本地連接)
PG服務器在運行pg://192.168.1.3:5435
(僅限本地連接)
Web控制檯服務器在運行http://192.168.1.3:8082
(僅限本地連接)
那麼如果調用程序,以顯示你的服務器端口,監聽套接字 和我的電話是 drewmac:〜drewpierce $ 須藤lsof的-i -P | grep的-i 「聽」
java的81339 drewpierce 19U的IPv6 0xffffff800b782ac0 0t0 TCP *:(LISTEN)
java的81339 drewpierce 22U的IPv6 0xffffff800b781bc0 0t0 TCP *:(LISTEN)
java的81339 drewpierce 24U的IPv6 0xffffff8015620800 0t0 TCP *:(LISTEN)
現在如果你要測試的Web服務器(HTTP和HTML
流到端口80,8082,無論你做什麼,你都可以發佈Chunk A.如果你想測試TCP服務器,你可以發佈Chunk B.
呼叫一大塊像的Java GreetingClient本地主機8082
或類似的Java GreetingClient ec2-1-2-3-4-amaz-aws-ec2.amazon.com 8082
不要」噸忘記2個參數在該測試中,否則它將BARF
堆塊A:
import java.io.*;
import java.net.*;
public class GreetingClient {
public static void main(String[] args) {
// declaration section:
// mySocket: our client socket pretending to be a browser
// os: output stream
// is: input stream
Socket mySocket = null;
DataOutputStream os = null;
DataInputStream is = null;
String serverName = args[0];
int port = Integer.parseInt(args[1]);
// Initialization section:
// btw make sure parameters are passed noting that this quick code is NOT
// Try to open input and output streams
System.out.println("*1");
try {
mySocket = new Socket(serverName,port);
os = new DataOutputStream(mySocket.getOutputStream());
is = new DataInputStream(mySocket.getInputStream());
} catch (UnknownHostException e) {
System.err.println("Don't know about host: hostname");
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to: hostname");
}
System.out.println("*2");
// If everything has been initialized then we want to write some data
// to the socket we have opened a connection to on port 80, 8082, whatever
// (what the server is listening on)
if (mySocket != null && os != null && is != null) {
try {
// pretend to be a browser and do a GET against a resource
System.out.println("*3");
os.writeBytes("GET /index.html HTTP/1.0\r\n\r\n");
System.out.println("*4");
// wait for response from webserver, dump out response for sanity check
String responseLine;
while ((responseLine = is.readLine()) != null) {
System.out.println("Server: " + responseLine);
if (responseLine.indexOf("Ok") != -1) {
break;
}
}
// clean up:
// close the output stream
// close the input stream
// close the socket
System.out.println("*5");
os.close();
is.close();
mySocket.close();
} catch (UnknownHostException e) {
System.err.println("Trying to connect to unknown host: " + e);
} catch (IOException e) {
System.err.println("IOException: " + e);
}
}
System.out.println("*6");
}
}
組塊(至少對我來說)A輸出:
*1
*2
*3
*4
Server: HTTP/1.1 200 OK
Server: Content-Type: text/html
Server: Cache-Control: no-cache
Server: Content-Length: 937
Server:
Server: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
Server: <!--
Server: Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0,
Server: and the EPL 1.0 (http://h2database.com/html/license.html).
Server: Initial Developer: H2 Group
Server: -->
Server: <html><head>
Server: <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
Server: <title>H2 Console</title>
Server: <link rel="stylesheet" type="text/css" href="stylesheet.css" />
Server: <script type="text/javascript">
Server: location.href = 'login.jsp?jsessionid=f3d05d9b68f4c5407054628f096ffccb';
Server: </script>
Server: </head>
Server: <body style="margin: 20px;">
Server:
Server: <h1>Welcome to H2</h1>
Server: <h2>No Javascript</h2>
Server: If you are not automatically redirected to the login page, then
Server: Javascript is currently disabled or your browser does not support Javascript.
Server: For this application to work, Javascript is essential.
Server: Please enable Javascript now, or use another web browser that supports it.
Server:
Server: </body></html>
*5
*6
有幾件事情,顯然這是H2輸出。塊源代碼可以減少到約10行。
組塊B(談話JDBC到TCP JDBC服務器)
//STEP 1. Import required packages
import java.sql.*;
import org.h2.Driver;
public class JdbcTrial {
// JDBC driver name and database URL
//static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
//static final String DB_URL = "jdbc:mysql://127.0.0.1/test";
static final String JDBC_DRIVER = "org.h2.Driver";
static final String DB_URL = "jdbc:h2:tcp://localhost/~/test";
// Database credentials
static final String USER = "sa";
static final String PASS = "";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
//STEP 2: Register JDBC driver
System.out.println("***** 1");
Class.forName(JDBC_DRIVER);
System.out.println("***** 2");
//STEP 3: Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
//STEP 4: Execute a query
System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql;
sql = "SELECT id, FirstName, LastName from people";
ResultSet rs = stmt.executeQuery(sql);
//STEP 5: Extract data from result set
while(rs.next()){
//Retrieve by column name
int id = rs.getInt("id");
String first = rs.getString("FirstName");
String last = rs.getString("LastName");
//Display values
System.out.print("ID: " + id);
System.out.print(", First: " + first);
System.out.println(", Last: " + last);
}
//STEP 6: Clean-up environment
rs.close();
stmt.close();
conn.close();
}catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}finally{
//finally block used to close resources
try{
if(stmt!=null)
stmt.close();
}catch(SQLException se2){
}// nothing we can do
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}//end finally try
}//end try
System.out.println("Goodbye!");
}//end main
}//end FirstExample
組塊B輸出:
drewmac:~ drewpierce$ java JdbcTrial
***** 1
***** 2
Connecting to database...
Creating statement...
ID: 1, First: joan, Last: london
ID: 2, First: Sgt., Last: Corholio
Goodbye!
工作得很好對MySQL和MariaDB的,和H2,只是通過與jdbc_driver和db_url搞亂
你可以做套接字連接,而不是發出數據檢索調用,真正修剪它。
至於如何用H2 getStatus做到這一點,我不知道。祝你好運。
你正在使用什麼版本的H2? – marcolopes
H2版本是1.4.184 – rajeesh
你能提供一些代碼嗎?你是如何開始H2服務器的? –